Sync from coreutils.
[gnulib.git] / lib / mkdirat.c
1 /* fd-relative mkdir
2    Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* written by Jim Meyering */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "openat.h"
25
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30
31 #include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
32 #include "save-cwd.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 #include "openat-priv.h"
38
39 /* Solaris 10 has no function like this.
40    Create a subdirectory, FILE, with mode MODE, in the directory
41    open on descriptor FD.  If possible, do it without changing the
42    working directory.  Otherwise, resort to using save_cwd/fchdir,
43    then mkdir/restore_cwd.  If either the save_cwd or the restore_cwd
44    fails, then give a diagnostic and exit nonzero.  */
45 int
46 mkdirat (int fd, char const *file, mode_t mode)
47 {
48   struct saved_cwd saved_cwd;
49   int saved_errno;
50   int err;
51
52   if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
53     return mkdir (file, mode);
54
55   {
56     char *proc_file;
57     BUILD_PROC_NAME (proc_file, fd, file);
58     err = mkdir (proc_file, mode);
59     /* If the syscall succeeds, or if it fails with an unexpected
60        errno value, then return right away.  Otherwise, fall through
61        and resort to using save_cwd/restore_cwd.  */
62     if (0 <= err || ! EXPECTED_ERRNO (errno))
63       return err;
64   }
65
66   if (save_cwd (&saved_cwd) != 0)
67     openat_save_fail (errno);
68
69   if (fchdir (fd) != 0)
70     {
71       saved_errno = errno;
72       free_cwd (&saved_cwd);
73       errno = saved_errno;
74       return -1;
75     }
76
77   err = mkdir (file, mode);
78   saved_errno = (err < 0 ? errno : 0);
79
80   if (restore_cwd (&saved_cwd) != 0)
81     openat_restore_fail (errno);
82
83   free_cwd (&saved_cwd);
84
85   if (saved_errno)
86     errno = saved_errno;
87   return err;
88 }