Unix File System
Unix File System
Display on screen:
$cat hello.cpp
#include <stdio.h>
int main(void)
{
printf("hello world!\n");
}
$
Print it:
$lpr < cat hello.cpp
cat doesn't know the difference between the monitor and printer. It's just writing its
output to the standard place: the standard ouput (stdout). In the first case, the stdout
points to the screen, and in the second case, it has been REDIRECTED to the
standard input (stdin) of the printer.
– Input redirection
• Fully qualified path names begin with root (/) followed by 0 or more
subdirectories
• Also don't use '-' (options), '*' and '?' (wild cards) in filenames
• The inode is the building block of the UNIX file system. They contain all
kinds of information about the directory entry, such as:
• A list of the disk block numbers where data for the file is stored. If the file is
small, direct block numbers are stored. Larger files require the inode to store
block numbers for blocks that contain more block numbers (indirection).
(Fig 1-2)
Do not mix in read()/write() calls with ANSI standard IO calls like fread(), fwrite() etc. for
the same file. This is because the ANSI standard IO calls provide for buffering, and so
the actual read or write from disk may not occur immediately after you make the call. By
contrast read()/write() reads or writes immediately, so mixing the two types of I/O
routines for the same file is a dangerous practice.
lseek() - random access
• off_t lseek(int filedes,off_t offset,int start_flag);
• takes the usual file descriptor
• offset is an signed int measured with respect to start_flag, cast it to
off_t
• start_flag can be 1 of:
SEEK_SET: offset is from begining of file
SEEK_CUR: offset is from current location
SEEK_END: offset is from end of file
File/Directory permissions
-rwxr-xr-x 1 reid 0 Jan 11 22:26 allexec*
-rw-r--r-- 1 reid 0 Jan 11 22:23 allread
-rw------- 1 reid 0 Jan 11 22:23 ownerread
-r--r--r-- 1 reid 0 Jan 11 22:23 readonly
drwxr-xr-x 1 reid 0 Jan 11 22:23 dir-read
drwx--x--x 1 reid 0 Jan 11 22:23 dir-search
• Understanding File Permissions (From the notes of Karen Reid)
• Although the same letters are used to denote permissions for regular files and for
directories, they have slightly different meanings.
• For regular files, r means that the file can be displayed by cat, w means that
it can be changed by an editor, and x means that is can be executed as a
program.
• For example:
• -rwxr-xr-x 1 reid 0 Jan 11 22:26 allexec*
• -rw-r--r-- 1 reid 0 Jan 11 22:23 allread
• -rw------- 1 reid 0 Jan 11 22:23 ownerread
• -r--r--r-- 1 reid 0 Jan 11 22:23 readonly
Everyone can read allread and readonly.
• Everyone can execute allexec.
• Only the owner of ownerread can read and write it.
• If the owner of readonly tries to edit it the editor will not allow the owner to save the changes.
• If anyone tries to execute allread (or ownerread, or readonly) they will get the message
"Permission denied."
A directory is a file that stores a list of filenames along with the inode number of the file. (The
inode is a data structure that stores data about a file and pointers to disk blocks that hold
the file's data.)
Read permission for a directory means that ls can read the list of filenames stored in that
directory. If you remove read permission from a directory, ls won't work.
For example:
% ls -ld testdir
d-wx--x--x 2 reid 512 Jan 11 22:41 testdir/
% ls -l testdir
testdir unreadable
% cat testdir/hidefile
I can still read this file
Note that it is still possible to read and write files inside testdir even though we cannot list its
contents.
Write permission for a directory means that you can create and remove files from that
directory. You should never allow write permission for others on any of your directories. If
you did, any other user could remove all of your files from such a directory.
% ls -l
-
rw-r--r-- 1 reid 0 Jan 11 22:23 allread
dr-xr-xr-x 2 reid 512 Jan 11 22:42 testdir/
% cp allread testdir
cp: cannot create testdir/allread: Permission denied
Changing permissions