The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo...
Transcript of The Linux File System - ariel.ac.il · The /proc File System – Example 1 $ cat /proc/cpuinfo...
The Linux File System
Everything is a file
• In Linux systems almost everything is a file.
– Regular Files
– Directories
– Hardware devices
• Why is it good?
Directories Structure
• The root directory - / contains all other directories in the system.
• The home directory - /home subdirectory of root and contains all users’ files.
– In the shell, the notation ~ getting straight to your home directory.
Special Directories
• /dev – file that represent physical devices and provide the interface to those devices.
• /bin – system’s programs (binaries).
• /lib – system’s libraries.
/dev files
• /dev/console – This device represents the system console.
• /dev/tty – The special file for the controlling terminal.
• /dev/null – This file is a null device.
Empty file
• With the command cp, /dev/null file can used as source to empty file.
>cp /dev/null <filename>
• Another way to creating empty files is to use the touch command.
>touch <filename>
- Example /dev/tty
#include <stdio.h>
int main() { FILE *input; FILE *output; int num,success; input = fopen("/dev/tty", "r"); output = fopen("/dev/tty", "w"); fprintf(output,"Enter a number:\n"); success = fscanf(input,"%d",&num); if(success) { fprintf(output,"Your number is: %d\n",num); } else { fprintf(stderr,"It isn't a number.\n"); }
}
Main File System Calls
• open: Open a file or device.
• read: Read from an open file or device.
• write: Write to a file or device.
• close: Close the file or device.
• ioctl: Pass control information to a device driver.
Library Functions – why?
• system calls are best avoided:
– Expensive performance.
– Limitations.
Library Functions
• collections of functions for user programs.
– stdio.h – for standard I/O
Low-Level File System Calls
Low-Level File Access
• Three descriptors are open automatically when each program starts:
– 0: Standard input
– 1: Standard output
– 2: Standard error
write()
size_t write(int fildes, const void *buf, size_t nbytes)
• fildes – file descriptor number. • buf – buffer to write from. • nbytes – number of bytes to write from the
buffer.
• Returns the number of bytes actually written or -1 on error.
read()
size_t read(int fildes, void *buf, size_t nbytes)
• fildes – file descriptor number to read from.
• buf – buffer to write the data.
• nbytes – number of bytes to read from the file.
• Returns the number of bytes actually read or -1 on error.
open()
int open(const char *path, int oflags) int open(const char *path, int oflags, mode_t mode) Establishes an access path to a file or device. • path – Name of the file or device. • oflags – specifying actions to be taken on opening the file
(read, write, create etc...) • mode – specifying file Initial Permissions.
• Returns a file descriptor that can be used in read, write and
other system calls or -1 on error.
close()
int close(int fildes)
Terminate the association between a file descriptor and its file.
• fildes – file descriptor.
• Returns the 0 on success or -1 on error.
ioctl()
int ioctl(int fildes, int cmd, ...)
provides an interface for controlling the behavior of devices and their descriptors and configuring underlying services.
• fildes – object descriptor number.
• cmd – functions\command to perform
Example 1 - A File Copy Program
#include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main() { char c; int in, out; in = open(“file.in”, O_RDONLY); out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); while(read(in,&c,1) == 1) write(out,&c,1); exit(0); }
Example 2 - A Second File Copy Program
#include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main() { char block[1024]; int in, out; int nread; in = open(“file.in”, O_RDONLY); out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); while((nread = read(in,block,sizeof(block))) > 0) write(out,block,nread); exit(0); }
ANSI-C I/O library functions
• fopen, fclose • fread, fwrite • fflush • fseek • fgetc, getc, getchar • fputc, putc, putchar • fgets, gets • printf, fprintf, and sprintf • scanf, fscanf, and sscanf
What is the difference between those and the system calls shown
?before
Example 3 - A Third File Copy Program
#include <stdio.h> #include <stdlib.h> int main() { int c; FILE *in, *out; in = fopen(“file.in”,”r”); out = fopen(“file.out”,”w”); while((c = fgetc(in)) != EOF) fputc(c,out); exit(0); }
Directories manipulations
• The library for directories manipulations: dirent.h.
• The struct type, DIR*, is a directory stream.
• Some of directories functions: – opendir, closedir
– readdir
– telldir
– seekdir
– closedir
Example - Directory scanning program
#include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> #include <stdlib.h> void printdir(char *dir, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf; if((dp = opendir(dir)) == NULL) { fprintf(stderr,”cannot open directory: %s\n”, dir); return; { …
Example - Directory scanning program
chdir(dir); while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if(S_ISDIR(statbuf.st_mode)) { if(strcmp(“.”,entry->d_name) == 0 || strcmp(“..”,entry->d_name) == 0) continue; printf(“%*s%s/\n”,depth,”“,entry->d_name); printdir(entry->d_name,depth+4); {
else printf(“%*s%s\n”,depth,”“,entry->d_name); {
chdir(“..”); closedir(dp); {
Example - Directory scanning program
int main()
{
printf(“Directory scan of /home:\n”);
printdir(“/home”,0);
printf(“done.\n”);
exit(0);
}
The /proc File System
• The directory /proc contains many special files that allow higher-level access to driver and kernel information.
The /proc File System – Example 1
$ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 2 model name : Intel(R) Pentium(R) 4 CPU 2.66GHz stepping : 8 cpu MHz : 2665.923 cache size : 512 KB fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 2 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss up bogomips : 5413.47 clflush size : 64
The /proc File System – Example 2
$ cat /proc/net/sockstat
sockets: used 285
TCP: inuse 4 orphan 0 tw 0 alloc 7 mem 1
UDP: inuse 3
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0
fcntl()
#include <fcntl.h> int fcntl(int fildes, int cmd); int fcntl(int fildes, int cmd, long arg); Performing several operations on open file descriptors. (getting/setting flags etc…) • fildes – file descriptor number. • cmd – operation to perform. • arg – argument for the command.
mmap()
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
map files or device into memory.
Example - Using mmap() #include <unistd.h> #include <stdio.h> #include <sys/mman.h> #include <fcntl.h> #include <stdlib.h> typedef struct { int integer; char string[24]; } RECORD; #define NRECORDS (100) int main() }
RECORD record, *mapped; int i, f; FILE *fp; fp = fopen(“records.dat”,”w+”);
Example - Using mmap()
for(i=0; i<NRECORDS; i++) {
record.integer = i;
sprintf(record.string,”RECORD-%d”,i);
fwrite(&record,sizeof(record),1,fp);
}
fclose(fp);
Example - Using mmap()
f = open(“records.dat”,O_RDWR); mapped = (RECORD *)mmap(0, NRECORDS*sizeof(record), PROT_READ|PROT_WRITE, MAP_SHARED, f, 0); mapped[43].integer = 243; sprintf(mapped[43].string,”RECORD-%d”,mapped[43].integer); msync((void *)mapped, NRECORDS*sizeof(record), MS_ASYNC); munmap((void *)mapped, NRECORDS*sizeof(record)); close(f); exit(0); {
Example - Using mmap()
fp = fopen(“records.dat”,”r+”); fseek(fp,43*sizeof(record),SEEK_SET); fread(&record,sizeof(record),1,fp); record.integer = 143; sprintf(record.string,”RECORD-%d”,record.integer); fseek(fp,43*sizeof(record),SEEK_SET); fwrite(&record,sizeof(record),1,fp); fclose(fp);
Files’ Metadata
• A set of data that describes and gives information about the file, stores in file’s i-node. – Access permissions
– Last access timestamp
– Owner
– Group
– Size
– And more…
File permissions
• File permissions - Allowing\disallowing access to the file in order to read (r), write (w) or Execute (x).
• Can give permission to the user, group, or others.
• Example of file permission bits in the bash: >ls -l <filename>
-rwxr-xr--
Extended attributes
• provide a mechanism for associating key/value pairs with files.
• Extended attributes used to insert additional attributes besides the attributes in the file system.
The Current Working Directory
• Every process has a current directory, which it initially inherits from its parent process.
• The current working directory is the starting point from which the kernel resolves relative pathnames.
• Example: if the process tries to open <filename>, the kernel will attempt to open: current directory/<filename>
• Get Current Working Directory:
char * getcwd (char *buf, size_t size);
Links and Hard links
• Link – connection between a file name and the actual data in the disk (i-node).
• Hard Link – a link between many files to the same i-node represents the file.
• Symbolic Link – a link between file and other file name in the file system.
link() system call
int link (const char *oldpath, const char *newpath);
Creates a link (hard link) under the newpath for the existing file oldpath.
Returns 0 on success and -1 on error.
symlink() system call
int symlink (const char *oldpath, const char *newpath);
Creates a link (symbolic link) under the newpath for the existing file oldpath.
Returns 0 on success and -1 on error.
unlink() system call
int unlink (const char *pathname);
Deletes the pathname from the file system.
Returns 0 on success and -1 on error.
File events - inotify
• Inotify – interface for monitoring file events to see when they are moved, read from, written to, or deleted.
The library - #include <sys/inotify.h>
inotify - Example
#include <sys/inotify.h> #include <stdio.h> #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) int main() { int fd,length, i = 0; fd = inotify_init (); int wd; wd = inotify_add_watch (fd, ".", IN_MOVE); char buffer[BUF_LEN]; length = read( fd, buffer, BUF_LEN );
inotify - Example
while ( i < length ) { struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; if (event->len) { if ( event->mask & IN_MOVE ) { printf( "The file %s was moved.\n", event->name ); } } i += EVENT_SIZE + event->len; } (void) inotify_rm_watch( fd, wd ); (void) close( fd ); return 0; }
Self studying
Error Handling:
• char *strerror(int errnum);
• void perror(const char *s);
Self studying
• Stream Errors:
– int ferror(FILE *stream);
– int feof(FILE *stream);
– void clearerr(FILE *stream);
• Streams and File Descriptors :
– int fileno(FILE *stream);
– FILE *fdopen(int fildes, const char *mode);
Self studying
• File and Directory Maintenance:
– int chmod(const char *path, mode_t mode);
– int chown(const char *path, uid_t owner, gid_t group);
– int unlink(const char *path);
– int link(const char *path1, const char *path2);
– int symlink(const char *path1, const char *path2);
Self studying
• mkdir and rmdir:
– int mkdir(const char *path, mode_t mode);
– int rmdir(const char *path);
• chdir and getcwd:
– int chdir(const char *path);
– char *getcwd(char *buf, size_t size);
Self studying
• File state:
– int stat (const char *path, struct stat *buf);
– int fstat (int fd, struct stat *buf);
– int lstat (const char *path, struct stat *buf);
• Permissions:
– int chmod (const char *path, mode_t mode);
– int fchmod (int fd, mode_t mode);