09.2 FileIO FileSystems C
09.2 FileIO FileSystems C
File Systems
●
Can we do anything more than just use data files?
●
How are file systems organized?
●
What are hard/soft links?
25-03-12 2
The Universality of I/O
25-03-12 3
Everything is a File
um
●
UNIX I/O model gives access to many things via files: you
– Actual files! cut these
run
– .. Devices keyboards hard-drives LEDs
fi
or
, , , was
on
opens
-
– Networks ccess
info
to
– Process information
special virtual file system
,
●
/proc File System > -
exists only
in memory
25-03-12 4
dynamically created
by home in RAI
They are
E.g., Terminal
rivIX Ereats firminal line any other
file ,
ruvery running
in Unix
process
●
Universality of file IO: Terminal is automatically
– 3 standard file descriptors that are always open. given
●
These are.. opened by the init process Sanderd
●
25-03-12 5
E.g., Device Files
●
Many devices have a "device file" in /dev/
– This is called a node.
/dev/sda – a hard disk
●
Some are..real devices • /dev/mouse or /dev/input/mice – a mouse
– e.g., a mouse, a disk. • /dev/tty – terminal
●
Some are.. virtual devices anything to
>
- written
– /dev/null provides a "black hole" of all data written to it. it is
-
discardet
– /dev/zero provides infinite null characters.
-
25-03-12 6
E.g., /sys File System
●
File IO in /sys file system
– /sys.. bernet internal information
ows
e.g., various device setups, kernel subsystem info, etc.
●
Examples I
& drivers
devices
-– Controlling LEDs thers
-– Accessing secondary processors expose
settings
- – Communicating to an accelerometer, etc. here
●
ioctl syscall
– Extra syscall for I/O for things
.. outside of the normal universal to model
which you
– E.g., Change the speed of a serial port.
un't do
is
read
using
25-03-12 7
get in BTOP
Proc-All the stuff you
with hardware
sys , den-How you might Internet
Disk Partitions
25-03-12 8
Disk Partitions
A disk is divided into partition
●
..
– /proc/partitions shows the partition info.
=>
In Windows, partitions are C:, D: , etc.
–
●
A partition is typically used as a file system
– A file system is
directores
.. a system that manages &
fles
– Many different types of file systems. ruch has
diff features
– Each partition can have a different file system.
●
E.g.,-
BeagleY-AI board has 2 partitions on its micro-SD card:
– One is Fat32, accessible to Windows and storing
- -
configuration data.
-
–
t
One is EXT4, used by Linux to store rest of the root file
system. -
25-03-12 9
Disk Partitions (cont)
●
User's perspective
– .. File system is file trop
a
management
dish used
-
e.g., .. paying
when RAM
– /proc/swaps
-
shows the swap space info. is full
(Don't always need to have swap space) The OS moves
inactive page from
MAM free up
to
.
memory
This is called
paging
25-03-12 10
I-Nodes
25-03-12 11
I-Nodes
●
A file is associated with an i-node.
– .. An i-node contains metadata about the file
e.g., file-
type, permissions,
-- owner,
- timestamps, etc.
– An - i-node is identified by a number.
ls -li shows i-node numbers (1st column).
-
>
-
●
stat(), lstat(), and fstat()
– Functions that work with file metadata mostly from the i-node.
– Read man 2 stat and man 3 stat for more details.
↓ t
syscalls structs about
mitodate
25-03-12 12
Activity: I-Node
●
Activity: use stat() to display if path is file or directory
– Use command line argument to get filename (arg[1] likely)
– Read man inode, especially about st_mode.
●
Check out S_ISREG(...), and S_ISDIR(...)
– Print "Regular file" if it's a file. #include <stdio.h>
#include <sys/stat.h>
– Print "Directory" if its a directory.
int main(int ardc, char *argv[]) {
– Print "Other" otherwise. char *path = argv[1];
if (S_ISREG(st.st_mode)) { fills
to
the
metadata
~
-
25-03-12 14
Hard Links
●
Hard links
.. we can give many names to the same
file
– A hard
-
link is giving another name to an existing file.
link points
●
Hard link limitations Hard which
– Cannot hard link a directory inode
the
- to
are within one
–
i.e., a child directory that links to the parent directory.
-
-
Hard links should
-
25-03-12 15
Activity: Hard Links
●
[5 min] Activity:
Use ln to create a hard link to a file.
– Read man ln to figure out how to create a hard link.
– Run ls -li for both the original file and the hard link.
(They're exactly the same)
●
ls -li shows the number of links as well (the third column)
●
# links should increase as more hard links are created
●
Modify content of original file
– Check contents of the hard link (and vice versa).
– They should be the same.
25-03-12 16
touch file.txt
~/units/05-lectures/09/link› ls -li
total 0
122647 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
~/units/05-lectures/09/link› ls
file.txt
~/units/05-lectures/09/link› ln file.txt linktofile.txt
~/units/05-lectures/09/link› ls
file.txt linktofile.txt
~/units/05-lectures/09/link› ls -li
total 0
122647 -rw-r--r-- 2 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
122647 -rw-r--r-- 2 cmpt201 cmpt201 0 Apr 13 16:52 linktofile.txt
~/units/05-lectures/09/link› cp file.txt filecopy.txt
~/units/05-lectures/09/link› ls-li
total 0
122651 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:54 filecopy.txt
122647 -rw-r--r-- 2 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
122647 -rw-r--r-- 2 cmpt201 cmpt201 0 Apr 13 16:52 linktofile.txt
~/units/05-lectures/09/link› ln linktofile.txt anotherlinktofile.txt
~/units/05-lectures/09/link› ls -li
total 0
122647 -rw-r--r-- 3 cmpt201 cmpt201 0 Apr 13 16:52 anotherlinktofile.txt
122651 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:54 filecopy.txt
122647 -rw-r--r-- 3 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
122647 -rw-r--r-- 3 cmpt201 cmpt201 0 Apr 13 16:52 linktofile.txt
~/units/05-lectures/09/link› rm linktofile.txt
~/units/05-lectures/09/link› ls -li
total 0
122647 -rw-r--r-- 2 cmpt201 cmpt201 0 Apr 13 16:52 anotherlinktofile.txt
122651 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:54 filecopy.txt
122647 -rw-r--r-- 2 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
~/units/05-lectures/09/link› rm anotherlinktofile.txt
~/units/05-lectures/09/link› ls -li
total 0
122651 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:54 filecopy.txt
122647 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
How rm works (aside)
●
rm only deletes the hard link.
actually unlink
=>
– .. n is
25-03-12 17
Soft Links (Symbolic Links) It is a
speich
C fit the contains
●
Soft links path to
.. also called symbolic link or sym link another
– Unlike a hard link,.. fits
The content of the file is the path to the original file. not its
inoll
-
There's a system call symlink().
–
●
No limitations like hard links
– Sym links are allowed for directories. -
– Sym links do not have to be within the same file system.
-
ws relete
If
mour
or
the main
link it becomes dangling
25-03-12 18
Activity: Soft Links
●
(5 min) Activity
Create a sym link with ln -s
– Run ls -li
●
They each have a unique i-node number, meaning they
are two different files.
-
●
The hard link count does not change even if you create a
-
–
-
The sym link will point to nothing if the original gets deleted.
This is called a dangling link.
●
=>
25-03-12 19
/units/05-lectures/09/link› ls
filecopy.txt file.txt
~/units/05-lectures/09/link› touch softtemp.txt
~/units/05-lectures/09/link› ls
filecopy.txt file.txt softtemp.txt
~/units/05-lectures/09/link› ln -s softtemp.txt softlink.txt
~/units/05-lectures/09/link› ls
filecopy.txt file.txt softlink.txt softtemp.txt
~/units/05-lectures/09/link› ls -li
total 0
122651 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:54 filecopy.txt
122647 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
122652 lrwxrwxrwx 1 cmpt201 cmpt201 12 Apr 13 17:30 softlink.txt -> softtemp.txt
120600 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 17:30 softtemp.txt
~/units/05-lectures/09/link› rm softtemp.txt
~/units/05-lectures/09/link› ls -li
total 0
122651 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:54 filecopy.txt
122647 -rw-r--r-- 1 cmpt201 cmpt201 0 Apr 13 16:52 file.txt
122652 lrwxrwxrwx 1 cmpt201 cmpt201 12 Apr 13 17:30 softlink.txt -> softtemp.txt
Optional:
Bits - setuid, setguid, sticky
X
25-03-12 20
Setuid / Setguid bits
●
Program Permission
– Normally, programs you run will run with your permission.
●
Setuid bit: if set, the user that runs the program can act as
the owner of the program.
– E.g., passwd sets a user's password.
It must write to the password file (/etc/shadow), which is
owned by the root.
– So, use the setuid bit: X
– When a user runs passwd, the program can act as root to
modify the password file.
●
Setgid bit: if set, the user that runs the program can act as if
the user belonged to the group of the program.
25-03-12 21
Sticky Bit
●
Sticky bit:
– Can be set on a shared directory for better control.
– When set, only able to delete/rename file if:
a) you own it
b) you have write permission for it
(It affects the directory, not the file access permissions)
X
25-03-12 22
Sticky Example
●
Situation 1: Regular Directory
– Create a shared_photos/ directory that is write-open for others
(e.g., rw-rw-rw-).
– User dr-evil creates a file selfie.jpg in it.
– User boogieman can delete selfie.jpg.
●
Situation 2: Sticky Bit!
X
– Set sticky bit on shared_photos/
chmod +t shared_photos/l
– User dr-evil creates a file selfie.jpg in it.
– User boogieman cannot delete selfie.jpg.
25-03-12 23
VFS - Virtual File System
and
Mount/Unmount
25-03-12 24
VFS (Virtual File System)
●
VFS (Virtual File System)
.. defines ,
an interface
that different file systems
an
implemens
– Interface includes: open, read, write, close, etc.
VFS in kernel define a function to handle each.
– It's not a file system of real files,
.. It is just software pretending to be a
fil system
●
If a file system implements this interface,
- -
↓
it can be used as a Linux file system. doesn't
It
-
25-03-12 25
Mounting
●
Linux presents all file systems as a single tree
– Starts at root directory /
●
In reality, this single file tree
.. is actually multiple file trees combined together
●
Recall:
– A partition contains a file tree
– There can be multiple partitions on a single disk.
– There can be multiple disks for a single machine.
25-03-12 26
Mounting and Unmounting
●
Mounting
.. Combining multiple file tris into one .
●
mount command mounts a file tree (a file system) to a
E -
specific directory
-
– The mount
-
command also shows the current setup.
(Shows the same information as /proc/mounts).
●
The &
umount command unmounts a file system.
-
25-03-12 27
Summary
●
Everything is a file
– Use file operations to access almost anything.
– /proc for process info
– /dev for devices
– /sys for system info
●
Partitions split up disks
●
I-Nodes used for meta data about each file/directory.
●
Hard/soft links allow two entries for one file.
●
Mounting places one file tree inside another.
25-03-12 28