]> git.cworth.org Git - tar/blob - gnu/fdopendir.c
7488fbb133bcb434dd8909e5b35aa4a9ab188aec
[tar] / gnu / fdopendir.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* provide a replacement fdopendir function
4    Copyright (C) 2004-2010 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 #include <dirent.h>
24
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #if !HAVE_FDOPENDIR
29
30 # include "openat.h"
31 # include "openat-priv.h"
32 # include "save-cwd.h"
33
34 # if GNULIB_DIRENT_SAFER
35 #  include "dirent--.h"
36 # endif
37
38 /* Replacement for Solaris' function by the same name.
39    <http://www.google.com/search?q=fdopendir+site:docs.sun.com>
40    First, try to simulate it via opendir ("/proc/self/fd/FD").  Failing
41    that, simulate it by using fchdir metadata, or by doing
42    save_cwd/fchdir/opendir(".")/restore_cwd.
43    If either the save_cwd or the restore_cwd fails (relatively unlikely),
44    then give a diagnostic and exit nonzero.
45    Otherwise, this function works just like Solaris' fdopendir.
46
47    W A R N I N G:
48    Unlike other fd-related functions, this one effectively consumes
49    its FD parameter.  The caller should not close or otherwise
50    manipulate FD if this function returns successfully.  Also, this
51    implementation does not guarantee that dirfd(fdopendir(n))==n;
52    the open directory stream may use a clone of FD, or have no
53    associated fd at all.  */
54 DIR *
55 fdopendir (int fd)
56 {
57   int saved_errno;
58   DIR *dir;
59
60   char buf[OPENAT_BUFFER_SIZE];
61   char *proc_file = openat_proc_name (buf, fd, ".");
62   if (proc_file)
63     {
64       dir = opendir (proc_file);
65       saved_errno = errno;
66     }
67   else
68     {
69       dir = NULL;
70       saved_errno = EOPNOTSUPP;
71     }
72
73   /* If the syscall fails with an expected errno value, resort to
74      save_cwd/restore_cwd.  */
75   if (! dir && EXPECTED_ERRNO (saved_errno))
76     {
77 # if REPLACE_FCHDIR
78       const char *name = _gl_directory_name (fd);
79       if (name)
80         dir = opendir (name);
81       saved_errno = errno;
82 # else /* !REPLACE_FCHDIR */
83       struct saved_cwd saved_cwd;
84       if (save_cwd (&saved_cwd) != 0)
85         openat_save_fail (errno);
86
87       if (fchdir (fd) != 0)
88         {
89           dir = NULL;
90           saved_errno = errno;
91         }
92       else
93         {
94           dir = opendir (".");
95           saved_errno = errno;
96
97           if (restore_cwd (&saved_cwd) != 0)
98             openat_restore_fail (errno);
99         }
100
101       free_cwd (&saved_cwd);
102 # endif /* !REPLACE_FCHDIR */
103     }
104
105   if (dir)
106     close (fd);
107   if (proc_file != buf)
108     free (proc_file);
109   errno = saved_errno;
110   return dir;
111 }
112
113 #else /* HAVE_FDOPENDIR */
114
115 # include <errno.h>
116 # include <sys/stat.h>
117
118 # undef fdopendir
119
120 /* Like fdopendir, but work around GNU/Hurd bug by validating FD.  */
121
122 DIR *
123 rpl_fdopendir (int fd)
124 {
125   struct stat st;
126   if (fstat (fd, &st))
127     return NULL;
128   if (!S_ISDIR (st.st_mode))
129     {
130       errno = ENOTDIR;
131       return NULL;
132     }
133   return fdopendir (fd);
134 }
135
136 #endif /* HAVE_FDOPENDIR */