1 /* Target directory operands for coreutils
2 
3    Copyright 2004-2023 Free Software Foundation, Inc.
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17 
18 #include <config.h>
19 
20 #define TARGETDIR_INLINE _GL_EXTERN_INLINE
21 #include <targetdir.h>
22 
23 #include <attribute.h>
24 
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 
30 #ifdef O_PATH
31 enum { O_PATHSEARCH = O_PATH };
32 #else
33 enum { O_PATHSEARCH = O_SEARCH };
34 #endif
35 
36 /* Must F designate the working directory?  */
37 
38 ATTRIBUTE_PURE static bool
must_be_working_directory(char const * f)39 must_be_working_directory (char const *f)
40 {
41   /* Return true for ".", "./.", ".///./", etc.  */
42   while (*f++ == '.')
43     {
44       if (*f != '/')
45         return !*f;
46       while (*++f == '/')
47         continue;
48       if (!*f)
49         return true;
50     }
51   return false;
52 }
53 
54 /* Return a file descriptor open to FILE, for use in openat.
55    As an optimization, return AT_FDCWD if FILE must be the working directory.
56    As a side effect, possibly set *ST to the file's status.
57    Fail and set errno if FILE is not a directory.
58    On failure return -2 if AT_FDCWD is -1, -1 otherwise.  */
59 
60 int
target_directory_operand(char const * file,struct stat * st)61 target_directory_operand (char const *file, struct stat *st)
62 {
63   if (must_be_working_directory (file))
64     return AT_FDCWD;
65 
66   int fd = -1;
67   int try_to_open = 1;
68   int stat_result;
69 
70   /* On old systems without O_DIRECTORY, like Solaris 10, check with
71      stat first lest we try to open a fifo for example and hang.  */
72   if (!O_DIRECTORY)
73     {
74       stat_result = stat (file, st);
75       if (stat_result == 0)
76         {
77           try_to_open = S_ISDIR (st->st_mode);
78           errno = ENOTDIR;
79         }
80       else
81         {
82           /* On EOVERFLOW failure, give up on checking, as there is no
83              easy way to check.  This should be rare.  */
84           try_to_open = errno == EOVERFLOW;
85         }
86     }
87 
88   if (try_to_open)
89     {
90       fd = open (file, O_PATHSEARCH | O_DIRECTORY);
91 
92       /* On platforms lacking O_PATH, using O_SEARCH | O_DIRECTORY to
93          open an overly-protected non-directory can fail with either
94          EACCES or ENOTDIR.  Prefer ENOTDIR as it makes for better
95          diagnostics.  */
96       if (O_PATHSEARCH == O_SEARCH && fd < 0 && errno == EACCES)
97         errno = (((O_DIRECTORY ? stat (file, st) : stat_result) == 0
98                   && !S_ISDIR (st->st_mode))
99                  ? ENOTDIR : EACCES);
100     }
101 
102   if (!O_DIRECTORY && 0 <= fd)
103     {
104       /* On old systems like Solaris 10 double check type,
105          to ensure we've opened a directory.  */
106       int err;
107       if (fstat (fd, st) == 0
108           ? !S_ISDIR (st->st_mode) && (err = ENOTDIR, true)
109           : (err = errno) != EOVERFLOW)
110         {
111           close (fd);
112           errno = err;
113           fd = -1;
114         }
115     }
116 
117   return fd - (AT_FDCWD == -1 && fd < 0);
118 }
119