Unit 1 - Files and directories Flashcards

1
Q

Syntax and purpose of stat function

A
#include 
int stat(const char *restrict pathname, struct stat *restrict buf );

returns: 0 if OK, −1 on error

Given a pathname, the stat function returns a structure of information about the
named file.

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

Syntax and purpose of fstat function

A
#include 
int fstat(int fd, struct stat *buf );

The fstat function obtains information about the file that is already open on the descriptor fd.

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

Syntax and purpose of lstat function

A
#include 
int lstat(const char *restrict pathname, struct stat *restrict buf );

The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link.

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

Syntax and purpose of fstatat function

A
#include 
int fstatat(int fd, const char *restrict pathname,
struct stat *restrict buf, int flag);

The fstatat function provides a way to return the file statistics for a pathname relative to an open directory represented by the fd argument.

The flag argument
controls whether symbolic links are followed; when the AT_SYMLINK_NOFOLLOW flag is set, fstatat will not follow symbolic links, but rather returns information about the link itself. Otherwise, the default is to follow symbolic links, returning information about the file to which the symbolic link points. If the fd argument has the value AT_FDCWD and the pathname argument is a relative pathname, then fstatat evaluates the pathname argument relative to the current directory. If the pathname argument is an absolute pathname, then the fd argument is ignored. In these two cases, fstatat
behaves like either stat or lstat, depending on the value of flag.

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

list and explain the attributes of stat structure

A

struct stat {
mode_t st_mode; /* file type & mode (permissions) /
ino_t st_ino; /
i-node number (serial number) /
dev_t st_dev; /
device number (file system) /
dev_t st_rdev; /
device number for special files /
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 /
struct timespec st_atim; /
time of last access /
struct timespec st_mtim; /
time of last modification /
struct timespec st_ctim; /
time of last file status change /
blksize_t st_blksize; /
best I/O block size /
blkcnt_t st_blocks; /
number of disk blocks allocated */
};

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

File types in unix

A
  1. Regular file. 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.
    Any interpretation of the contents of a regular file is left to the application processing the file.
  2. Directory file. A file that 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 directly to a directory file. Processes must use the functions described in this chapter to
    make changes to a directory.
  3. Block special file. A type of file providing buffered I/O access in fixed-size units to devices such as disk drives. When a block special file is used for device I/O, data is transferred in large fixed-size blocks. This type of access is called block device access.
  4. Character special file. A type of file providing unbuffered I/O access in variable-sized units to devices. All devices on a system are either block special
    files or character special files. When a character special file is used for device I/O, data is transferred one character at a time. This type of access is called raw device access.
  5. FIFO. A type of file used for 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 non-network communication between processes on a single host.
  7. Symbolic link. A type of file that points to another file.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List the functions to check for file types for each type of file

A

S_ISXXX(buf.st_mode) > 0 syntax

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Different types of IDs about the owner associated to a process

A

real user ID
real group ID
who we really are

effective user ID
effective group ID
used for file access permission checks

supplementary group IDs
saved set-user-ID
saved set-group-ID
saved by exec functions

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

Define Real user/group ID

A

The real user ID and real group ID identify who we really are. These two fields
are taken from our entry in the password file when we log in. Normally, these
values don’t change during a login session,

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

Define Effective user/group ID

A

The effective user ID, effective group ID, and supplementary group IDs determine our file access permissions,

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

Define set-user-ID / set-group-ID

A

The saved set-user-ID and saved set-group-ID contain copies of the effective user ID and the effective group ID, respectively, when a program is executed.

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

List the file access permissions

A

st_mode mask

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
S_IXOTH other-execute
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Syntax and purpose of access & faccessat function

A

Sometimes, a process wants to test accessibility based on the real user and group IDs for which access and faccessat functions can be used .The access and faccessat functions base their tests on
the real user and group IDs.

#include 
int access(const char *pathname, int mode);
int faccessat(int fd, const char *pathname, int mode, int flag);
Both return: 0 if OK, −1 on error

The mode is either the value F_OK to test if a file exists, or the bitwise OR of any of the flags shown

mode
R_OK test for read permission
W_OK test for write permission
X_OK test for execute permission

The faccessat function behaves like access when the pathname argument is absolute or when the fd argument has the value AT_FDCWD and the pathname argument is relative. Otherwise, faccessat evaluates the pathname relative to the open directory
referenced by the fd argument.

The flag argument can be used to change the behavior of faccessat. If the AT_EACCESS flag is set, the access checks are made using the effective user and group
IDs of the calling process instead of the real user and group IDs.

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

How can be the ownership of a new file or a directly be set

A

The user ID of a new file is set to the effective user ID of the process.
The group ID of a new file can be the effective group ID of the process.
The group ID of a new file can be the group ID of the directory in which the file is being created.

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

Syntax and purpose of umask

A
describe the file mode creation mask that is associated with every process.
The umask function sets the file mode creation mask for the process and returns the previous value. 
#include 
mode_t umask(mode_t cmask);

Returns: previous file mode creation mask

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

Syntax and purpose of chmod, fchmod, and fchmodat Functions

A

The chmod, fchmod, and fchmodat functions allow us to change the file access permissions for an existing file.

#include 
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
int fchmodat(int fd, const char *pathname, mode_t mode, int flag);

The flag argument can be used to change the behavior of fchmodat—when the
AT_SYMLINK_NOFOLLOW flag is set, fchmodat doesn’t follow symbolic links.

17
Q

Syntax and purpose of chown, fchown, fchownat, and lchown Functions

A

The chown functions allow us to change a file’s user ID and group ID, but if either of the arguments owner or group is −1, the corresponding ID is left unchanged.

#include 
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int fchownat(int fd, const char *pathname, uid_t owner, gid_t group,int flag);
int lchown(const char *pathname, uid_t owner, gid_t group);

All four return: 0 if OK, −1 on error

fchownat acts like lchown if the AT_SYMLINK_NOFOLLOW flag is set in the flag argument, or it acts like chown if the AT_SYMLINK_NOFOLLOW flag is clear.

18
Q

Which attribute holds the file size and how does file size depend on the type of files (Regular files,Directories, sym link)

A

The st_size member of the stat structure contains the size of the file in bytes. This field is meaningful only for regular files, directories, and symbolic links.

For a regular file, a file size of 0 is allowed. We’ll get an end-of-file indication on the first read of the file. For a directory, the file size is usually a multiple of a number, such as 16 or 512. For a symbolic link, the file size is the number of bytes in the filename.

19
Q

Syntax and purpose of truncate, ftruncate functions

A

Sometimes we would like to truncate a file by chopping off data at the end of the file.
Emptying a file, which we can do with the O_TRUNC flag to open, is a special case of truncation.

#include 
int truncate(const char *pathname, off_t length);
int ftruncate(int fd, off_t length);

These two functions truncate an existing file to length bytes. If the previous size of the file was greater than length, the data beyond length is no longer accessible. Otherwise, if the previous size was less than length, the file size will increase and the data between the old end of file and the new end of file will read as 0

20
Q

Explain inode

A

An Inode number is a uniquely existing number for all the files in Linux and all Unix type systems.

When a file is created on a system, a file name and Inode number is assigned to it.

Generally, to access a file, a user uses the file name but internally file name is first mapped with respective Inode number stored in a table.

An Inode is a data structure containing metadata about the files.Following contents are stored in the Inode from a file:

User ID of file
Group ID of file
Device ID
File size
Date of creation
Permission
Owner of the file
File protection flag
Link counter to determine number of hard links

Each Inode has a unique number and Inode number can be seen with the help of ls -li command.

21
Q

Syntax and function of link and linkat function

A
#include 
int link(const char *existingpath, const char *newpath);
int linkat(int efd, const char *existingpath, int nfd, const char *newpath,int flag);
Both return: 0 if OK, −1 on error

These functions create 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. The rest of the path must already exist.
With the linkat function, the existing file is specified by both the efd and existingpath arguments, and the new pathname is specified by both the nfd and newpath
arguments. By default, if either pathname is relative, it is evaluated relative to the corresponding file descriptor. If either file descriptor is set to AT_FDCWD, then the corresponding pathname, if it is a relative pathname, is evaluated relative to the current directory. If either pathname is absolute, then the corresponding file descriptor argument is ignored

22
Q

Syntax and purpose of unlink and unlinkat function

A
#include 
int unlink(const char *pathname);
int unlinkat(int fd, const char *pathname, int flag);
Both return: 0 if OK, −1 on error

These functions remove the directory entry and decrement 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.

23
Q

Syntax and purpose of rename and renameat

A
#include 
int rename(const char *oldname, const char *newname);
int renameat(int oldfd, const char *oldname, int newfd, const char *newname);
Both return: 0 if OK, −1 on error
24
Q

Explain symbolic links

A

A symbolic link is an indirect pointer to a file, unlike the hard links described in the previous section, which pointed directly to the i-node of the file. Symbolic links were introduced to get around the limitations of hard links.
• Hard links normally require that the link and the file reside in the same file
system.
• Only the superuser can create a hard link to a directory (when supported by the
underlying file system

There are no file system limitations on a symbolic link and what it points to, and anyone
can create a symbolic link to a directory

25
Q

Syntax of symlink

A
#include 
int symlink(const char *actualpath, const char *sympath);
int symlinkat(const char *actualpath, int fd, const char *sympath);
Both return: 0 if OK, −1 on error
26
Q

Syntax and purpose of mkdir, mkdirat, rmdir

A

Directories are created with the mkdir and mkdirat functions, and deleted with the rmdir function.

#include 
int mkdir(const char *pathname, mode_t mode);
int mkdirat(int fd, const char *pathname, mode_t mode);
int rmdir(const char *pathname);
27
Q

Syntax and purpose of access function

A
#include 
int access(const char *pathname, int mode);
return: 0 if OK, −1 on error

The mode is either the value F_OK to test if a file exists, or the bitwise OR of

mode Description
R_OK test for read permission
W_OK test for write permission
X_OK test for execute permission

28
Q

What is a sticky bit

A

A Sticky bit is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file. No other user is given privileges to delete the file created by some other user.

Stick bit to a directory If the bit is set for a directory, a file in the directory can be removed or renamed only if the user has write permission for the directory and meets one of the following criteria:
• Owns the file
• Owns the directory
• Is the super user

29
Q

What are Hard links

A

Hard links: two filenames pointing to the same inode and the same data blocks.
Hardlinks must be on the same filesystem, softlinks can cross filesystems.
Hardlinked files stay linked even if you move either of them (unless you move one to another file system triggering the copy-and-delete mechanism).
Hardlinked files are co-equal, while the original is special in softlinks, and deleting the original deletes the data. The data does not go away until all hardlinks are deleted.

Disadvantages
Hard links normally require that the link and the file reside in the same file system.
Only the super user can create a hard link to a directory (when supported by the underlying file system).

ln blah1 blah1-hard eg

30
Q

Which attributes of a file are shown when ls -l command is executed

A
permission modes
no of links
owner
group
size
date(modified)
file name
31
Q

What are soft links

A

A symbolic link is an indirect pointer to a file, There are no file system limitations on a symbolic link and what it points to.

Anyone can create a symbolic link to a directory. Symbolic links are typically used to ‘‘move’’ a file or an entire directory hierarchy to another location on a system.

Symbolic or soft links: there are two inodes: one contains the actual data of the file, the other serves as a pointer to the first file, containing only the first file’s name.

Symbolic links can be used across file systems, even on different computers.

They are used on almost every Unix system for their flexibility.

ln –s is used to create symbolic link.

ln -s blah2 blah2-soft eg

32
Q

syntax and purpose of utime

A

utime( ): changes the access and modification times of the inode specified by filename to the actime and modtime fields of buf respectively.

Syntax:
int utime(const char *filename, const struct utimbuf *buf); 

The structure used by this function is:

struct utimbuf 
{ 
time_t actime; /* access time */
 time_t modtime; /* modification time */
 };
  • If times is a null pointer , the access time and modification time are both set to the current time. To do this either the effective user ID of the process must equal the owner ID of the file or the process must have write permission for the file.
  • If times is a non-null pointer the access time and the modification time are set to the values in the structure pointed to by times.
program
struct utimbuf timebuf;
timebuf.actime=statbuf.st_atime;
timebuf.modtime= stat.st_mtime;
if(utime(argv[i], &timebuf)<0){
printf(“utime error”);
continue;
}
33
Q

program to read a directory

A
#include 
Int main(int argc, char *argv[])
{
DIR *dp; 
struct dirent *dirp;

if (argc != 2)
printf(“usage: ls directory_name”);

if ((dp = opendir(argv[1])) == NULL)
printf(“can’t open %s”, argv[1]);

while ((dirp = readdir(dp)) != NULL)
printf(“%s\n”, dirp->d_name);

closedir(dp); exit(0);

}