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: LockingExampleRead.java
public
class LockingExampleRead {
public static final boolean SHAREMode = true;
public static void main(String args[]) throws IOException {
FileLock sharedLock = null;
try {
RandomAccessFile raf = new RandomAccessFile(args[0], "rw");
FileChannel channel = raf.getChannel();
sharedLock = channel.lock(0, raf.length(), SHAREMode);
System.out.println("obtained read lock”);
Thread.sleep(30000);
sharedLock.release();
}
}
Example: LockingExampleWrite.java
public class LockingExampleWrite
{
public static final
Boolean SHAREMode = false;
FileLock
exclusiveLock = null;
try {
RandomAccessFile raf = new RandomAccessFile(args[0], "rw");
FileChannel channel = raf.getChannel();
exclusiveLock = channel.lock(0,
raf.length(), SHAREMode);
System.out.println("obtained write lock”);
Thread.sleep(30000);
exclusiveLock.release();
}
}
Example usage: On 4
windows:
% java LockingExampleRead lockfile.txt % LockingExampleRead
lockfile.txt
% java LockingExampleWrite lockfile.txt % LockingExampleWrite
lockfile.txt
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: setfacl, getfacl
[stream]
/tmp>hostname
stream.cs.odu.edu
[stream]
/tmp>uname
Linux
stream:/tmp> ls
-lt testfile
-rw-rw-r--
1 cs471w cs471w 0 Mar 23 09:59 testfile
stream:/tmp> getfacl
testfile
# file:
testfile
# owner:
cs471w
# group:
cs471w
user::rw-
group::rw-
other::r--
stream:/tmp> setfacl
-m u:wahab:w testfile
stream:/tmp> getfacl
testfile
# file:
testfile
# owner:
cs471w
# group:
cs471w
user::rw-
user:wahab:-w-
group::rw-
mask::rw-
other::r--
stream:/tmp> ls
-lt testfile
-rw-rw-r--+
1 cs471w cs471w 0 Mar 23 09:59 testfile
stream:/tmp> setfacl
-m mask:r--
testfile
stream:/tmp> ls
-lt testfile
-rw-r--r--+ 1 cs471w cs471w 0 Mar 23 09:59 testfile
stream:/tmp> getfacl
testfile
# file:
testfile
# owner:
cs471w
# group:
cs471w
user::rw-
user:wahab:-w- #effective:---
group::rw- #effective:r--
mask::r--
other::r--
Example: chgrp
> ls
-lt
testfile
-rw------- 1 wahab faculty 0
2011-04-08 09:27 testfile
> groups
faculty cs476
cs779 tcpdump cs778
cs471w
> chgrp
cs471w testfile
> ls -lt testfile
-rw------- 1 wahab cs471w 0 2011-04-08 09:27
testfile
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);
}
}