Operating Systems - Spring 2009 Assignment 3: The File System

Karl Marklund A file is a collection Almost all of data or information information. that has stored in a a name, called the computer must . be in a file.

Different types of files In Unix a directory is a file store different types of with information on how information. For example, to find other files. program files store programs, whereas text files store text Unix File System

Files and directories are organized in a tree structure – starting at the root.

bin dev etc usr tmp unix boot

you mike paul mary

junk junk temp junk data Unix File System

bin dev etc usr tmp unix boot

Absolute you mike paul mary path to file temp? junk junk temp junk data

/usr/paul/temp Unix File System

bin dev etc usr tmp unix boot

Relative path from you mike paul mary directory paul to file data? junk junk temp junk data

../mary/data How are files A file is represented represented? by exactly one

Inode number

File mode Owner Timestamps inode Size Reference count

Pointer to data

Examples of what we need to store in an inode

A file does not have a name. The file is uniquely identifed by its inode number Data can be ... Or to allow for ... Or double stored in direct more blocks indirect data data blocks... indirect data blocks blocks can be used... In UNIX you can use the command to get information about files

The owner Highes group that the of the file onwer belongs to $> ls –l -rw------1 hans it readme.txt -rwx-r—r-- 1 karl it script.sh

File mode - Name of File or permissions Directory $> ls –l -rw------1 hans it readme.txt -rwxr--r-- 1 karl it script.sh

rwxr--r--

Permissions for the Permissions Permissions for owner: for the group all others. r – read w – write x - execute

If I want to obtain information about files, how can the Unix help me?

There are a number of useful system calls. #include ”sys/types.h” #include ”sys/stat.h”

// Two system calls that can be used to // obtain information about files or // directories. int stat(const char *path, struct *buf); int lstat(const char *path, struct *buf);

// Both stat & lstat returns 0 on sucess // and -1 on failure #include ”sys/types.h” #include ”sys/stat.h”

// Two system calls Relativethat orcan abosolute be used path to to // obtain informationfile orabout directory. files or // directories. int stat(const char *path, struct stat *buf); int lstat(const char *path, struct stat *buf);

// Both stat & lstat returns 0 on sucess // and -1 on failure Must pass a pointer to a stat struct – will hold results of the . Don’t forget to use the #include address-of operator & to get #include the pointer to buffer. #include struct stat buffer; int status; status = stat(”./file.txt”, &buffer); printf(”MODE = %d \n”, (int) buffer.st_mode);

Now the buffer struct will be populated with data.

// Convert the numeric mode to the standrad // string representation such as ”-rw-r—r—” printf(”MODE = %s \n”, strmode(buffer.st_mode));

You will be given the strmode function in the lab. See the man pages for more information

$> man –s2 stat $> ls –l -rw------1 hans it readme.txt -rwxr--r-- 1 karl it script.sh

How can we get the user name of the file owner? We can get the user id....

#include status = stat(”./file.txt”, &buffer); struct passwd *pwd; See man pages for mor details: pwd = getpwuid(buffer.st_uid); $> man getpwuid printf(”USER = %s \n”, pwd->pw_name); Whats the difference between stat and lstat? int stat(const char *path, struct stat *buf); int lstat(const char *path, struct stat *buf);

The lstat() function shall be equivalent to stat(), except when path refers to a .

In that case lstat() shall return information about the link, while stat() shall return information about the file the link references. Whats the difference between stat and lstat? int stat(const char *path, struct stat *buf); int lstat(const char *path, struct stat *buf);

The lstat() function shall be equivalent to stat(), except when path refers to a symbolic link.

In that case lstat() shall return information about the link, while stat() shall return information about the file the link references. $> ls –l lrw------1 karl it link -> file.txt -rw-r--r-- 1 karl it file.txt

link is a symbolic link to file.txt #define MAX_LINK_LEN 20 Use lstat() so we can get info about symbolic links struct stat buffer; lstat(”link”, &buffer);

// Using lstat we can check if a file is a // symbolic link or not: S_ISLNK(m) – macro that tests wheter file with mode m is a symbolic link or not. if (S_ISLNK(buffer.st_mode)) { char link_buffer[MAX_LINK_LEN]; int len = readlink(”link”, link_buffer, readlink() – system call used to MAX_LINK_LEN); get contents of symbolic link.

link_buffer[len] = 0; // Null terminate printf(”link -> %s\n”, link_buffer); }