3 Linux Systems Programming m3 Slides
3 Linux Systems Programming m3 Slides
Chris Brown
In This Module
foo 5 4
5
bar 3 inode table Here is the data
Link
Examining File Attributes
S_ISUID
S_ISGID
S_ISVTX
S_IRUSR
S_IWUSR
S_IXUSR
S_IRGRP
S_IWGRP
S_IXGRP
S_IROTH
S_IWOTH
S_IXOTH
S_ISREG
S_ISDIR
S_ISCHR if (statbuf.st_mode & S_IWOTH)
S_ISBLK printf("file is world writeable");
S_ISFIFO
S_ISLNK if (S_ISREG(statbuf.st_mode))
S_ISSOCK printf("regular file");
Creating Files
Includes O_CREAT
creat(name, mode)
Creating and Removing Links
link(oldname, newname)
Gives the file an additional name
unlink(name)
If this is the last remaining link
the file is deleted
Deletion is postponed if a
process has the file open
Creating and Removing Links
Directory 1
open("foo", O_CREAT, 0644);
2
3
link("foo", "bar");
foo 3 4
5 unlink("foo");
bar 3 Inode
table
Creating and Removing Links
Directory 1
open("foo", O_CREAT, 0644);
2
3
link("foo", "bar");
4
5 unlink("foo");
bar 3 Inode
table
Symbolic Links
symlink(oldname, newname)
unlink(name)
Removes the symlink, not the target file
Hard Links vs. Symbolic Links
inode Another
Name Name
number name
To Follow or Not to Follow
Some system calls follow symbolic links:
Some don't
unlink()
Interacting with Directories
getcwd(buf, size)
Result here
The Current Directory
chdir(pathname)
Creating and Deleting Directories
mkdir(name, mode)
This will only create one directory
mkdir("a/b/c", 0755) will fail unless a, b exist
rmdir(name)
Fails unless the directory is empty
Reading Directories
d = opendir(dirname)
struct dirent
loop
d_ino /* inode number */
d_type /* file type */
d_name /* file name */
info = readdir(d)
closedir(d)
Directory Traversal Example
/* Add up sizes of all files in the current directory */
#include <stdio.h>
#include <sys/stat.h>
#include <dirent.h>
void main()
{
DIR *d;
struct dirent *info; /* A directory entry */
struct stat sb; /* The stat buffer */
long total = 0; /* Total of file sizes */
Directory Traversal Example
d = opendir(".");
closedir(d);
import os
total = 0
I've been
I've been modified!
created!
I've been
I've been deleted!
closed!
fd = inotify_init()
Watch
Path name of file or
descriptors
directory to be watched
Reading Events
wd
File descriptor mask
struct
from inotify_init() cookie inotify_event
len
name
n = read(fd, buf, size)
n padding len
The Environment
Time