File Accessibility and Directory Linux

Embed Size (px)

Citation preview

  • 8/3/2019 File Accessibility and Directory Linux

    1/7

    access - check user's permissions for a file

    #include

    int access(const char *pathname, int mode);

    DESCRIPTION

    access checks whether the process would be allowed to read, write or test for existence of the file

    (or other file system object) whose name is pathname. Ifpathname is a symbolic link

    permissions of the file referred to by this symbolic link are tested.

    mode is a mask consisting of one or more ofR_OK, W_OK, X_OK and F_OK.

    R_OK, W_OK and X_OK request checking whether the file exists and has read, write and

    execute permissions, respectively. F_OK just requests checking for the existence of the file.

    The tests depend on the permissions of the directories occurring in the path to the file, as given

    in pathname, and on the permissions of directories and files referred to by symbolic links

    encountered on the way.

    The check is done with the process's real uid and gid, rather than with the effective ids as is done

    when actually attempting an operation. This is to allow set-UID programs to easily determine the

    invoking user's authority.

    Only access bits are checked, not the file type or contents. Therefore, if a directory is found to be

    "writable," it probably means that files can be created in the directory, and not that the directorycan be written as a file. If the process has appropriate privileges, an implementation may indicate

    success forX_OK even if none of the execute file permission bits are set.

    RETURN VALUE

    On success (all requested permissions granted), zero is returned. On error (at least one bit

    in mode asked for a permission that is denied, or some other error occurred), -1 is returned,

    and errno is set appropriately.

    ERRORS

    access shall fail if:

    EACCES

    http://usr/include/unistd.hhttp://usr/include/unistd.h
  • 8/3/2019 File Accessibility and Directory Linux

    2/7

    The requested access would be denied to the file or search permission is denied to one of

    the directories in pathname.

    ELOOPToo many symbolic links were encountered in resolving pathname.

    ENAMETOOLONGpathname is too long.

    ENOENTA directory component in pathname would have been accessible but does not exist or wasa dangling symbolic link.

    ENOTDIRA component used as a directory in pathname is not, in fact, a directory.

    EROFSWrite permission was requested for a file on a read-only filesystem.

    access may fail if:

    EFAULTpathname points outside your accessible address space.

    EINVAL

    mode was incorrectly specified.

    EIOAn I/O error occurred.

    ENOMEMInsufficient kernel memory was available.

    ETXTBSYWrite access was requested to an executable which is being executed.

    chdir

    #include int chdir(const char *path);

    int fchdir(int fd);

    http://usr/include/unistd.hhttp://usr/include/unistd.h
  • 8/3/2019 File Accessibility and Directory Linux

    3/7

    DESCRIPTION

    chdir changes the current directory to that specified in path.

    fchdir is identical to chdir, only 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.

    ERRORS

    Depending on the file system, other errors can be returned. The more general errors for chdir are

    listed below:

    EFAULT

    path points outside your accessible address space.

    ENAMETOOLONGpath is too long.

    ENOENTThe file does not exist.

    ENOMEMInsufficient kernel memory was available.

    ENOTDIRA component ofpath is not a directory.

    EACCESSearch permission is denied on a component ofpath.

    ELOOPToo many symbolic links were encountered in resolving path.

    EIOAn I/O error occurred.

    The general errors for fchdir are listed below:

    EBADFfdis not a valid file descriptor.

  • 8/3/2019 File Accessibility and Directory Linux

    4/7

    EACCESSearch permission was denied on the directory open on fd.

    Chroot

    #include int chroot(const char *path);

    DESCRIPTION

    chroot changes the root directory to that specified in path. This directory will be used for path

    names beginning with /. The root directory is inherited by all children of the current process.

    Only the super-user may change the root directory.

    RETURN VALUE

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

    ERRORS

    Depending on the file system, other errors can be returned. The more general errors are listed

    below:

    EPERMThe effective UID is not zero.

    EFAULTpath points outside your accessible address space.

    ENAMETOOLONGpath is too long.

    ENOENTThe file does not exist.

    ENOMEMInsufficient kernel memory was available.

    ENOTDIRA component ofpath is not a directory.

    http://usr/include/unistd.hhttp://usr/include/unistd.h
  • 8/3/2019 File Accessibility and Directory Linux

    5/7

    EACCESSearch permission is denied on a component of the path prefix.

    ELOOPToo many symbolic links were encountered in resolving path.

    EIOAn I/O error occurred.

    stat

    #include

    #include

    #include int stat(const char *file_name, struct stat *buf);

    DESCRIPTION

    These functions return information about the specified file. You do not need any access rights tothe file to get this information but you need search rights to all directories named in the path

    leading to the file.

    stat states the file pointed to by file_name and fills in buf.

    It returns a statstructure, which contains the following fields:

    struct stat {

    dev_t st_dev; /* device */ino_t st_ino; /* inode */

    mode_t st_mode; /* protection */

    nlink_t st_nlink; /* number of hard links */

    uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */

    dev_t st_rdev; /* device type (if inode device) */

    off_t st_size; /* total size, in bytes */blksize_t st_blksize; /* blocksize for filesystem I/O */

    blkcnt_t st_blocks; /* number of blocks allocated */

    time_t st_atime; /* time of last access */time_t st_mtime; /* time of last modification */

    http://usr/include/sys/types.hhttp://usr/include/sys/stat.hhttp://usr/include/unistd.hhttp://usr/include/unistd.hhttp://usr/include/sys/stat.hhttp://usr/include/sys/types.h
  • 8/3/2019 File Accessibility and Directory Linux

    6/7

    time_t st_ctime; /* time of last change */

    };

    The value st_size gives the size of the file in bytes.

    The value st_blocks gives the size of the file in 512-byte blocks.

    The value st_blksize gives the "preferred" blocksize for efficient file system I/O. (Writing to afile in smaller chunks may cause an inefficient read-modify-rewrite.)

    The field st_atime is changed by file accesses.

    The field st_mtime is changed by file modifications.

    Also st_mtime of a directory is changed by the creation or deletion of files in that directory.

    The st_mtime field is notchanged for changes in owner, group, hard link count, or mode.

    The field st_ctime is changed by writing or by setting inode information (i.e., owner, group, link

    count, mode, etc.).

    The following POSIX macros are defined to check the file type:

    S_ISREG(m)is it a regular file?

    S_ISDIR(m)

    directory?

    S_ISCHR(m)character device?

    S_ISBLK(m)block device?

    S_ISFIFO(m)fifo?

    S_ISLNK(m)symbolic link?

    S_ISSOCK(m)socket? (Not in POSIX.1-1996.

  • 8/3/2019 File Accessibility and Directory Linux

    7/7

    There are many flags defined for the st_mode field. Some of them are:

    S_IFMT bitmask for the file type bitfields

    S_IFLNK symbolic link

    RETURN VALUE

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

    ERRORS

    EBADFfiledes is bad.

    ENOENTA component of the path file_name does not exist, or the path is an empty string.

    ENOTDIRA component of the path is not a directory.

    ELOOPToo many symbolic links encountered while traversing the path.

    EFAULT

    Bad address.

    EACCESPermission denied.

    ENOMEMOut of memory (i.e. kernel memory).

    ENAMETOOLONGFile name too long.