1 / 45

Directories and the Directory Interface

Directories and the Directory Interface. Directories Structure. Recall that in Unix your hard drive is organized into a hierarchical system of directories and files. Root. B. A. C. B. A. C. C. B. B. B. C. C. C. C. char* getcwd(char* buff, size_t size).

keita
Download Presentation

Directories and the Directory Interface

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. Directories and the Directory Interface

  2. Directories Structure Recall that in Unix your hard drive is organized into a hierarchical system of directories and files. Root B A C B A C C B B B C C C C

  3. char* getcwd(char* buff, size_t size) returns the full pathname of the current working directory returns NULL if the operation fails.

  4. Directories All of the directory information is defined in the header file dirent.h A pointer to a DIR structure is used to manipulate directory information, much as a FILE structure is used to manipulate files.

  5. The dirent Structure On Linux The dirent structure is defined as (see readdir man page) struct dirent { ino_t d_ino; // inode number off_t d_off; // offset to next dirent unsigned short d_reclen; // length of this record unsigned short d_type; // type of file char d_name[256]; // filename }; types defined in <sys/types.h> This is the only field guaranteed by Posix

  6. dirent File Types DT_UNKNOWN 0 DT_FIFO 1 DT_CHR 2 DT_DIR 4 DT_BLK 6 DT_REG 8 DT_LNK 10 DT_SOCK 12 DT_WHT 14

  7. Opening a Directory #include <dirent.h> #include <sys/types.h> DIR *opendir (const char *name); If the open succeeds, a pointer to a directory structure is returned. Otherwise a NULL pointer is returned. see the man page for opendir!

  8. Reading from a Directory #include <dirent.h> #include <sys/types.h> struct dirent *readdir (DIR *dirp); readdir reads the next entry from the directory and returns a pointer to a dirent structure. A null is returned if the end of the directory is encounterd. see the man page for readdir!

  9. Example Listing the files in a directory

  10. directory DIR* dirp directory entry directory entry directory entry . . . ino_td_ino; off_td_off; unsigned short d_reclen; unsigned short d_type; char d_name[256]; struct dirent* direntptr

  11. Other Directory Operations telldir( ) returns the current location in the directory stream seekdir( ) sets the position in the directory stream rewinddir( ) sets the position to the beginning of the directory stream closedir( ) closes a directory

  12. How Does ls Work? links $ ls -al total 104 drwxr-xr-x 25 debryro staff 850 Aug 3 14:50 . drwxr-xr-x 13 debryro staff 442 Jun 15 13:09 3060 drwxr-xr-x 2 debryro staff 68 Jun 15 13:08 CNS3060 -rw-r--r-- 1 debryro staff 36 Jun 14 14:09 afile -rw-r--r-- 2 debryro staff 16 Jun 14 15:08 file-a -rw-r--r-- 2 debryro staff 16 Jun 14 15:08 file-b lrwxr-xr-x 1 debryro staff 6 Jun 14 15:11 file-c -> file-a -r-xr-xr-x 1 debryro staff 1809 Jun 14 11:34 monthly drwxr-xr-x 4 debryro staff 136 Aug 1 14:59 more owner group size last modified name permissions file type

  13. Where does ls get its information? file attributes are returned in a stat structure. The header file <sys/stat.h> declares all of the symbols used in this structure. File attributes are returned in the following three functions: int stat (const char *filename, struct stat *buf); int lstat (const char *filename, struct stat *buf); int fstat (int filedes, struct stat *buf); when the file is a symbolic link, lstat returns information about the link, while stat returns information about the file the link points to! returns 0 when successful -1 when fails fstat returns information about a file that is already open.

  14. stat Structure on Ubuntu 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 */ };

  15. File Permissions

  16. st_mode bits permission bits r w r w r w x x x suid sgid sticky file type User (the file owner) group other

  17. ls –l myscript.sh - r w x r - x - - - 1 mtk users 1667 Jan 15 0922 myscript.sh permission bits

  18. Whenever you want to open a file by name, you need execute permission in every directory in the path. Read permission on a directory means that you can list the contents of the directory. To create a file in a directory you need both read and execute permission. You can delete a file if you have write and execute permission in the directory. You do not need read or write permission for the file itself.

  19. st_mode bits r w r w r w x x x suid If the suid (set user id) bit is set on an executable file, normal users who have permission to execute the file gain the privileges of the file owner (commonly this is root).

  20. File Ownership Every process has six or more IDs associated with it. These identify who we really are. They are taken from the password file when we log in. These are properties of the file. a real user id a real group id an effective user id an effective group id When we execute a program, the effective user id is usually the same as the real user id and the effective group id is usually the same as the real group id. These are properties of the process. It is possible to set a flag that says “when this file is executed, set the effective user ID of the process to be the owner of the file. Example: if the file owner is root, and the set-user-ID bit is set, then while that file is running, it has root privileges (example – passwd) Test the set-User-ID bit with the constant S_ISUID

  21. st_mode bits r w r w r w x x x sgid If the sgid (set group id) bit is set, then the program is executed as if it were being run by someone in the group that owns the program.

  22. r w r w r w x x x Stick bit In the olden days, the sticky bit meant to keep this file on the swap device, even if it was not being used right now … no longer used.

  23. The fileinfo Example

  24. Reading st_mode bits Use the following macros to test the file type int S_ISDIR (mode_t m); // returns non-zero if a directory int S_ISCHR (mode_t m); // returns non-zero if a char device int S_ISBLK (mode_t m); // returns non-zero if a block device int S_ISREG (mode_t m); // returns non-zero if a regular file int S_ISFIFO (mode_t m); // returns non-zero if a pipe int S_ISLNK (mode_t m) returns non-zero if a symbolic link int S_ISSOCK (mode_t m); // returns non-zero if a socket Example usage: if (S_ISREG(buf.st_mode) ) printf(“regular file”);

  25. Use these masks for the permission bits S_IRUSR read bit for file owner S_IWUSR write bit for file owner S_IXUSR execute but for file owner S_IRGRP read bit for group S_IWGRP write bit for group S_IXGRP execute bit for group S_IROTH read bit for other S_IWOTH write bit for other S_IXOTH execute bit for other Example: if ( buf.st_mode & S_IRUSR) printf (“user read bit is on”);

  26. Getting a name from the passwd file passwd* getpwuid(uid) struct passwd { char* pw_name; char* pw_passwd _uid_t pw_uid; _gid_t pw_gid; char* pw_gecos; char* pw_dir; char* pw_shell; }

  27. Dealing with Time Values Field Description Example ls option st_atime last access time of file read -u st_mtime last modification time write default st_ctime last change time (inode) chmod -c These fields are all of type time_t

  28. The basic Linux (and Unix) time service counts the number of seconds that have passed since January 1, 1970 (UTC). These seconds are normally stored in a variable whose type is time_t. This is called calendar time.

  29. We can get calendar time by using the call time_t time (time_t *tptr); returns the time, and stores the value here if the argument is non-null.

  30. Summary of Time Functions string formatted string asctime strftime struct tm broken down time ctime localtime gmtime mktime time_t calendar time time kernel takes time zone into account

  31. Broken down time struct tm { int tm_sec; // seconds after the minute [0-61] int tm_min; // minutes after the hour [0-59] int tm_hour; // hours after midnight [0-23] int tm_mday; // day of the month [1-31] int tm_mon; // month of the year [0-11] int tm_year; // years since 1900 int tm_wday; // day of the week [0-6] int tm_yday; // days since Jan 1 [0-365] int tm_isdst // daylight savings flag [pos, 0, neg] }; allows leap seconds struct tm Work with tm time when you want to manipulate individual fields.

  32. #include <time.h> struct tm *gmtime (const time_t *tptr); struct tm *localtime (const time_t *tptr); Converting time_t to tm broken down time is in UTC broken down time is in local time, given time zone and daylight savings

  33. Converting tm to time_t time_t mktime (struct tm *tptr);

  34. char *asctime (const struct tm *tmptr); char *ctime (const time_t *tptr); generating time strings Takes time zone into account both generate a 26 character string of the form Tue Sep 26 16:49:05 2002\n\0

  35. Generating a formatted string size_t strftime (char *tbuf, size_t maxsize, const char *format, const struct tm *tptr); the buffer to store the string in, and its max size a broken down time the format string – works like printf returns the size of the formatted string if successful 0 if fails

  36. Format Description Example %a abbreviated weekday name Tue %A full weekday name Tuesday %b abbreviated month name Jan %B full month name January %c date and time Tue Jan 14 19:40:05 2002 %d day of the month [00-31] 14 %H hour of 24 hour day [00-23] 19 %I hour of 24 hour day [00-12] 07 %j day of the year [001-366] 014 %m month [01-12] 01 %M minute [00-59] 40 %p am or pm PM %S second [00-61] 05 %U Sunday week number [00-53] 02 %w weekday [0=Sunday – 6] 5 %W Monday week number [00-53] 07 %x date 01/14/02 %X time 19:40:04 %y year without century[00-99] 02 %Y year with century 2002 %Z time zone name MST

  37. Whenever we want to open any type of file by name, we must have execute permissions in each directory mentioned in the path to the file, including the current directory. Read permission on a directory only means that you can read the directory – obtaining a list of file names in the directory. We cannot create a new file in a directory unless we have both write and execute permissions for the directory. To delete a file we need both write and execute permissions in the directory. Note that you do not need read and write permissions for the file! Some File Access Permission Rules

  38. The kernel performs the following access tests each time a process opens, creates, or deletes a file: File access tests If the effective user ID of the process is 0, access is allowed. If the effective user ID of the process equals the owner ID of the file, access is allowed if the appropriate permission bit is set. If the effective group ID of the process equals the group ID of The file, access is allowed if the appropriate permission bit is set. If the appropriate other permission bit is set access is allowed.

  39. #include <sys/types.h> #include <sys/stat.h> int chmod (const char *pathname, mode_t mode); int fchmod(int fd, mode_t mode); Changing File Permissions If (chmod(“foo”, S_IRUSR | S_IWUSR | S_IXUSR) < 0) // error code …

  40. #include <sys/stat.h> #include <unistd.h> int chown (const char *pathname, uid_t owner, gid_t group); int fchown ( int fd, uid_t owner, gid_t group); int lchown (const char *pathname, uid_t owner, gid_t group); Changing File Ownership Changes the owner of the symbolic link, not The owner of the file linked to. Normally, only the file owner can change ownership of the file.

  41. Getting the name of the Current Working Directory #include <unistd.h> extern char *getcwd (char *buf, size_t size); size is max size of the path name that can be returned.

  42. Project #4du command

  43. To get the full pathname of the current directory, use the getcwd( ) function. Use the d_type field in the dirent structure to get the type of file (a regular file or a directory) Use the d_name field in the dirent structure to get the name of the file Use the stat( ) function to get information for a regular file. The file size in blocks is in the st_blocks field of the stat structure. Read chapter 3 in Molay for more ideas. Project #4du command

More Related