L5/6: Files and Directories Flashcards

1
Q

Six Main Topics

in the

Systems Programming Class

A

Program Development

Files and Directories

Tool Building

Processes

Networking

OS Implementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Shell Command Equivalents:

mv

A

From stdio.h library:

rename( old_filepath, new_filepath);

Equivalent to:

%mv old new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Shell Command Equivalents:

ln

A

From unistd.h library:

link( old_filepath, new_filepath);

Equivalent to:

%ln old new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Shell Command Equivalents:

rm

A

From unistd.h library:

unlink( “old_file” );

Equivalent to:

%rm old_file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Shell Command Equivalents:

rm -r

A

From stdio.h library:

remove( “old_file” );

Equivalent to:

%rm -r old_file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Shell Command Equivalents:

cp

A

No direct method.

Use read/write File I/O operations

to copy old file to new.

Equivalent to:

%cp old new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Shell Command Equivalents:

cd

A

From unistd.h library:

chdir( “path” );

Equivalent to:

%cd path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Shell Command Equivalents:

mkdir

A

From unistd.h library:

mkdir( “newpath”, 0777);

Note: Not sure what the 0777 flag does

Equivalent to:

%mkdir newpath

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Shell Command Equivalents:

rmdir

A

From unistd.h library:

rmdir( “path”);

Equivalent to:

%rmdir path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

unistd.h

Methods that are equivalent to

common shell commands (5)

A
  • link()
    • equivalent to “ln”
  • unlink()
    • equivalent to “rm”
  • chdir()
    • equivalent to “chdir”
  • mkdir()
    • equivalent to “mkdir”
  • rmdir()
    • equivalent to “rmdir”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Physical Disk Organization:

Partition Sections

A
  • boot and superblocks
  • ilist
    • contains inodes
  • datablocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Physical Disk Organization:

inode structure

A

An inode consists of

  • a status block
  • addresses to multiple data blocks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Physical Disk Organization:

Key Points

A
  • Disk is arranged into blocks
    • some for data
    • some for management
  • Each inode corresponds to one file and the data blocks associated with that file
  • Physical layout is not consecutive, file blocks can be all over the place
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How many data blocks can an inode point to?

A

13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do inodes handle larger file sizes?

A
  • The inode only has 13 pointers, so an indirect approach is used
  • File blocks can each contain many pointers
  • Pointers are classified into levels L0, L1, L2, …
  • This allows a hierarchy of blocks to be created to handle arbitrary file sizes
  • Level L0 is a pointer that points directly to an actual datablock
  • L1 points to a block of L0 pointers, L2 to a block of L1 pointers, and so on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Directory Access Rules (3)

A
  • Search Rule
    • to open a file, the user or program must have execute permission on each directory in the full pathname
  • Write Rule
    • to create or delete a file, must have write access to directory
  • Read Rule
    • to read file names, must have read access on the directory
17
Q

inodes:

Design Consequences

for

Move Operations

A
  • Move speed is independent of the file size
    • data blocks do not have to be moved around
    • only the pointers change(unless moving partitions/drives)
  • Moves are easy to implement as atomic operations
18
Q

Structure of a

Directory

A
  • The directory is a specific type of file
  • Contains a table of inodes paired with filenames
  • inodes are identified with inode numbers
  • The table contains a reference to the directory itself (.)
  • and to it’s parent directory( .. )
19
Q

Directory Structure:

How do Link(ln) operations work?

A
  • Works similarly to move operation, but old reference is kept
  • The inode number is copied from the first directory’s table and added to the second specified directory’s table with a new filename
  • Both directories now have a reference to the same inode, but may have different names for the same file.
  • File data is not duplicated
  • Example:

ln dir_1/myFile dir_2/myFile2

20
Q

Directory Structure:

How Move (mv)

operations work

A
  • Physical data is not moved around on the disk, and will keep the same inode
  • The inode number is copied from the table in the source directory
  • Then added to the table in the destination directory with a new filename
  • The old reference is then deleted from the table in the source directory
  • Example:

mv dir_1/derp.txt dir_2/samederp.txt

21
Q

Physical Disk Organization:

Common Data Block Size

Pointer Size

A

Block Size: 512 bytes

Pointer Size: 4 bytes

22
Q

Disk Structure:

How data is accessed by a programmer

A
  • A programmer can get to data only through an inode.
  • Data is “seen” as a simple, expandable array of bytes
  • Physical and “housekeeping” details are invisible to the programmer
23
Q

Which Operations( Access Modes)

are files protected for?

A

Each file is protected for:

  • read
  • write
  • execute
24
Q

3 Identities for File Access

A

Access (bits)permissions are defined for:

  • user
  • group
  • other
25
Q

How do Unix/Linux systems

distinguish

Users and Groups?

A

Each User has a unique identifier

  • uid
  • The etc/passwd file matches the user name and uid

Each User belongs to one or more groups

Each Group is identified by a unique identifier

  • gid
  • The etc/groups file matches group names and gid
26
Q

Where are uid’s and user names stored?

A

etc/passwd

This matches user names to uid

27
Q

Where are group id’s (gid) and names stored?

A

etc/groups

matches group names and gid

28
Q

What is the “stat” structure?

A

A structure that can be obtained at runtime,

it contains statistics and information about a specific file.

Often used to check the access mode bits on a file before attempting to use it.

29
Q

Important fields of the “stat” structure (9)

A

Given a stat structure “s”

Not comprehensive.

  • st_ino - inode
  • st_nlink - link count
  • st_uid - user id of the file
  • st_gid - group id of the file
    • st_mode - mode bits
    • st_atime - last access time
  • st_ctime - creation time
  • st_mtime - last modification time
  • st_size - size of the file
30
Q

Difference between

real uid

and

effective uid

A

Real uid

  • the uid of the actual user who is logged in

Effective uid

  • uid temporarily used for the lifetime of a process

Most of the time, the effective uid and real uid are the same,

but Administrative programs are able to operate with a different uid than the invoker.

31
Q

How to get the

Status of a file

(in code)

A

Need to use the “stat” struct

  • located in the “sys/stat.h” library

Use one of the functions:

  • stat( “pathname”, stat s)
  • fstat()
  • lstat()

To return a stat structure, placed in s

Then check s->st_mode to determine the file type and if the program has the correct permissions to access

32
Q

File Status:

Helpful operations defined in

sys/stat.h

(2)

A

S_ISREG(m)

  • Check if the file is a regular file
  • pass the stat struct as the parameter “m”
  • if( S_ISREG( s->st_mode) ) …

S_ISDIR(m)

  • Check if the file is a directory
  • if ( S_ISDIR( s->st_mode) ) …
33
Q

stat structure:

How to check permissions

A

the sys/stat.h header file defines some useful values to use as bitmasks to check against s->st_mode:

  • User permissions:
    • S_IRUSR, S_IWUSR, S_IXUSR
  • Group permissions:
    • S_IRGRP, S_IWGRP, S_IXGRP
  • Other permissions:
    • S_IROTH, S_IWOTH, S_IXOTH

These can be “anded” against s->st_mode:

  • if (s->st_mode && S_IRUSR) …
34
Q

inodes and Datablocks

are reclaimed only

when ________

A

Two conditions are met:

  • The Link Count becomes 0
  • The number of “opens” becomes 0
35
Q

How to change the

Effective uid

of a program?

A
  • Must use the “set-uid” bit on executables
  • The Super User must turn on the set-uid bit for trusted programs
  • The effective uid becomes that of the executable’s owner for the duration of the process

Turning on the set-uid bit:

Use chmod, as super user. It is the 4th of the permission bits:

-rws –x –x

36
Q

Symbolic Links:

Overview

A
  • A special file type that simply contains the name of another file
  • Different from regular links, called “hard links”
  • All file operations attempted on a symbolic link will follow the link and affect the destination file
  • Created using command:
    • ln -s
  • Also called sym links
37
Q

How to list inodes on the shell

A

ls -i

38
Q

Design Consequences of Inodes

A
  • Directories reference files only through inode numbers
    • moving files between directories is very fast
    • and, the operation is atomic (as are link and remove)
  • Two directories can include the same inode number
    • a file can be shared, appearing in several places with same or different name
    • also, no special cases for “shortcuts” etc.
  • Space is freed only when inode link & open counts are both 0
    • removing a file may not create free space
    • files can’t disappear while a process is working on them
    • also, no special cases for “sharing violation” etc.
  • An inode number is unique only within a file system
    • files can’t span partitions or disks, or be moved to new disks
    • and, files can’t be bigger than a single physical disk
  • Files don’t have “type” (beyond regular vs. directory)
    • no optimizations for databases, no self-identifying objects, no new types
    • but, easy to code general programs such as “cp”, “mv”
  • Plus, as we will see, file security information is in the inode
    • data is protected even when files are shared (details later)