0% found this document useful (0 votes)
35 views

L15-File Management

The document discusses file management concepts in Linux/Unix systems such as creating and removing directories using the mkdir() and rmdir() functions. It describes how directories are represented and organized in a file system. It also covers creating and reading directory entries, creating special device files using mknod(), and retrieving file metadata like device numbers using the stat structure. The key functions and system calls related to directories and special files are explained with examples.

Uploaded by

jackops
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

L15-File Management

The document discusses file management concepts in Linux/Unix systems such as creating and removing directories using the mkdir() and rmdir() functions. It describes how directories are represented and organized in a file system. It also covers creating and reading directory entries, creating special device files using mknod(), and retrieving file metadata like device numbers using the stat structure. The key functions and system calls related to directories and special files are explained with examples.

Uploaded by

jackops
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

L15-File Management

Prof. Jibi Abraham

College of Engineering Pune (COEP)


Forerunners in Technical Education
rmdir()
• int rmdir(char *path);
/* Returns 0 on success and -1 on error */
• is used remove empty directories from the file system
• If the directory is not an empty directory, rmdir() shall fail and
set errno to [EEXIST] or [ENOTEMPTY].
• If path refers either dot or dot-dot, rmdir() shall fail
• If link count becomes 0 and no process has the directory open,
the space occupied by the directory shall be freed
• If any process has opened the directory when last link is
removed, the dot and dot-dot entries, if present, shall be
removed before rmdir() returns and no new entries may be
created in the directory, but the directory shall not be removed
until all references to the directory are closed
Program to Create and Remove Directory
int main(int argc,char *argv[])
{ int md,rd;
md = mkdir(argv[1],0777);
if(md == 0)
printf("%s directory is created\n",argv[1]);
else
printf("%s directory is not created\n",argv[1]);
rd = rmdir(argv[2]);
if(rd == 0)
printf("%s directory is removed\n",argv[2]);
else
printf("%s directory is not removed\n",argv[2]);
}
Poll Question
• Upon successful completion, the rmdir() function shall
mark for update the ____ fields of the parent directory
a) st_ctime
b) st_mtime
c) st_atime
d) all 3 times
e) st_ctime and st_mtime
f) st_atime and st_ctime
g) st_mtime and st_atime
Directory Features
• uid of the new directory is the effective uid of the
calling process
• gid of the new directory is the effective gid of the
calling process or the gid of the directory in which it is
created
Reading Directories
• #include <dirent.h>

• DIR *opendir(const char *name);


• struct dirent *readdir(DIR *dir);

• A directory file is record oriented

• struct dirent {
ino_t d_ino; /* Inode number */
char d_name[NAME_MAX + 1]; /* Null-terminated filename */
}
• If the end of the directory stream is reached, NULL is
returned
Reading Directories
• #include <dirent.h>

• void rewinddir(DIR *dir); reset directory stream


• off_t telldir(DIR *dir); return current location in
directory stream
• void seekdir(DIR *dir, off_t offset); set the position of
the next readdir() call in the directory stream
• int closedir(DIR *dir);
“ls” command
int main()
{ struct dirent *direntp;
DIR *dirp;
dirp=opendir("."); /* Open the current directory */
while((direntp = readdir(dirp)) != NULL)
printf("%s \n",direntp →d_name);
closedir(dirp);
return 0;
}
There is no malloc to store the records read by readdir. Then where does the direntp pointing to?

is statically allocated and hence part of the process memory


Search in a Directory
• Program to search in a given directory for a given
filename. If the file exists in the directory, returns 0.
Otherwise, it returns a nonzero value
Poll Question
struct dirent *direntp;
DIR *dirp;
int md, count = 0;
md = mkdir(argv[1], 0777);
dirp=opendir(argv[1]);
while((direntp = readdir(dirp)) != NULL) count++;
closedir(dirp);
• What will be the value of count?
A. 0
B. 1
C. 2
D. Others
mknode()
• mknod() creates a file (regular, file, device special
file, or named pipe) named pathname, with attributes
specified by mode and dev
• mode argument specifies both the file mode to use
and the type of file to be created
• int mknod(char *path, mode_t mode, dev_t dev);
– Directory created with makenod does not include ’.’ and ‘..’
files
• file type must be one of S_IFREG, S_IFCHR,
S_IFBLK, S_IFIFO, or S_IFSOCK
• Dev specifies major and minor number for device
files, others 0
• Linux, mknod() cannot be used to create directories
Device Files
• Device nodes are special files usually under /dev that
allow applications to interface with device drivers
• When an application performs the usual Unix I/O—
opening, closing, reading, writing and so on—on a
device node, the kernel does not handle those
requests as normal file I/O
• Kernel passes such requests to a device driver
• Device driver handles the I/O operation and returns
the results to the user
• Character special Device files

• Block special Device file


Major and minor numbers
• Each device node is assigned two numerical values,
called a major number and a minor number
• Major and minor numbers map to a specific device
driver loaded into the kernel
• null device has a major number of 1 and a minor
number of 3. It lives at /dev/null
• major number identifies the device driver
• minor number identifies the specific subdevice
• Eg. a disk drive often contains several file systems.
Each file system on the same disk drive would usually
have the same major number, but a different minor
number
Device Files
• int mknod(char *path, mode_t mode, int device_id);
– Returns 0 on success and -1 on failure
– Mode specifies the access permission and S_IFCHR or
S_IFBLK flag
– mknode must be called by a process with superuser
privileges
– Device_id specifies the major and minor device numbers to
set st_rdev fields for the device
• Early systems stored the device number in a 16-bit
integer, with upper 8 bits for the major number and
lower 8 bits for the minor
Example
• Create a char device file with command line
arguments

maj = atoi(argv[2]; min = atoi[argv[3];


mknod(argv[1], S_IFCHR|S_IRWXU, maj<<8|min
st_dev and st_rdev
• st_dev value for every filename on a
system is the device number of the
file system containing that filename
and its corresponding i-node.
• combination of st_dev and st_ino
uniquely identifies a file across all
file systems
• Only character special files and
block special files have an st_rdev
value. This value contains the
device number for the actual device.
Example Program
• To print device number for each command-line argument. If the
argument is a character or a block special file, the st_rdev value
is also printed.
Sample Output
Thank You

College of Engineering Pune (COEP)


Forerunners in Technical Education

You might also like