Presented By :
K. Kumaraswamy
Dept.of CSE
Asst. Professor
LINUX PROGRAMMING
        Unit-II
 Files and Directories:
 Files and Directories:
                          2
                               UNIT-2
 Files : File Concept, File System Structure, File metadata- Inodes,
kernel support for files, System calls, Kernel support for files,
System calls for File I/O Operations- open, creat, read, write , close,
lseek, dup2file, filestatus information- stat family, File and record
locking-fcntl function, File permissions chmod, fchmod, file
ownership-chown, lchown, fchown, links- softlinks and hard links-
Symlink, Link, Unlink
 Directories: Creating, removing and changing directories- mkdir,
rmdir, chdir, obtaining current working directory-getcwd, Directory
contents, Scanning Directories- Opendir, readdir, closedir,
rewinddir functions
                                                                    3
1. Regular files: The most common type of file, which contains data of
some form. There is no distinction to the UNIX kernel whether this
data is text or binary.
2. Directory file: A file contains the names of other files and pointers
to information on these files. Any process that has read permission for
a directory file can read the contents of the directory, but only the
kernel can write to a directory file.
3. Character special file: A type of file used for certain types of devices
on a system.
4. Block special file: A type of file typically used for disk devices. All
devices on a system are either character special files or block special
files.
5. FIFO: A type of file used for interprocess communication
between processes. It’s sometimes called a named pipe.
6. Socket: A type of file used for network communication between
processes. A socket can also be used for nonnetwork communication
between processes on a single host.
7. Symbolic link: A type of file that points to another file.
Linux filesystem structures and understand the meaning of
individual high-level directories.
1. / – Root
Every single file and directory starts from the root directory.
Only root user has write privilege under this directory.
2. /bin – User Binaries
Contains binary executables.
Common linux commands you need to use in single-user modes are
located under this directory.
For example: ps, ls, ping, grep, cp.
3. /sbin – System Binaries
Just like /bin, /sbin also contains binary executables.
For example: iptables, reboot, fdisk, ifconfig, swapon
4. /etc – Configuration Files
Contains configuration files required by all programs.
This also contains startup and shutdown shell scripts used to
start/stop individual programs.
For example: /etc/resolv.conf, /etc/logrotate.conf
5. /dev – Device Files
Contains device files.
These include terminal devices, usb, or any device attached to the
system.
For example: /dev/tty1, /dev/usbmon0
6. /proc – Process Information
Contains information about system process.
This is a pseudo filesystem contains information about running
process. For example: /proc/{pid} directory contains information
about the process with that particular pid.
This is a virtual filesystem with text information about system
resources. For example: /proc/uptime
7. /var – Variable Files
var stands for variable files.
Content of the files that are expected to grow can be found under this
directory.
This includes — system log files (/var/log); packages and database
files (/var/lib); emails (/var/mail); print queues (/var/spool); lock files
(/var/lock); temp files needed across reboots (/var/tmp);
8. /tmp – Temporary Files
Directory that contains temporary files created by system and
users.
Files under this directory are deleted when system is rebooted.
9. /usr – User Programs
Contains binaries, libraries, documentation, and source-code for
second level programs.
/usr/bin contains binary files for user programs. If you can’t find a
user binary under /bin, look under /usr/bin. For example: at, awk,
cc, less, scp
10. /home – Home Directories
Home directories for all users to store their personal files.
For example: /home/john, /home/nikita
11. /boot – Boot Loader Files
Contains boot loader related files.
Kernel initrd, vmlinux, grub files are located under /boot
For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-
generic
12. /lib – System Libraries
Contains library files that supports the binaries located under /bin and
/sbin
Library filenames are either ld* or lib*.so.*
For example: ld-2.11.1.so, libncurses.so.5.7
13. /opt – Optional add-on Applications
opt stands for optional.
Contains add-on applications from individual vendors.
add-on applications should be installed under either /opt/ or /opt/ sub-
directory.
                           File Metadata
 Inodes
• A structure that is maintained in a separate area of the hard disk.
• File attributes are stored in the inode.
• Every file is associated with a table called the inode.
• The inode is accessed by the inode number.
• Inode contains the following attributes of a file: file type, file
permissions , no. of links
UID of the owner, GID of the group owner, file size date and time
of last modification, last access, change.
File attributes     Attribute value meaning
File type           type of the file
Access permission   file access permission for owner, group and others
Hard link count     no.of hard links of a file.
UID                 file owner user ID.
GID                 the file group ID.
File size            file size in bytes
                      Kernal support for files
The internal representation of a file is given by an inode, which
contains a description of the disk layout of the file data and other
information such as the file owner, access permissions, and access
times.
The term inode is a contraction of the term index node and is
commonly used in literature on the UNIX system.
Every file has one inode, but it may have several names, all of which
map into the inode.
Each name is called a link. When a process refers to a file by
name, the kernel parses the file name one component at a time,
checks that the process has permission to search the directories in
the path, and eventually retrieves the inode for the file.
 For example, if a process callsthe kernel retrieves the inode for
"/fs2/mjb/rje/sourcefile".
When a process creates a new file, the kernel assigns it an unused
inode. Inodes are stored in the file system, as will be seen shortly,
but the kernel reads them into an in-core2) inode table when
manipulating files.
Figure 2. File Descriptors, File Table, and Inode Table
                       System calls
 To use the services in the OS Unix offers some special
  functions known as system calls.
 The system call is a task which performs very basic functions
  that requires communication with CUP, memory and other
  devices.
 The system calls are functions used in the kernel itself. To the
  programmer, the system call appears as a normal C function
  call.
                               L1.1                             17
• UNIX system calls are used to manage the file
  system, control processes, and to provide inter
  process communication.
Types of system calls in UNIX:
1.Open     2.create 3.read 4.write
5.Close    6.lseek    7.stat 8.fstat
9.ioctl     10.umask 11.dup 12.dup2
                       L1.2                     18
open function:
A file is opened or created by calling the open
   function.
#include <fcntl.h>
 int open(const char *pathname, int oflag, .../
   mode_t mode */ );
Returns: file descriptor if OK, -1 on error.
The pathname is the name of the file to open or
   create. This function has a multitude of
   options, which are specified by the oflag
   argument.             L2.1                     19
• This argument is formed by ORing together
  one or more of the following constants from
  the <fcntl.h> header:
O_RDONLY open for reading only.
O_WRONLY open for writing only.
O_RDWR          open for reading and writing.
• Most implementations define O_RDONLY as
  0, O_WRONLY as 1, and O_RDWR as 2, for
  compatibility with older programs.
                      L2.2                      20
 creat Function
A new file can also be created by calling the
   creat function.
Syntax:
#include <fcntl.h>
 int creat( const char *pathname, mode_t mode);
Returns: file descriptor opened for write-only if
   OK, -1 on error.
This function is equivalent to
open (pathname, O_WRONLY | O_CREAT |
   O_TRUNC, mode);
                       L2.5                     21
close Function:
• An open file is closed by calling the close
  function.
#include <unistd.h>
int close(int filedes);
 Returns: 0 if OK,-1 on error.
                        L3.1                    22
read :
• Data is read from an open file with the read
  function.
#include <unistd.h>
ssize_t read( int filedes, void *buf, size_t
  nbytes);
Returns: number of bytes read, 0 if end of file, -1
  on error
• read attempts to read nbyte characters from
  the file descriptor filedes into the buffer
  buf.buf represents the pointer to the generic
  buffer. nbyte is the size
                         L3.2
                              of the buffer.      23
write function:
• Data is written to an open file with the write
  function.
#include <unistd.h>
ssize_t write(int filedes, const void *buf,
   size_t nbytes);
Returns: number of bytes written if OK,-1 on
  error
• write writes nbyte no. of bytes from the
  generic buffer buf to the file descriptor filedes.
                         L3.3                      24
Implementing cat:
#include< fcntl.h>
#define BUFSIZE 1
main(int argc, char *argv[])
{
int fd, n; char buf;
fd=open(argv[1],O_RDONLY);
printf(“conents of %s file are”,argv[1]);
while((n=read(fd,&buf,1))>0)
{
write(1,&buf,1);
}
                            L3.4            25
}
Implementing mv:
#include< fcntl.h>
#include< stdio.h>
#include<unistd.h>
#include<sys/ stat.h>
main(int argc, char *argv[])
{
int fd1,fd2;
fd1=open(argv[1],O_RDONLY);
Fd2= creat(argv[2],S_IWUSR);
rename(fd1,fd2);
unlink(argv[1]);
                           L3.5   26
}
lseek function:
An open file's offset can be set explicitly by
  calling lseek.
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
Returns: new file offset if OK, -1 on error.
                        L3.6                          27
stat, fstat, and lstat Functions:
#include <sys/stat.h>
int stat(const char * pathname, struct stat * buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char * pathname, struct stat * buf);
All three return: 0 if OK, -1 on error.
Given a pathname, the stat function returns a
  structure of information about the named file. The
  fstat function obtains information about the file
  that is already open on the descriptor filedes.
                                                 28
 The definition of the structure can be:
struct stat {
mode_t st_mode; /*file type & mode*/
ino_t st_ino;        /* i-node number */
dev_t st_dev; /* device number */
nlink_t st_nlink; /* number of links */
uid_t st_uid;     /* user ID of owner */
gid_t st_gid;     /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last file status change */
                                                      29
blksize_t st_blksize; blkcnt_t st_blocks; };
File Types:
The types of files are:
1.Regular file
2.Directory file
3.Block special file
4.Character special file
5.FIFO
6.Socket
7.Symbolic link
• The type of a file is encoded in the st_mode
  member of the stat structure.
                                             30
Macro         Type of file
S_ISREG() regular file
S_ISDIR()      directory file
S_ISCHR() character special file
S_ISBLK()      block special file
S_ISFIFO() pipe or FIFO
S_ISLNK()      symbolic link
S_ISSOCK() socket
• File type macros are defined in
  <sys/stat.h>.
                                    31
File Access Permissions:
• The st_mode value also encodes the access
  permission bits for the file.
   S_IRUSR user-read
   S_IWUSR user-write
   S_IXUSR user-execute
   S_IRGRP group-read
   S_IWGRP group-write
   S_IXGRP group-execute
   S_IROTH other-read
   S_IWOTH other-write
                                          32
   S_IXOTH other-execute
access function:
#include <unistd.h>
int access(const char *pathname, int mode);
Returns: 0 if OK,-1 on error.
The mode is the bitwise OR of any of the
  constants shown below.
Mode        description
R_OK      test for read permission
W_OK test for write permission
X_OK      test for execute permission
F_OK      test for existence of a file        33
ioctl Function:
It performs a variety of control functions on
   devices.
#include <unistd.h> /* System V */
#include <sys/ioctl.h>/* BSD and Linux*/
   #include <stropts.h> /* XSI STREAMS */ int
   ioctl(int filedes, int request, ...);
Returns: -1 on error, something else if OK.
                                            34
umask Function:
• The umask function sets the file mode
  creation mask for the process and returns the
  previous value.
   #include <sys/stat.h>
   mode_t umask(mode_t cmask);
Returns: previous file mode creation mask.
• The cmask argument is formed as the bitwise
  OR of any of the nine constants like
  S_IRUSR, S_IWUSR, and so on.
                                            35
 File Sharing:
• The UNIX System supports the sharing of open
  files among different processes.
• The kernel uses three data structures to represent
  an open file:
1.Process table: Every process has an entry in the
  process table. Within each process table entry is
  a table of open file descriptors, with one entry
  per descriptor. Associated with each file
  descriptor are
a)The file descriptor flags
b)A pointer to a file table entry                36
2.File table: The kernel maintains a file table for all
  open files. Each file table entry contains
   a)The file status flags for the file, such as read,
  write, append, sync, and non blocking
   b)The current file offset
   c) A pointer to the v-node table entry for the file
3.v-node table: Each open file (or device) has a v-
  node structure that contains information about the
  type of file and pointers to functions that operate
  on the file.
                                                    37
dup and dup2 Functions:
• An existing file descriptor is duplicated by
  either of the following functions.
   #include <unistd.h>
   int dup(int filedes);
   int dup2(int filedes, int filedes2);
Both return: new file descriptor if OK,-1 on
  error.
• The new file descriptor returned by dup is
  guaranteed to be the lowest-numbered
  available file descriptor.
                                            38
• With dup2, we specify the value of the new
  descriptor with the filedes2 argument. If filedes2
  is already open, it is first closed. If filedes equals
  filedes2, then dup2 returns filedes2 without
  closing it.
• dup(filedes); is equivalent to
  fcntl(filedes, F_DUPFD, 0);
• dup2(filedes, filedes2); is equivalent to
  close(filedes2);
           fcntl(filedes, F_DUPFD, filedes2);
                                                     39
 chown, fchown, and lchown Functions:
• The chown functions allow us to change the user ID of a
   file and the group ID of a file.
#include <unistd.h>
int chown (const char *pathname, uid_t owner, gid_t group);
int fchown (int filedes, uid_t owner, gid_t group);
int lchown (const char *pathname, uid_t owner, gid_t
   group);
All three return: 0 if OK,-1 on error.
These three functions operate similarly unless the referenced
   file is a symbolic link.
                                                         40
Unlink:
• To remove an existing directory entry, we call the
  unlink function.
#include <unistd.h>
int unlink(const char *pathname);
Returns: 0 if OK, 1 on error.
This function removes the directory entry and
  decrements the link count of the file referenced by
  pathname. If there are other links to the file, the
  data in the file is still accessible through the other
  links. The file is not changed if an error occurs.
                                                    41
Link:
• we can create a link to an existing file is with the
  link function.
#include <unistd.h>
int link(const char *existingpath, const char
  *newpath);
Returns: 0 if OK, -1 on error.
This function creates a new directory entry,
  newpath, that references the existing file
  existingpath. If the newpath already exists, an
  error is returned. Only the last component of the
  newpath is created.                               42
symlink :
• A symbolic link is created with the symlink
  function.
#include <unistd.h>
int symlink(const char *actualpath, const char
  *sympath);
Returns: 0 if OK, -1 on error.
• A new directory entry, sympath, is created
  that points to actualpath. It is not required that
  actualpath exist when the symbolic link is
  created.
                                                 43
mkdir and rmdir :
• Directories are created with the mkdir function
  and deleted with the rmdir function.
#include <sys/stat.h>
int mkdir (const char *pathname, mode_t mode);
Returns: 0 if OK, -1 on error.
This function creates a new, empty directory. The
  entries for dot and dot-dot are automatically
  created. The specified file access permissions,
  mode, are modified by the file mode creation
  mask of the process.
                                              44
rmdir:
• An empty directory is deleted with the rmdir
  function. An empty directory is one that contains
  entries only for dot and dot-dot.
#include <unistd.h>
int rmdir(const char *pathname);
Returns: 0 if OK, -1 on error.
• If the link count of the directory becomes 0 with
  this call, and if no other process has the directory
  open, then the space occupied by the directory is
  freed.
                                                  45
Chdir, fchdir:
• We can change the current working directory of
  the calling process by calling the chdir or fchdir
  functions.
#include <unistd.h>
int chdir (const char *pathname);
int fchdir(int filedes);
Both return: 0 if OK, -1 on error.
• We can specify the new current working directory
  either as a pathname or through an open file
  descriptor.
                                                 46
 Example of chdir function:
int main(void)
 {
 if (chdir("/tmp") < 0)
err_sys("chdir failed");
 printf("chdir to /tmp succeeded\n");
exit(0);
 }
                                        47
getcwd :
#include <unistd.h>
char *getcwd (char *buf, size_t size);
Returns: buf if OK, NULL on error
We must pass to this function the address of a
  buffer, buf, and its size (in bytes). The buffer
  must be large enough to accommodate the
  absolute pathname plus a terminating null byte, or
  an error is returned.
                                                 48
 Example of getcwd function:
Int main(void)
 {
 char *ptr; int size;
if (chdir("/usr/spool/uucppublic") < 0)
   err_sys("chdir failed");
 ptr = path_alloc(&size); /* our own function */
 if (getcwd(ptr, size) == NULL)
 err_sys("getcwd failed");
 printf("cwd = %s\n", ptr);
 exit(0); }                                      49
    Directory handling system calls:
opendir:
#include <dirent.h>
DIR *opendir(const char *pathname);
Returns: pointer if OK, NULL on error.
readdir:
struct dirent *readdir(DIR *dp);
Returns: pointer if OK, NULL at end of directory
  or error.
                                             50
• The dirent structure defined in the file
   <dirent.h> is implementation dependent.
   Implementations define the structure to
   contain at least the following two members:
struct dirent
 {
ino_t d_ino;/* i-node number */
char d_name [NAME_MAX + 1]; /* null-
   terminated filename */
}
                                                 51
closedir, rewinddir, telldir, seekdir:
int closedir(DIR *dp);
Returns: 0 if OK, -1 on error.
void rewinddir(DIR *dp);
long telldir (DIR *dp);
void seekdir(DIR *dp, long loc);
Returns: current location in directory associated
  with dp.
                                               52