Unit 1 - Files and directories Flashcards
Syntax and purpose of stat function
#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.
Syntax and purpose of fstat function
#include int fstat(int fd, struct stat *buf );
The fstat function obtains information about the file that is already open on the descriptor fd.
Syntax and purpose of lstat function
#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.
Syntax and purpose of fstatat function
#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.
list and explain the attributes of stat structure
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 */
};
File types in unix
- 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. - 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. - 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.
- 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. - FIFO. A type of file used for communication between processes. It’s sometimes called a named pipe.
- 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.
- Symbolic link. A type of file that points to another file.
List the functions to check for file types for each type of file
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
Different types of IDs about the owner associated to a process
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
Define Real user/group ID
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,
Define Effective user/group ID
The effective user ID, effective group ID, and supplementary group IDs determine our file access permissions,
Define set-user-ID / set-group-ID
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.
List the file access permissions
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
Syntax and purpose of access & faccessat function
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 can be the ownership of a new file or a directly be set
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.
Syntax and purpose of umask
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