03 ch3 ELEC462
03 ch3 ELEC462
(ELEC462)
Directories and File Properties:
Looking through ls
Dukyun Nam
HPC Lab@KNU
Contents
● Introduction
● Project: ls command
● Brief Review of the File System Tree
● Converting File Mode to a String
● The Three Special Bits
● Setting and Modifying the Properties of a File
● Summary
● Appendix: gdb Debugger
2
Introduction
● Recall
○ Last time, we looked at ways to operate on the CONTENTS of a file:
open, read, write, lseek, close
● But, there is more to a file than just contents:
○ 1) Properties
■ Size, owner, permission, type, etc.
○ 2) Location in a directory tree
● To learn about these:
< Data flow in the who command >
○ We shall write a version of ls
3
What Does ls Do?
● Type the command ls to see what it does:
4
What Does ls Do? (cont.)
● When given the ‘-l’ command-line option
○ ls present information about each file and its properties using the long
format
5
What Does ls Do? (cont.)
● Listing other directories
6
What Does ls Do? (cont.)
● Popular command-line options
7
Brief Review of the File System Tree
9
How Does ls Work? (cont.)
● Reading the contents of a directory
10
How Does ls Work? (cont.)
● The dirent structure
○ Typically defined /usr/include/dirent.h
■ An inode (index node) serves as a unique identifier for a specific piece of metadata on
a given filesystem
■ d_name contains the null-terminated filename
11
Can I Write ls? (ls1.c)
● Logic for listing a directory:
12
Writing ls -l
● ls -l operation
○ Mode: type of file (-, d) + permissions (user, group, everyone (other))
○ Links: reference to a file (often used in Linux)
○ Owner
○ Group
○ Size
○ Last-modified
○ Name
13
Writing ls -l (cont.)
● Needs a system call, called “stat”:
15
Writing ls -l (cont.)
● ‘statinfo.c’ : to show file properties via stat()
16
Writing ls -l (cont.)
● Results of statinfo
17
Converting File Mode to a String
18
Converting File Mode to a String (cont.)
● Subfield coding
● Masking
19
Converting File Mode to a String (cont.)
● Using masking to decode file type and permission bits
20
Converting User/Group ID to Strings
● Users
○ /etc/passwd is the list of users
■ But that does not always present a complete list of users
○ A library function getpwuid provides access to the complete list of users.
● Groups
○ /etc/group is the list of group
○ A user can belong to more than one group
○ Function getgrgid provides access to list of group
21
Putting It All Together: ls2.c
22
Putting It All Together: ls2.c (cont.)
23
Putting It All Together: ls2.c (cont.)
24
Putting It All Together: ls2.c (cont.)
25
The Three Special Bits
● The st_mode member of the stat structure contains 16 bits
26
1. The Set-User-ID Bit
● That SUID bit tells the kernel to run the program as though it were being
run by the owner of the program: let’s type ‘passwd.’
○ Note that this program belongs to “root” as owner and “root” as group.
● The name ‘set user ID’ comes from the fact that the bit causes the kernel
to set the effective user ID to the user ID of the owner of the program
○ User root owns /etc/passwd, so a program running as root can modify the file
27
2. The Set-Group-ID Bit
28
3. The Sticky Bit
● For files
○ The sticky bit tells the kernel to keep the program on the “swap device”
(somewhere on disk) even if nobody was using it right now
■ The program gets never fragmented on the swap device on the disk
○ If the bit is “unset,” then the program might be split into many small
sections scattered across the disk
■ So, the loading time of the program may get longer than when the bit is unset
○ Typically, loading a program from the swap device is faster than that from
the regular section of the disk
■ By having the bit set to the file associated with the program, we can load the program
quicker than otherwise
29
3. The Sticky Bit (cont.)
● For directories
○ Some directories are designed to hold “temporary” files
○ These scratch (temp) directories, notably /tmp, are publicly writable, allowing
any user to create and delete any files there
○ Setting the sticky bit overrides the publicly writable attribute for a directory
■ To avoid deletion of a folder and its content by other users
○ Files in the directory with the sticky bit set may only be deleted by their owners
■ “No one can delete things in my directory!”
30
Setting and Modifying the Properties of a File
● 1) Type of a file
○ A file has a type
■ It can be a regular file(-), a directory(d), a device file(b), a socket(s), a symbolic link(l),
or a named pipe(p) (connected processes by a pipe)
○ Establishing the “type” of a file
■ The type of the file is determined when the file is created
● For example, the creat system call creates a regular file
● Different system calls are used to create directories, devices, and the like
31
Setting and Modifying the Properties of a File (cont.)
● 2) Permission bits and special bits
○ Establishing the mode of a file
○ Changing the mode of a file
32
Setting and Modifying the Properties of a File (cont.)
● 3) Number of links to a file
○ Represents the number of times the file is referenced in directories (or
somewhere in the filesystem)
33
Setting and Modifying the Properties of a File (cont.)
● 4) Changing the owner and group of a file
○ A program can modify the owner and group of a file by making the chown
system call
34
Setting and Modifying the Properties of a File (cont.)
● 5) Size of a file
○ The size of a file, directory, and named pipe represents the number of bytes
stored
● 6) Modification and access time
35
Setting and Modifying the Properties of a File (cont.)
● 7) Name of a file
○ Changing the name of a file is the ‘rename’ system call
36
Summary
● Chapter 3 continues the discussion
about files, showing there is more to
files than their contents.
● Files are stored in directories, and files
have attributes.
● Chapter 3 shows how to read a
directory, and explains how to read,
modify, and understand file attributes.
● The project for the chapter is to
understand and write a version of the ls
command.
37
Appendix
gdb Debugger
38
What is gdb?
● “GNU Debugger”
● A debugger for several languages, including C and C++
● It allows you to inspect what the program is doing at a certain point
during execution
● Errors like segmentation faults may be easier to find with the help of
gdb
● Online manual
○ https://sourceware.org/gdb/current/onlinedocs/gdb/
39
gdb Debugger
● GDB: interactive debugger
○ Allows the user to run a program and interactively examine its execution.
○ Features include:
■ breakpoints (“run until control reaches here, then prompt user”)
■ stack backtrace (chain of calls leading to some point in the code)
■ examination of program variables
41
gdb Commands (cont.)
● step
○ Execute a line; if the current line has a function call, then run into the function and execute each
line of the function at a time (abbreviated command: s)
● next
○ Continue to the next source line; if that line has a function call, then execute the function without
stopping (abbreviated command: n)
● print expr
○ Perform the ‘expr’ expression and then print its value (outcome) (abbreviated: p).
● display
○ Enable automatic displaying of certain expressions each time GDB stops at a breakpoint or after a
step
● list [first, last]
○ Print lines from first to last: 10 lines by default
42
gdb Hands-on with buggy
● Run the following code
#include <stdio.h>
#include "string.h"
int tester();
tester(x, k);
}
44
gdb Hands-on with buggy (cont.)
● Enable debugging information
$ gcc -g buggy.c
$ ./a.out
Segmentation fault (core dumped)
(gdb) run
...
Program received signal SIGSEGV, Segmentation fault.
0x00005555555551ce in main (argc=1, argv=0x7fffffffe4b8) at buggy.c:9
9 x[i] = i;
45
gdb Hands-on with buggy (cont.)
● Execute inside gdb
$ gcc -g buggy.c
$ ./a.out
Segmentation fault (core dumped)
(gdb) break buggy.c:8
Breakpoint 1 at 0x5555555551b4: file buggy.c, line 8.
(gdb) step
...
(gdb) print i
(gdb) print x[i]
(gdb) display i
(gdb) display x[i]
(gdb) list
(gdb) continue
...
(gdb) quit
46