1 / 18

Introduction to Unix Operating System

Introduction to Unix Operating System. Additional File System Calls Directory System Calls. Today’s Agenda. Additional file system calls. File system calls truncate and ftruncate fstat, stat and lstat (struct stat) rename (renames a file or directory)

lalasa
Télécharger la présentation

Introduction to Unix Operating System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unix Operating System

  2. Additional File System Calls Directory System Calls Today’s Agenda

  3. Additional file system calls • File system calls • truncate and ftruncate • fstat, stat and lstat (struct stat) • rename (renames a file or directory) • chmod (changes permission on file/directory)

  4. truncate() #include<unistd.h> int truncate( const char *path, /*pathname*/ off_t length); /*new length*/ /*Returns 0 on success and -1 on failure*/ #include<unistd.h> int truncate( int fd, /*file descriptor*/ off_t length); /*new length*/ /*Returns 0 on success and -1 on failure*/

  5. Accessing and displaying file Metadata • stat(),fstat(),lstat() systam calls • These system calls helps to retrieve file meta data (inode info) like owner, modification time, access permissions etc. #include<sys/stat.h> int stat( const char *path, /*pathname*/ struct stat *buf); /*returned information*/ /*Returns 0 on success and -1 on failure*/ struct stat will store all the information which can be printed

  6. Struct stat • struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };

  7. The following POSIX macros are defined to check the file type using the st_mode field: S_ISREG(m) is it a regular file? S_ISDIR(m) directory? S_ISCHR(m) character device? S_ISBLK(m) block device? S_ISFIFO(m) FIFO (named pipe)? S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) S_ISSOCK(m) socket? (Not in POSIX.1-1996.)

  8. Directory System Calls • getcwd() • mkdir() • chdir() • rmdir() • opendir() • closedir() • readdir()

  9. getcwd System Call • To get the current working directory in UNIX, we use the “pwd” command. • The system call behind the “pwd” command is the getcwd() call. # include<unistd.h> char *getcwd( char *buf, /* Returned pathname */ size_t bufsize /* sizeof buf */ ); /* Returns pointer to 'buf' on success and NULL on error */ bufsize should the maximum size of path. Following system call to get maximum size of path long max= pathconf(“/”,_PC_PATH_MAX);

  10. A program that uses the getcwd() # include<stdio.h> # include<unistd.h> int main(void) { long max; char *buf; max= pathconf(“/”,_PC_PATH_MAX); buf=(char*)malloc(max); getcwd(buf,max); printf(“%s\n”,buf); return 0; }

  11. chdir System Call(cdcommand in Unix) # include<unistd.h> int chdir( const char *path; ); /* Returns zero on success and -1 on error */ This system call changes the current working directory to that specified in “path”.

  12. Make and remove dir System Call # include<sys/stat.h> intmkdir( const char *path, /* Pathname */ mode_t perms /* Permissions */ ); /* Returns 0 on success and -1 on error */ intrmdir( const char *path /* Pathname */ ); /* Returns 0 on success and -1 on error */

  13. Program to make and remove directory int main(int argc,char *argv[]) { int md,rd; DIR *ds; struct dirent *dir; md = mkdir(argv[1],0777); if(md == 0) printf("%s directory is created\n",argv[1]); else printf("%s directory is not created\n",argv[1]); rd = rmdir(argv[2]); if(rd == 0) printf("%s directory is removed\n",argv[2]); else printf("%s directory is not removed\n",argv[2]); }

  14. opendir and closedir System Calls # include<dirent.h> DIR* opendir( const char *path /* directory pathname */ ); /* Returns a DIR pointer or NULL on error */ # include<dirent.h> int closedir( DIR *dirp /*DIR pointer from opendir */ ); /* Returns a 0 on success or -1 on error */

  15. readdir System Call and dirent structure #include <dirent.h> struct dirent *readdir( DIR *dirp /* DIR Pointer from opendir */ ); /* Returns structure or NULL on EOF or error */ struct dirent { ino_t d_ino; /* i-number */ char d_name[]; /* name */ };

  16. int main(int argc,char *argv[]) { DIR *ds; struct dirent *dir; ds = opendir(argv[1]); if(ds == NULL) printf("directory %s is not opened\n",argv[1]); else printf("ds = %ld\n",ds); printf("list of files and directories\n"); while((dir = readdir(ds)) != NULL) printf("%s\n",dir->d_name); if((cld = closedir(ds)) == 0) printf("%s is successfully closed\n",argv[1]); else printf("%s is not successfully closed\n",argv[1]); } [Mayuri@localhost dirProgs]$ ./a.out /home/Mayuri/Downloads/ open the directory ds = 9236496 list of files and directories .. . SimplescalarHowto.pdf Installation_Guide_for_SimpleScalar.pdf p130-spradling.pdf /home/Mayuri/Downloads/ is successfully closed [Mayuri@localhost dirProgs]$

  17. program to display the detailed directory structure (output) mayuri@atlas:~$ ./a.out dsa Directory scan of dsa lab1/ SortedSeqList.h greedyAlgos/ a.out coinset.c IntervalScheduling/ a.out dataDef.h output.txt schOps.h input.txt main.c schedulingOps.c schedulingOps.o **************DONE************ mayuri@atlas:~$

  18. Exercise program • Write a program to mimic ls –l command in unix. • Write a program to display number of files in current working directory. • Write a program which performs rm –r unix command.

More Related