chapter 4 2

13

Upload: lopjuan

Post on 14-Jan-2015

261 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Chapter 4 2
Page 2: Chapter 4 2
Page 3: Chapter 4 2

System Call mkdir

NAME

mkdir - create a directory

SYNOPSIS

#include <sys/stat.h>

#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);

DESCRIPTION

mkdir() attempts to create a directory named pathname.

RETURN VALUE

On success, zero is returned. On error, -1 is returned, and errno is set appropriately

Page 4: Chapter 4 2

System Call rmdir

NAME

rmdir - delete a directory

SYNOPSIS

#include <unistd.h>

int rmdir(const char *pathname);

DESCRIPTION

rmdir() deletes a directory, which must be empty.

RETURN VALUE

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

Page 5: Chapter 4 2

System Call unlink

NAME

unlink - delete a name and possibly the file it refers to

SYNOPSIS

#include <unistd.h>

int unlink(const char *pathname);

DESCRIPTION

unlink() deletes a name from the file system. If that name was the

last link to a file and no processes have the file open the file is

deleted and the space it was using is made available for reuse.

If the name was the last link to a file but any processes still have

the file open the file will remain in existence until the last file

descriptor referring to it is closed.

If the name referred to a symbolic link the link is removed.

RETURN VALUE

On success, zero is returned. On error, -1 is returned, and errno is

set appropriately.

Page 6: Chapter 4 2

System Calllink

NAME

link - make a new name for a file

SYNOPSIS

#include <unistd.h>

int link(const char *oldpath, const char *newpath);

DESCRIPTION

link() creates a new link (also known as a hard link) to an existing

file.

If newpath exists it will not be overwritten.

This new name may be used exactly as the old one for any operation;

both names refer to the same file (and so have the same permissions and

ownership) and it is impossible to tell which name was the "original".

RETURN VALUE

On success, zero is returned. On error, -1 is returned, and errno is

set appropriately.

Page 7: Chapter 4 2

System Callrename

NAME

rename - change the name or location of a file

SYNOPSIS

#include <stdio.h>

int rename(const char *oldpath, const char *newpath);

DESCRIPTION

rename() renames a file, moving it between directories if required.

Any other hard links to the file (as created using link(2)) are unaf-

fected. Open file descriptors for oldpath are also unaffected.

If newpath already exists it will be atomically replaced (subject to a

few conditions; see ERRORS below), so that there is no point at which

another process attempting to access newpath will find it missing.

RETURN VALUE

On success, zero is returned. On error, -1 is returned, and errno is

set appropriately.

Page 8: Chapter 4 2

System Callchdir

NAME

chdir, fchdir - change working directory

SYNOPSIS

#include <unistd.h>

int chdir(const char *path);

int fchdir(int fd);

DESCRIPTION

chdir() changes the current working directory of the calling process to

the directory specified in path.

fchdir() is identical to chdir(); the only difference is that the directory is given as an open file descriptor.

RETURN VALUE

On success, zero is returned. On error, -1 is returned, and errno is set

appropriately.

Page 9: Chapter 4 2

What is current directory?

• Each running program on Unix has a current directory of the process.

• Internally, the process keeps a variable that store the inode number of the current directory.

• Change to a new directory is to change the value of that variable

Page 10: Chapter 4 2
Page 11: Chapter 4 2

865 .

193 ..

491 Y

277 A

520 C

277 .

865 ..

402 Y

520 .

865 ..

651 d1

247 d2

.

520 ..

402 Xlink

247 .

520 ..

680 xcopy

Page 12: Chapter 4 2

Algorithm of pwd

• Note the inode number for “.” call it n• chdir .. (use chdir)• Find the name of the link with inode n (use

opendir, readdir, closedir)

repeat (until reach the top of the tree)

Note:• In the root directory, “.” and “..” point to the

same inode

Page 13: Chapter 4 2

Homework

• Write a program which receives a filename and return its inode number.

Inode test.txtThis i node number is 1234

Read pwd.c on page 13.