File System Interface

 

 

File Attributes

 

·       Name – only information kept in human-readable form

·       Identifier – unique tag (number) identifies file within file system

·       Type – needed for systems that support different types

·       Location – pointer to file location on device

·       Size – current file size

·       Protection – controls who can do reading, writing, executing

·       Time, date & user identification

 

Information about files are kept in the directory structure, which is maintained on the disk

 

File Operations

 

·       Create

·       Write

·       Read

·       Reposition within file

·       Delete

·       Truncate

 

 

File Locking

 

·       Mandatory – access is denied depending on locks held.

·       Advisory – processes can find status of locks and decide what to do.

 

Lock and Unlock Functions:

 

read_lock(int fd)
{                      
      fcntl(fd, F_SETLKW, file_lock(F_RDLCK, SEEK_SET));         
}

write_lock(int fd)
{                      
      fcntl(fd, F_SETLKW, file_lock(F_WRLCK, SEEK_SET);
}

unlock(int fd)
{                      
      fcntl(fd, F_SETLKW, file_lock(F_UNLCK, SEEK_SET);          
}

struct flock   *
file_lock(short type, short whence)
{
      static struct flock ret;
      ret.l_type = type;
      ret.l_start = 0;
      ret.l_whence = whence;
      ret.l_len = 0;  /* means until end of file */
      ret.l_pid = getpid();
      return &ret;
}

 

 

Example: LockingExampleRead.c

 

int
main(int argc, char **argv)
{
      int             fd;
      fd = open(argv[1], O_RDONLY);      
      read_lock(fd);

 

printf("obtained read lock\n");

      sleep(30);


      unlock(fd);
}

 

Example: LockingExampleWrite.c

 

int
main(int argc, char **argv)
{
      int             fd;
      fd = open(argv[1], O_WRONLY);      
      write_lock(fd);

printf("obtained write lock\n");

      sleep(30);


      unlock(fd);
}

 

Example usage: On 4 windows:

 

 

% LockingExampleRead lockfile.txt       % LockingExampleRead lockfile.txt

 

% LockingExampleWrite lockfile.txt      % LockingExampleWrite lockfile.txt

NOTE: When a process is terminated, all file locks are released.

 

Example: wait4allforkFileLock.c

 

Example usage: 

 

% wait4allforkFileLock 10 10

% wait4allforkFileLock 10 10 NC

 

 

File Types – Name, Extension

 

 

 

 

Access Methods

 

Sequential Access

                                                                   read next

                                                                   write next

                                                                   reset

                                                                  

Direct Access

                                                                   read n

                                                                   write n

                                                                   position to n

                                                                   read next

                                                                   write next

                                                                  

 

          n = relative block number

 

 

Example: fileseek.c

 

int
main(int argc, char **argv)
{
   int  fd, length;
   off_t FileLength, position;
   char buf[1024];
   fd = open(argv[1], O_RDONLY);

   position = atoi(argv[2]);
   length = atoi(argv[3]);
   FileLength = lseek(fd, 0, SEEK_END);
   printf("File Length %d\n", FileLength);
   if ((position+length) > FileLength){
     printf("exceeds file limit\n");
     exit(0);
   }
   lseek(fd, position, SEEK_SET);
   read(fd, buf, length);
   write(1, buf, length);
}

 

 

 


 

 

Typical File-system Organization

 

 

 

Operations Performed on Directory

 

·       Search for a file

·       Create a file

·       Delete a file

·       List a directory

·       Rename a file

·       Traverse the file system

 

 

Single-Level Directory

 

 

Two-Level Directory

 

 

Tree-Structured Directories

 

 

 

Acyclic-Graph Directories

 

 

General Graph Directory

 

 

Example:

>touch  testfile1

>ls  -l

-rw-------  1 cs471w cs471w    0 2011-04-08 11:46 testfile1

 

>ln   testfile1   testfile1-link1

 

>ls   -l

-rw-------  2 cs471w cs471w    0 2011-04-08 11:46 testfile1

-rw-------  2 cs471w cs471w    0 2011-04-08 11:46 testfile1-link1

 

>ln testfile1  testfile1-link2

 

>ls -l

-rw-------  3 cs471w cs471w    0 2011-04-08 11:46 testfile1

-rw-------  3 cs471w cs471w    0 2011-04-08 11:46 testfile1-link1

-rw-------  3 cs471w cs471w    0 2011-04-08 11:46 testfile1-link2

 

>ln -s /home/cs471w symbolic-link

 

>ls -l

lrwxrwxrwx  1 cs471w cs471w   12 2011-04-08 11:50 symbolic-link -> /home/cs471w

 

File System Mounting:

 

 

 

 

File Sharing

 

·       Sharing of files on multi-user systems is desirable

·       Sharing may be done through a protection scheme

·       On distributed systems, files may be shared across a network

·       Network File System (NFS) is a common distributed file-sharing method

 

File Sharing – Multiple Users

 

·        User IDs  identify users, allowing permissions and protections to be per-user

·        Group IDs  allow users to be in groups, permitting group access rights

 

File Sharing – Remote File Systems

 

·        Uses networking to allow file system access between systems

o   Manually via programs like FTP

o   Automatically, seamlessly using distributed file systems

o   Semi automatically via the world wide web

 

·        Client-server model allows clients to mount remote file systems from servers

o   Server can serve multiple clients

o   NFS is standard UNIX client-server file sharing protocol

o   Standard operating system file calls are translated into remote calls

 

·        Remote file systems add new failure modes, due to network failure, server failure

 

·        Unix file system (UFS) implements:

Writes to an open file is visible immediately to other users of the same open file

Sharing file pointer to allow multiple users to read and write concurrently

 

 

Protection

 

·        File owner/creator should be able to control:

o   what can be done

o   by whom

·        Types of access

o   Read

o   Write

o   Execute

o   Append

o   Delete

o   List

 

 

Example:   getfacl & setfacl

 

% cd
% pwd
/home/wahab
% hostname
atria
% uname
Linux
% cd /tmp
% touch testfile
% /bin/ls -lt testfile
-rw-r----- 1 wahab faculty 0 Apr 15 12:01 testfile

% getfacl testfile
# file: testfile
# owner: wahab
# group: faculty
user::rw-
group::r--
other::---

% setfacl -m u:cs471w:rwx

% getfacl testfile
# file: testfile
# owner: wahab
# group: faculty
user::rw-
user:cs471w:rwx
group::r--
mask::rwx
other::---

/bin/ls -lt testfile
-rw-rwx---+ 1 wahab faculty 0 Apr 15 12:01 testfile

 

 

 

Example:   groups  & chgrp

 

 % groups
faculty cs778 cs779 cs471w tcpdump cs476
 

% chgrp cs471w testfile
/bin/ls -lt testfile
-rw-rwx---+ 1 wahab cs471w 0 Apr 15 12:01 testfile

% getfacl testfile
# file: testfile
# owner: wahab
# group: cs471w
user::rw-
user:cs471w:rwx
group::r--
mask::rwx
other::---
 

 

Example:  listfiles

int
main(int argc, char **argv)
{
   DIR            *dp;
   char           *dirname;
   struct stat     st;
   struct dirent  *d;
   char            filename[1024];

   dirname = argv[1];

   if ((dp = opendir(dirname)) == NULL) {
      perror(dirname);
      exit(-1);
   }

   while ((d = readdir(dp)) != NULL) {
      sprintf(filename, "%s/%s", dirname, d->d_name);
      if (lstat(filename, &st) < 0) {
         perror(filename);
         continue;
      }
      printf("%u", st.st_ino);
      printf("%5d ", st.st_blocks);
      printf("%3d ", st.st_nlink);
      printf("%5d/%-5d ", st.st_uid, st.st_gid);
      printf("%.12s ", ctime(&st.st_mtime) + 4);
      printf("%s\n", filename);
   }
}