1 / 19

Files and Directories

Files and Directories. Properties of files ownership, permissions, etc. Functions relating to files stat, fstat, lstat access umask chmod, fchmod chown, fchown, lchown. stat Functions. Three basic stat functions stat, fstat, lstat return 0 ok, -1 on error all three take buffer args

xandy
Download Presentation

Files and Directories

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. Files and Directories • Properties of files • ownership, permissions, etc. • Functions relating to files • stat, fstat, lstat • access • umask • chmod, fchmod • chown, fchown, lchown

  2. stat Functions • Three basic stat functions • stat, fstat, lstat • return 0 ok, -1 on error • all three take buffer args • fill with information on files • stat structure system dependent • stat and lstat take pathnames • fstat takes file descriptor of open file • lstat returns info on symbolic link

  3. stat Structure • system defined structure which contains file information • ‘man stat’ should return local details • common elements include • st_mode - file type and permissions • st_uid - uid of owner • st_gid - gid of owner • st_size - size in bytes

  4. File Types • Regular file - data in some form • Directory - file with data on files • Character special - device file • Block special - device file • FIFO - named pipe for interprocess communication • Socket - interprocess communication • Symbolic link - file that points to a file • st_mode element of stat structure shows type

  5. user and group ID’s • properties of processes • real user, real group • set at login • effective user, effective group, supplementary group • establish permissions, changeable • saved set-user-ID, set-group-ID • set to effective when program executes

  6. real UID, GID • real UID and GID established at login • normally unchanged in session, but can be changed by root • ‘id’ command will show UID, GID, and groups for specific user (effective if no user given)

  7. effective UID, GID • effective ID’s normally match real ID’s • effective ID’s can be changed via flag in st_mode • effective ID’s can be changed under a login session using the ‘su’ (substitute user) command from the terminal

  8. set-user-ID, set-group-ID bits • bits in st_mode which force effective user and group ID’s. • programs with set-user-ID bit set will run under the effective user ID of the programs owner • programs with set-group-ID bit set will run under the effective group ID of the programs group

  9. Access Permissions • nine permission bits per file • user, group, other • read, write, execute • st_mode contains values

  10. Permission rules • opening a file requires execute (search) permissions in all intervening directories, write permission is required for truncation • creating a new file requires write and execute permission on it’s directory • deleting a file requires write and execute permissions on it’s directory (NOT on the file) • execute permissions are required to use the exec function

  11. File access checks • EUID of 0 (root) give unlimited access • EUID = owner of file • bit set, action allowed, else denied • EGID/SGID = group of file • bit set, action allowed, else denied • other bit controls all other access • steps tried in sequence until match, then finished

  12. New File Ownership • files and directories have same ownership rules • user ID of new file set as effective ID of creating process • group ID of new file has two possible values • effective group ID of creating process • group ID of containing directory • LINUX uses SVR4 rules • SGID bit forces directory matching • no SGID bit forces process EGID matching

  13. access Function • Tests for access to file under real user ID • takes pathname, mode • returns 0 OK, -1 on error

  14. umask Function • used to set permissions mask for file creation • takes mask • bitwise OR of permission bits • returns previous mask • no error return • shell umask command can set current mask (for shell session)

  15. chmod and fchmod Functions • Used to change access permissions and special states • chmod used for file • fchmod used for open file descriptor • both take mode_t values • 9 permissions values plus suid, sgid, and sticky • clears sgid and sticky if permissions incorrect • chmod takes pathname, fchmod FD • both return 0 OK, -1 on error

  16. Sticky Bit • historically used to save text to swap • currently used for directories • allows only file/directory owner or root to remove or rename contained file

  17. chown, fchown, and lchown Functions • used to change user or group ID of file • chown, lchown take pathnames • fchown takes FD • lchown changes ownership of symbolic link • all take UID and GID • all return 0 OK, -1 on error

  18. File Size • meaningful for regular files, directories, symbolic links • size in bytes • directory size is size of directory file • symbolic link size is size of pathname

  19. File Truncation • reduce (normally) file size • some systems will extend • truncate, ftruncate functions • truncate takes pathname • ftruncate takes FD • both take new file size in bytes • both return 0 OK, -1 on error

More Related