CSci 4061 Introduction to Operating Systems
File Systems: Basics
File as Abstraction
• Container for related information • Named
• Associated attributes
Naming a File
creat/open (“path/name”, …);
Links: files with multiple names File Attributes: Access to metadata Exercise: Metadata Storing File Meta-data: Unix inode Filesystem Filesystem (cont’d) • On-disk organization Unix file types/modes File types Another look at ls -l Example: Filesystem semantics: Unix Filesystem semantics (cont’d) File permissions File permissions (cont’d) File permissions (cont’d) Power of IDs Power of IDs (cont’d) Masks Masks (cont’d) Memory-mapped files: discussion Mmap: discussion
Each name is an alias
#include
int link (const char *original_path,
const char *new_path)
cannot exist as a file already
link (“foo”, “bar”); // “bar” refers to file “foo” unlink (“bar”); // remove name “bar”
// if file is open by someone, will not actually get deleted until // all fd’s to it are closed
#include
int fstat (int filedes, struct stat *buf)
int stat (const char *pathname,
struct stat *buf)
Structure contains file/directory info:
off_t st_size; // file size
nlink_t st_nlink; // links
mode_t st_mode; // type + permission time_t st_mtime; // last modification time
fcntl can also be used to set or get lower-level attrs
• Write a program that monitors a given file every minute and if the file size has changed, it outputs the new size to stdout
#include
int stat (const char *pathname,
struct stat *buf)
Structure contains file/directory level info: off_t st_size; // file size nlink_t st_nlink; // links
…
inode disk sector address
“map” for a file on-disk in-memory
ls –i
• Directory is a file as well • it has an inode
• what are file contents?
• list of file_name, inode pairs
• Filesystem • Files
• Directories
• Free disk sectors • Root dir
• inode for root dir of filesystem “/” stored in well-known sector on the disk
• inode for disk sector free-list also stored in a well-known sector on the disk
• These are stored in the superblock
• Indicated by the first character in ls –l • – regular file
• d directory
• c character special file
• b block special file • p pipe
• s socket
• l symbolic link
• Within stat structure: struct_t stat st; stat (“foo”, &st);
Macros:
int S_ISDIR (st.st_mode); int S_ISREG (st.st_mode); int S_ISSOCK (st.st_mode); …
P. 158
drwx-xr-x 3 jon fac 4066 Nov 2 09:14 st
file type # hard allocation links size
• Two processes open the same file
• Reader sees most recent write
• One reader and one writer – run together
• File “foo” contains “aaaaaaaaaaaaaaaaaaaaaa”
// reader.c
#include
#include
void main () {
int fd;
// writer.c
#include
#include
void main () { int fd;
}
char c, buf[100];
read (0, &c, 1);
fd = open (“foo”, O_RDONLY); n = read (fd, buf, 10); buf[n] = ‘\0’;
printf (“buf=%s\n”, buf);
…
char c, buf[100] = “bbbbbbbbbb…”;
read (0, &c, 1);
fd = open (“foo”, O_WRONLY);
write (fd, buf, 10);
read (0, &c, 1);
close (fd); }
What is the output for reader? Writer does write
Reader then does read
Does machine matter?
Operations (r, w, x): read, write, execute
Subjects (u: user/owner, g: group, o: others)
Users may belong to any number of groups (type groups at the shell)
shell> ls –l
drwxr-xr-w 3 jon fac 46
u g o owner group
When a file is created it is given a restricted
permission and a default group
You can broader or further restrict permissions
#include
#include
int chmod (char *path, mode_t mode); prefer symbolic flags: man fstat, e.g. S_IRGRP : GRouP has Read)
Also at the command-line (absolute)
chmod 0077 st.txt
Also at the command-line (relative)
chmod go-xr st.txt chmod u+xrw st.txt
0077
each octal digit is rwx (or 1) for ugo 000, 111, 111
• Real user-id: user that actually initiated a process
• Not executable owner!
-r-x … 1 jon fac 203 Feb 10 10:47 test Bill> /usr/jon/fac
• Effective user-id: user that system associates with the process for purposes of protection
• Usually the same as the real user-id: this would be?
• Sometimes want effective user-id to that of the file owner and not the user … why?
• How do to it?
shell> chmod u+s my_file
-r-s… 1 jon fac 203 Feb 10 10:47 test
Bill> /usr/jon/test has priviledge of ‘jon’
shell> chmod g+s
Has priviledge of group ‘fac’
creat (“my_file”, 0777);
• Expectation:
• shell> ls –l
• -rwxrwxrwx jon • Instead:
• -rw——– …. • What happened?
… my_file … my_file
• To prevent against accidental exposure, Unix sets a default mask with your process (type umask)
• Typically: 077 (1 means mask out)
creat (“name”, PERM & (~mask)); AND regular files also mask out execute
creat (“name”, PERM & (~mask)); umask is 022: what is this one?
To change the mask:
mode_t umask (mode_t newmask);
• Map file into memory
#include
void *mmap (void *start, // ignore, 0
size_t length, // how much to map
int prot,
int flags,
int fd,
off_t offset)
• Return memory address of file data
• Can use normal memory operations to read/write data
// access type
// the file
// starting where
fildes = open(…) lseek(fildes, some_offset) read(fildes, buf, len)
becomes:
fildes = open(…)
address = mmap (0, len, PROT_READ,
MAP_PRIVATE, fildes,
some_offset) /* Use data at address. */
Advantage?