File properties

Download Report

Transcript File properties

File System Implementation
A possible file system layout
1.1
Implementing Files
Storing a file as a linked list of disk blocks
1.2
Index Node (inode)
 Each file of whatever type, stored in a disk partition, is
allocated a number (called its inode number) which is
actually the index number of an entry in an array stored on
the disk.
 Each element of the array is an inode which stores the
administrative information about a single file (such as, when
it was created, who owns it and where the data blocks for
this file are stored on the disk partition). This information can
be displayed by using stat:
 $ stat file1
File: `file1'
Size: 123 Blocks: 8 IO Block: 4096
Regular File
Device: 306h/774d
Inode: 32787
Links: 2
Access:(0644/-rw-r--r--)Uid:(501/rinaldi)Gid:(501/docenti)
Access: 2003-03-31 12:58:37.000000000 +0200
Modify: 2003-03-31 12:58:25.000000000 +0200
Change: 2003-04-04 19:05:14.000000000 +0200
1.3
Implementing Files
An example i-node: it contains only the first 12 addresses of
the blocks of the file.
1.4
A UNIX i-node
1.5
Index Node (inode)
 It is the inode number of a file that is stored in a
directory alongside the file's name. So,
essentially, directories are just tables that
associate file names with inode numbers. Each
file name and inode number pair in a directory is
called a link.
games
mail
inode
news
work
inode
inode
inode
1.6
Linking Files
 A link is a pointer to another file. Remember that a directory is
nothing more than a list of the names and i-numbers of files.
 A directory entry can be a hard link, in which the i-number points
directly to another file. When a hard link is made, then the inumbers of two different directory file entries point to the same
inode.
 ln utility makes link between files.
 $
cat > file
Hello ! ^D
$ ls -l file
-rw-rw-r-- 1 rinaldi rinaldi 8 apr 7 14:22
$ ln file fileln
$ ls -li file*
12004 -rw-rw-r-- 2 rinaldi rinaldi 8 apr 7
12004 -rw-rw-r-- 2 rinaldi rinaldi
8 apr 7
$ rm file
$ ls -l fileln
-rw-rw-r-- 1 rinaldi rinaldi 8 apr 7 14:22
1.7
file
14:22 file
14:22 fileln
fileln
Linking Files
 If the last argument is the name of a directory, then
the hard link is made from the directory to all the
specified filenames
$ mkdir ~/didattica/corsi
$ ln file* ~/didattica/corsi
$ ls -l
-rw-rw-r-- 2 rinaldi rinaldi 8 apr
$ cd
$ rm fileln
$ ls -l ~/didattica/corsi/
-rw-rw-r-- 1 rinaldi rinaldi 8 apr
7 14:22 fileln
7 14:22 fileln
 A hard link may not be created from a file on one file
system to a file on a different file system. To get
around this problem, create a symbolic link instead.
1.8
Linking Files
 By default, ln makes hard links, with the “-s” option, it
makes symbolic (or "soft") links.
 $ ln -s /usr/include/stdio.h stdio.h
$ ls -l stdio.h
lrwxrwxrwx 1 rinaldi rinaldi 20 apr
stdio.h -> /usr/include/stdio.h
7 14:46
 $ ln -s ~/didattica/corsi/fileln file
$ ls -l file
lrwxrwxrwx 1 rinaldi rinaldi 37 apr 7 14:50
file -> /home/rinaldi/didattica/corsi/fileln
$ rm ~/didattica/corsi/fileln
$ ls –l file
lrwxrwxrwx
1 rinaldi rinaldi 37 apr 7 14:50
file -> /home/rinaldi/didattica/corsi/fileln
$ cat fileln
cat: fileln: No such file or directory
1.9
File Attributes
 The time stamp: each file has three dates associated with it:
 The creation time
 The last modification time
 The last access time
 The owner: every file is owned by one user of the system
 The group: every file has also a group of users associated
with it. The most common group for user files is called
users which is usually shared by all the users account on
the system
 The permissions: every file has permissions associated
with it which tell who can access to that file, or change it, or
in the case of programs, execute it. Each of this permission
can be toggled separately for the owner, the group and all
the other users.
1.10
File Attributes
 The information of the files can be obtained by
means of the command ls –l
$ ls -l
totale 28
drwxrwxr-x
drwxrwxr-x
drwxr-xr-x
-rwx------rw-r--r-drwxrwxr-x
drwx------
4
2
2
1
1
2
2
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
4096
4096
4096
1999
123
4096
4096
mar
mar
mar
mar
mar
mar
mar
27
21
14
31
31
21
14
16:52
17:35
19:21
13:25
12:58
17:35
18:50
Desktop/
didattica/
Documents/
file*
file1
ricerca/
tmp/
 In addition to the name of each file, print the file type,
permissions, number of hard links, owner name, group
name, size in bytes, and timestamp, normally the
modification time.
1.11
Permissions
 Everything in UNIX is managed via files, even access to
devices (via the device files in '/dev') like printers, modems,
sound cards etc. If you do not have the permission on a file
which represents a device, you can't use that device.
 The first concept of permissions is ownership. You either
own a file or a directory you have created or that has been
handed over to you by using the chown command. If you
own a file, you can do whatever you want with it, usually.
 Often more than one account needs access to a file or
device. Changing the ownership of this file every time
someone else needs access to it would be very tedious to
say the least. This is why ownership is not only provided for
single accounts but also for groups. Groups are defined in
/etc/group, an entry in this file looks like this:
 group_name:x:group_ID:group_member
1.12
Permissions
 To see a list of groups your current account is a member of,
use the command
 $ groups
 So, if you are member of a group and that group has
permissions on a file, you can do something with that file,
even if you are not its owner.
 An average UNIX system has some 60.000 files and more.
A majority of them has to be accessible in some way by all
the users on a system. One way to achieve this could be to
create a 'catch-all' group which would unite all the accounts
on a system and assign all these files to this group.
 So, we can divide file permissions into 3 categories:
 Owner permissions
 Group permissions
 Other permissions
1.13
Permissions
 The output of the ls -l command shows the permissions
of a file/directory
$ ls -l
drwxrwxr-x
drwxr-xr-x
-rwx------rw-r--r-drwxrwxr-x
drwx------
4
2
1
1
2
2
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
rinaldi
4096
4096
1999
123
4096
4096
mar
mar
mar
mar
mar
mar
27
14
31
31
21
14
16:52
19:21
13:25
12:58
17:35
18:50
Desktop/
Documents/
file*
file1
ricerca/
tmp/
the flag 'r' is for reading, 'w' for writing, and 'x' is for
executing.
 The first field has three options, d, l or -, where d indicates a
directory, l a symbolic link, and - a file. The other nine fields
indicate the owner permissions, group permissions and 'all
others' permissions.
 Owner | Group | Others = rw- | r-- | r-1.14
( file1)
Permissions on directories
 Permissions
on directories are different from
permissions on files. Here:
drwxrwxr-x 4 rinaldi rinaldi
drwxr-xr-x 2 rinaldi rinaldi
drwx------ 2 rinaldi rinaldi
4096 mar 27 16:52 Desktop/
4096 mar 14 19:21 Documents/
4096 mar 14 18:50 tmp/
 r indicates that the contents of the directory can be listed
 w indicates that the contents of the directory can be
changed (copy, move, create new files)
 x indicates that users can "cd" to that directory, read,
write, execute programs and search in the directory.
1.15
Changing permission settings
 The command, chmod lets you change the permission
mode settings of a file/directory. You also need to specify 3
digits with the chmod command. These digits dictate what
permissions will be applied. The first digit determines the
permissions for the owner, the second digit determines the
permissions for the group, and the third for others.
 To change the permissions of the file “file1” type:
 chmod XYZ file1
where XYZ represents 3 digits e.g. 666
 The first digit is the sum of 4 for read permission, 2 for write
permission and 1 for execute permissions.
 chmod 640 file1
 chmod 754 file1
 chmod 664 file1
user
rwrwx
rw1.16
group
r-r-x
rw-
others
--r-r--
Changing permission settings
Who
Action
Permission
u = user
g = group
o = other
a = all
+ = add
- = delete
= = assign
r = read
w = write
x = execute
 chmod a+rw hello.c
 chmod o-w hello.c
 chmod og+rx prog*
 chmod g=rw rep*
 chmod +w *
 The option -R changes recursively the permissions of a
directory and its contents
1.17
Changing Groups
 If you are member of several groups and you create a file,
what is the group id of the file ? Although you may be a
member of several groups, only one of them is your
effective group at any given time. When a process creates
a file, the group id of the file is set to the effective group id.
 The system administrator chooses which one of your
groups is used as your login shell’s effective group id.
However, you may create a shell with a different effective
group id by using newgrp utility
$ date > ~/test1 … creates “test1” from a “rinaldi” group shell
$ newgroup informatica
$ date > ~/test2 … creates “test2” from a “informatica” group
shell
^D
$ ls –lg test*
-rw-r--r-- 1 rinaldi
-rw-r--r-- 1 informatica
30 mar 27 16:52 test1
30 mar 27 16:52 test1
1.18
The Linux Proc File System
 The proc file system does not store data, rather, it creates a directory
for each process. The name of the directory is the PID of the process
 $ cd /proc ; ls
1/
$ ps
PID
1839
2289
1023/
1089/
TTY
pst/2
pst/2
1111/
… 1839/ … 2289/ …
TIME
00.00.00
00.00.00
CMD
bash
ps
 A proc directory collects the appropriate information about the process
(command line, environ strings, status, …)
 $ cd 1839 ; ls
cmline
status
cwd
environ
exe
fd
maps
mem
mounts stat
statm
 So, proc directories contain many information about the CPU, disk
partitions, devices, ... The user programs can obtain these information
for learning many information about the behavior of the system in a
safe way.
 pstree: shows the depencies between all the active processes
 kill: kills a process
1.19
Mounting
 The operating system attaches a physical file system to its
virtual file system by means of the command mount. This
operation is called mounting and its format is:
 $ mount –t
type device mount-point
 Examples: $ mount –t ext2 /dev/hda6 /
The partition of ext2 is called hda6. (hd for hard drive, a for the first of
these, and 6 for the sixth partition). The documentation of the devices is
in /usr/src/linux/Documentation/devices.txt.
 $ mount –t vfat /dev/hda1 /mnt/windows
 $ mount –t msdos /dev/fd0 /mnt/floppy
 $ mount –t iso9660 /dev/hdc /mnt/cdrom
 The command mount is used by the super-user. The
operating system performs mount –a during the boot. This
command mounts the file systems listed in /etc/fstab. We
can establish the mounted devices by typing in: $ mount
 We can unmount the file system by using umount
 umount /mnt/floppy
1.20
Compressing Files
 The compressed file types in Linux are:
 file.Z -- compressed with the compress:
$ compress file1
$ uncompress file1.Z
 file.z, file.gz -- compressed with the GNU zip program: zip, gzip.
$ gzip –-best huge_file
$ gunzip huge_file.gz
 file.bz2 -- compressed with bzip2
$ bzip2 –9 huge_file
$ bunzip2 huge_file.bz2
 We can view a file in a compressed archive without
extracting it, by means of zcat or bzcat
 $ zcat huge_file.gz
 $ bzcat huge_file.bz2 1.21
Archiving Files
 We can use tar to store files in an archive, to extract them
from an archive, and to do other types of manipulation. The
synopsis of tar is:
 $ tar options tarfilename filelists
 Some options are
 -c create an archive
 -x extract from an archive
 -t list the contents of an archive
 -v encourage verbose output
 -u update only files that are more recent than those archived
 -f use the “tarfilename” as filename of the archive (the name of
“tarfilename” is /dev/rmt0 by default)
 Example:




tar
tar
tar
tar
-cvf logfile.tar logs.*
-tvf logfile.tar
-xvf logfile.tar
-cvzf logfile.tar.gz logs.*
1.22