CM 03

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Files

Nicolas Magaud
Konul Aliyeva
Files


The file is the most basic and fundamental
abstraction in Linux.
Linux follows the everything-is-a-file
philosophy

2

In UNIX, filenames may or may not have
extensions. An executable file can be named
foo.bar.foo, a text file can be named as
text.exe.

The type of a file can be of text, executable or
data (“binary” or non-printable).

3
man file


There are three sets of tests performed in this
order: file system tests, magic tests, and
language tests.

4
Types of files

By default UNIX have only three types of files.


They are:

Regular files (-)

Directory files (d)

Special files

5
Special files have 5 sub types:

Block file (b)

Character device file (c)

Pipe file (p)

Symbolic link file (l)

Socket file (s)

6
Regular file types

1. Readable file or
2. A binary file or
3. Image file or
4. Compressed file, etc.
Use touch command to create regular files.
To list regular files use ls -l | grep ^ -

7
Directory files

To list directory files use ls -l | grep ^d


Use mkdir to create a directory.

8
Block file type


These files are hardware files, most of them
are present in /dev.

To list them use ls -l | grep ^b

Block devices are for communicating with
storage devices and capable of buffering
output and storing data for later retrieval.
Examples: hard drive, memory.

9
Character device files


To list them use ls -l | grep ^c

Special files allowing the OS to communicate
with input/output devices. Examples:
keyboard, mouse, monitor, audio or graphics
cards.

10
Pipe files


The other name of pipe is a “named” pipe,
which is sometimes called FIFO. FIFO stands
for First In First Out and refers to the property
that the order of bytes going in is the same
coming out.

TO list them use ls -l |grep ^p

To create them use mkfifo

11
Symbolic link files


These are linked files to other files.
They are either Directory/Regular file.
The inode number for this file and its parent
file is same.

12
To list them use ls -l | grep ^l

13
Socket files


A socket file is used to pass information
between applications for communication
purpose

To create them use socket() system call

14
Open() file


Int open(const char *path, int flags)

Int open(const char *path, int flags, mode_t
mode)

This primitive has 2 different signatures (the
first one simply opens a file, the second one
creates a file if needed, hence the mode_t flag
to figure out the permissions).

15

It returns file descriptor (>=3), -1 in case of an
error.

0 is for stdin

1 is for stdout

2 is for stderr

16

Examples:

open(“foo”, O_RDONLY) opens in read-only
mode (the file must exist).

open(“foo”, O_WRONLY | O_CREAT |
O_TRUNC, 0666) creates a newfile (or resets to
0 an existing file) and opens it in writeonly
mode.

open(“foo”, O_RDWR | O_CREAT | O_APPEND,
0666) creates a file (If it didn’t already exist)
and opens it in read/write mode with addition at
the end (append mode).
17
There are 3 standard file descriptors opened
by default(stdin, stdout, stderr). They are
opened by default by shell. It allows input,
output to a terminal however they can also be
redirected to any other file.

18
close()


You must always close the file you don’t need
any more

Int close(int fd)

However, files are closed automatically at the
end of the execution of a process. In the case
of pipes (redirections), closing at the right
time is of paramount importance.

19
Read from a file/ write into a file


ssize_t read(int fd, void *buf, size_t nb)

ssize_t write(int fd, const void *buf, size_t nb)

These functions return number of bytes
transferred (0 when EOF reached, -1 in case
of an error)

20
Move around a file


off_t lseek(int fd, off_t offset, int apartir)

Lseek() allows to modify the offset depending
on the apartir (=from)

SEEK_SET: move to an absolute position

SEEK_CUR: move to the current position

SEEK_END: move from the end of a file

21

Lseek() returns the offset as it as after the modification
Examples:

lseek(fd, 1000000, SEEK_SET) moves to the offset=1
million bytes

lseek(fd, -20, SEEK_CUR) goes back 20 bytes from the
current position.

lseek(fd, 300000, SEEK_END) moves 300 000 bytes after
the current end of a file. Read shall return 0, write can
write new bytes. In this case, the system leaves a hole in
the file, in case of reading in the hole, it’s like there were
300 000 times the byte 0. The size of the file is not the
space used on the disk.

lseek(fd, 0, SEEK_CUR): where am I?
22
System primitives / library functions


There are 2 kinds of files to access a file:

System primitives: open(), close(), read(),
write(), lseek()

Library functions: fopen(), fclose(), getc(),
scanf(), fread(), putc(), printf(), fwrite(), fseek()

23

You might also like