+ All Categories
Home > Documents > Kernel Directory Interface

Kernel Directory Interface

Date post: 18-Jan-2016
Category:
Upload: gwen
View: 26 times
Download: 0 times
Share this document with a friend
Description:
Kernel Directory Interface. operating systems. Recall that in Unix your hard drive is organized into a hierarchical system of directories and files. Root. Directories. B. A. C. B. A. C. C. B. B. B. C. C. C. C. operating systems. Directories. - PowerPoint PPT Presentation
Popular Tags:
36
Kernel Directory Kernel Directory Interface Interface
Transcript
Page 1: Kernel Directory Interface

Kernel Directory InterfaceKernel Directory InterfaceKernel Directory InterfaceKernel Directory Interface

Page 2: Kernel Directory Interface

DirectoriesDirectoriesDirectoriesDirectoriesRecall that in Unix your hard drive is organizedinto a hierarchical system of directories and files.

Root

A B C

A

C C

B

C

B

B B

C C

C

operatingsystems

Page 3: Kernel Directory Interface

DirectoriesDirectoriesDirectoriesDirectories

All of the directory functions are 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.

operatingsystems

Page 4: Kernel Directory Interface

Opening a DirectoryOpening a DirectoryOpening a DirectoryOpening a Directory

#include <dirent.h>#include <sys/types.h>

DIR *opendir (const char *name);

If the open succeeds, a pointer toa directory structure is returned.Otherwise a NULL pointer is returned.

operatingsystems

see the man page for opendir!

Page 5: Kernel Directory Interface

Reading from a DirectoryReading from a DirectoryReading from a DirectoryReading from a Directory

#include <dirent.h>#include <sys/types.h>

struct dirent *readdir (DIR *dirp);

readdir reads the next entry from the directoryand returns a pointer to a dirent structure.

operatingsystems

see the man page for readdir!

Page 6: Kernel Directory Interface

The dirent structureThe dirent structureThe dirent structureThe dirent structureoperatingsystems

On OS/X The dirent structure is defined as (see dirent man page)

struct dirent { u_int32_t d_fileno; // files inode number u_int16_t d_reclen; // length of this record u_int8_t d_type; // file type u_int8_t d_namlen; // length of name string char d_name[255 + 1]; // name};

This is the only field guaranteed by Posix

types definedin <sys/types.h>

Page 7: Kernel Directory Interface

dirent File Typesdirent File Typesdirent File Typesdirent File Typesoperatingsystems

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

Page 8: Kernel Directory Interface

operatingsystems

What Else Can You Do What Else Can You Do with a Directory?with a Directory?

telldir( ) returns the current location in thedirectory stream

seekdir( ) sets the position in the directorystream

rewinddir( ) sets the position to the beginningof the directory stream

closedir( ) closes a directory

Page 9: Kernel Directory Interface

operatingsystems

Look at an Example

Page 10: Kernel Directory Interface

The ls CommandThe ls CommandThe ls CommandThe ls Command

$ ls -altotal 104drwxr-xr-x 25 debryro staff 850 Aug 3 14:50 .drwxr-xr-x 13 debryro staff 442 Jun 15 13:09 3060drwxr-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-blrwxr-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 monthlydrwxr-xr-x 4 debryro staff 136 Aug 1 14:59 more

file typepermissions

links

owner group size last modified name

Page 11: Kernel Directory Interface

Where does ls get its Where does ls get its information?information?

Where does ls get its Where does ls get its information?information?

file attributes are returned in a stat structure. Theheader file <sys/stat.h> declares all of the symbols usedin 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);

returns 0 when successful -1 when fails

when the file is a symbolic link, lstat returns information about the link, while stat returns information about the file the link points to!

fstat returns information about a file thatis already open.

operatingsystems

Page 12: Kernel Directory Interface

stat structure stat structure (os/X)(os/X)stat structure stat structure (os/X)(os/X)

struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* File serial number */ mode_t st_mode; /* Mode of file */ nlink_t st_nlink; /* Number of hard links */ uid_t st_uid; /* User ID of the file */ gid_t st_gid; /* Group ID of the file */ dev_t st_rdev; /* Device ID */ time_t st_atime; /* Time of last access */ long st_atimensec; /* nsec of last access */ time_t st_mtime; /* Last data modification time */ long st_mtimensec; /* last data modification nsec */ time_t st_ctime; /* Time of last status change */ long st_ctimensec; /* nsec of last status change */ off_t st_size; /* file size, in bytes */ blkcnt_t st_blocks; /* blocks allocated for file */ blksize_t st_blksize; /* optimal blocksize for I/O */ __uint32_t st_flags; /* user defined flags for file */ __uint32_t st_gen; /* file generation number */ __int32_t st_lspare; /* RESERVED: DO NOT USE! */ __int64_t st_qspare[2]; /* RESERVED: DO NOT USE! */};

see /usr/include/sys.stat.h

operatingsystems

Page 13: Kernel Directory Interface

Reading st_mode bitsReading st_mode bitsReading st_mode bitsReading st_mode bits

Use the following macros to test the file typeint S_ISDIR (mode_t m); // returns non-zero if a directoryint S_ISCHR (mode_t m); // returns non-zero if a char deviceint S_ISBLK (mode_t m); // returns non-zero if a block deviceint S_ISREG (mode_t m); // returns non-zero if a regular fileint S_ISFIFO (mode_t m); // returns non-zero if a pipeint S_ISLNK (mode_t m) returns non-zero if a symbolic linkint S_ISSOCK (mode_t m); // returns non-zero if a socket

Example usage: if (S_ISREG(buf.st_mode) ) printf(“regular file”);

operatingsystems

Page 14: Kernel Directory Interface

Use these masks for the permission bitsS_IRUSR read bit for file ownerS_IWUSR write bit for file ownerS_IXUSR execute but for file owner

S_IRGRP read bit for groupS_IWGRP write bit for groupS_IXGRP execute bit for group

S_IROTH read bit for otherS_IWOTH write bit for otherS_IXOTH execute bit for other

Example:if ( buf.st_mode & S_IRUSR) printf (“user read bit is on”);

operatingsystems

Page 15: Kernel Directory Interface

Look at Some Examples

operatingsystems

Page 16: Kernel Directory Interface

File OwnershipFile OwnershipFile OwnershipFile Ownership

Every process has six or more IDs associated with it.

a real user ida real group id

These identify who we really are.They are taken from the password file when we log in. These are properties of the file.

an effective user idan effective group id

When we execute a program, theeffective user id is usually the sameas the real user id and the effectivegroup id is usually the same as the realgroup id. These are properties of theprocess.

It is possible to set a flag that says“when this file is executed, set theeffective 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, thenwhile that file is running, it has rootprivileges (example – passwd)

Test the set-User-ID bit with the constant S_ISUID

operatingsystems

Page 17: Kernel Directory Interface

Some File Access Some File Access Permission RulesPermission RulesSome File Access Some File Access Permission RulesPermission Rules

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 canread 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!

operatingsystems

Page 18: Kernel Directory Interface

File access testsFile access testsFile access testsFile access testsThe kernel performs the following access tests each time a process opens, creates, or deletes a file:

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 ofThe file, access is allowed if the appropriate permission bit is set.

If the appropriate other permission bit is set access is allowed.

operatingsystems

Page 19: Kernel Directory Interface

Changing File Changing File PermissionsPermissions

Changing File Changing File PermissionsPermissions

#include <sys/types.h>#include <sys/stat.h>

int chmod (const char *pathname, mode_t mode);

int fchmod(int fd, mode_t mode);

If (chmod(“foo”, S_IRUSR | S_IWUSR | S_IXUSR) < 0) // error code …

operatingsystems

Page 20: Kernel Directory Interface

Changing File OwnershipChanging File OwnershipChanging File OwnershipChanging File Ownership

#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);

Changes the owner of the symbolic link, notThe owner of the file linked to. Normally, only the file owner can

change ownership of the file.

operatingsystems

Page 21: Kernel Directory Interface

Time ValuesTime ValuesTime ValuesTime Values

Field Description Example ls option

st_atime last access time of file read -ust_mtime last modification time write defaultst_ctime last change time (inode) chmod -c

operatingsystems

Page 22: Kernel Directory Interface

Unix Time FunctionsUnix Time FunctionsUnix Time FunctionsUnix Time Functions

A digression …A digression …

operatingsystems

Page 23: Kernel Directory Interface

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 typeis time_t.

This is called calendar time.

operatingsystems

Page 24: Kernel Directory Interface

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.

operatingsystems

Page 25: Kernel Directory Interface

time_t

struct tm

string formatted string

kernel

time

calendar time

broken down time

gmti

me

loca

ltim

e

mkt

ime

ctime

asctimestrftime

takes time zone into account

operatingsystems

Page 26: Kernel Directory Interface

struct tmstruct tmstruct tmstruct tm

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

operatingsystems Broken down time

Page 27: Kernel Directory Interface

Converting time_t to tmConverting time_t to tmConverting time_t to tmConverting time_t to tm

#include <time.h>

struct tm *gmtime (const time_t *tptr);

struct tm *localtime (const time_t *tptr);broken down time is in local time, given time zone and daylight savings

broken down time is in UTC

operatingsystems

Page 28: Kernel Directory Interface

Converting tm to time_tConverting tm to time_tConverting tm to time_tConverting tm to time_t

time_t mktime (struct tm *tptr);

operatingsystems

Page 29: Kernel Directory Interface

generating time stringsgenerating time stringsgenerating time stringsgenerating time strings

char *asctime (const struct tm *tmptr);

char *ctime (const time_t *tptr);

both generate a 26 character string of the form

Tue Sep 26 16:49:05 2002\n\0

Takes time zone into account

operatingsystems

Page 30: Kernel Directory Interface

Generating a formatted Generating a formatted stringstring

Generating a formatted Generating a formatted stringstring

size_t strftime (char *tbuf, size_t maxsize, const char *format, const struct tm *tptr);

returns the size of the formatted string if successful0 if fails

a broken down time

the buffer to store the string in, and its max size

the format string – works like printf

operatingsystems

Page 31: Kernel Directory Interface

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

Page 32: Kernel Directory Interface

Getting the Directory Getting the Directory NameName

Getting the Directory Getting the Directory NameName

#include <unistd.h>

extern char *getcwd (char *buf, size_t size);

size is max size of the path name that can be returned.

operatingsystems

Page 33: Kernel Directory Interface

Project #2Project #2du commanddu commandProject #2Project #2

du commanddu commanddu command with no arguments

operatingsystems

Your du command should use the current working directory.

Print out the number of bytes in the directory entry itself.

For each file in the directory, print out the size of the file in bytes, followed by the file name.

For each sub-directory in the directory, print out the number of bytes in the directory entry, then open the directory and repeat this process for each entry in that sub-directory. Repeat this recursively until everything in the directory has been handled.

Page 34: Kernel Directory Interface

Project #2Project #2du commanddu commandProject #2Project #2

du commanddu commanddu command with a filename

operatingsystems

print out the size of the file in bytes, followed by the file name.

Page 35: Kernel Directory Interface

Project #2Project #2du commanddu commandProject #2Project #2

du commanddu commanddu command with a directory name

operatingsystems

Print out the number of bytes in the directory entry itself.

For each file in the directory, print out the size ofthe file in bytes, followed by the file name.

For each sub-directory in the directory, print out the numberof bytes in the directory entry, then open the directory andrepeat this process for each entry in that sub-directory. Continue to repeat this recursively until everything in the directory has been handled.

Page 36: Kernel Directory Interface

Project #2Project #2du commanddu commandProject #2Project #2

du commanddu commandHints

operatingsystems

To get the full pathname of the current directory,use the getcwd( ) function.

Use the d_type field in the dirent structure to getthe type of file (a regular file or a directory)

Use the d_name field in the dirent structure to getthe name of the file

Read chapter 3 in Molay for more ideas.


Recommended