The Unix File System

The Unix File System

Operating Systems - Spring 2009 Assignment 3: The Unix File System Karl Marklund <[email protected]> A file is a collection Almost all of data or information information. that has stored in a a name, called the computer must filename. 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 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 ls 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 operating system 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 system call. Don’t forget to use the #include <stdio.h> address-of operator & to get #include <sys/types.h> the pointer to buffer. #include <sys/stat.h> 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 <pwd.h> 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 symbolic link. 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); }.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    21 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us