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
Syntax and purpose of chmod, fchmod, and fchmodat Functions
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.
Syntax and purpose of chown, fchown, fchownat, and lchown Functions
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.
Which attribute holds the file size and how does file size depend on the type of files (Regular files,Directories, sym link)
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.
Syntax and purpose of truncate, ftruncate functions
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
Explain inode
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.
Syntax and function of link and linkat function
#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
Syntax and purpose of unlink and unlinkat function
#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.
Syntax and purpose of rename and renameat
#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
Explain symbolic links
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
Syntax of symlink
#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
Syntax and purpose of mkdir, mkdirat, rmdir
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);
Syntax and purpose of access function
#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
What is a sticky bit
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
What are Hard links
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
Which attributes of a file are shown when ls -l command is executed
permission modes no of links owner group size date(modified) file name
What are soft links
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
syntax and purpose of utime
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; }
program to read a directory
#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);
}