summaryrefslogtreecommitdiff
path: root/StdLib/PosixLib/Gen/access.c
blob: 3031e4f9426a558813c8055e9d5d9cf4922b06bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/** @file
  Implementation of the Posix access() function.

  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials are licensed and made available under
  the terms and conditions of the BSD License that accompanies this distribution.
  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include  <LibConfig.h>
#include  <sys/EfiCdefs.h>

#include  <ctype.h>
#include  <errno.h>
#include  <sys/stat.h>
#include  <string.h>
#include  <unistd.h>
#include  <sys/syslimits.h>

/** Save some typing later on. */
#define GOOD_MODE   (F_OK | X_OK | W_OK | R_OK)

/** Determine accessibility of a file.
    The access() function checks the file, named by the pathname pointed to by
    the Path argument, for accessibility according to the bit pattern contained
    in Mode.

    The value of Mode is either the bitwise-inclusive OR of the access
    permissions to be checked (R_OK, W_OK, X_OK) or the existence test (F_OK).

    If Path ends in '/' or '\\', the target must be a directory, otherwise it doesn't matter.
    A file is executable if it is NOT a directory and it ends in ".efi".

    @param[in]    Path    Path or name of the file to be checked.
    @param[in]    Mode    Access permissions to check for.

    @retval   0   Successful completion.
    @retval  -1   File is not accessible with the given Mode.  The error condition
                  is indicated by errno.  Values of errno specific to the access
                  function include: EACCES, ENOENT, ENOTDIR, ENAMETOOLONG
**/
int
access(
  const char *Path,
  int         Mode
  )
{
  struct stat     FileStat;
  int             retval      = -1;
  size_t          PLength;
  uint32_t        WantDir;
  uint32_t        DirMatch;

  if((Path == NULL) || ((Mode & ~GOOD_MODE) != 0)) {
    errno = EINVAL;
  }
  else {
    PLength = strlen(Path);
    if(PLength > PATH_MAX) {
      errno = ENAMETOOLONG;
    }
    else {
      retval = stat(Path, &FileStat);
      if(retval == 0) {
        /* Path exists.  FileStat now holds valid information. */
        WantDir   = isDirSep(Path[PLength - 1]);   // Does Path end in '/' or '\\' ?
        DirMatch = (! WantDir && (! S_ISDIR(FileStat.st_mode)));

        /* Test each permission individually.  */
        do {
          if(Mode == F_OK) {  /* Existence test. */
            if(DirMatch) {    /* This is a directory or file as desired. */
              retval = 0;
            }
            else {
              /* Indicate why we failed the test. */
              errno = (WantDir) ? ENOTDIR : EISDIR;
            }
            break;  /* F_OK does not combine with any other tests. */
          }
          if(Mode & R_OK) {
            if((FileStat.st_mode & READ_PERMS) == 0) {
              /* No read permissions.
                 For UEFI, everything should have READ permissions.
              */
              errno = EDOOFUS;  /* Programming Error. */
              break;
            }
          }
          if(Mode & W_OK) {
            if((FileStat.st_mode & WRITE_PERMS) == 0) {
              /* No write permissions. */
              errno = EACCES;  /* Writing is not OK. */
              break;
            }
          }
          if(Mode & X_OK) {
            /* In EDK II, executable files end in ".efi" */
            if(strcmp(&Path[PLength-4], ".efi") != 0) {
              /* File is not an executable. */
              errno = EACCES;
              break;
            }
          }
          retval = 0;
        } while(FALSE);
      }
      else {
        /* File or path does not exist. */
        errno = ENOENT;
      }
    }
  }
  return retval;
}