]> git.cworth.org Git - tar/blob - lib/fchownat.c
Imported Upstream version 1.20
[tar] / lib / fchownat.c
1 /* This function serves as replacement for a missing fchownat function,
2    as well as a work around for the fchownat bug in glibc-2.4:
3     <http://lists.ubuntu.com/archives/ubuntu-users/2006-September/093218.html>
4    when the buggy fchownat-with-AT_SYMLINK_NOFOLLOW operates on a symlink, it
5    mistakenly affects the symlink referent, rather than the symlink itself.
6
7    Copyright (C) 2006-2007 Free Software Foundation, Inc.
8
9    This program is free software: you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 /* written by Jim Meyering */
23
24 #include <config.h>
25
26 #include "openat.h"
27
28 #include <unistd.h>
29
30 #include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
31 #include "save-cwd.h"
32 #include "openat-priv.h"
33
34 /* Replacement for Solaris' function by the same name.
35    Invoke chown or lchown on file, FILE, using OWNER and GROUP, in the
36    directory open on descriptor FD.  If FLAG is AT_SYMLINK_NOFOLLOW, then
37    use lchown, otherwise, use chown.  If possible, do it without changing
38    the working directory.  Otherwise, resort to using save_cwd/fchdir,
39    then mkdir/restore_cwd.  If either the save_cwd or the restore_cwd
40    fails, then give a diagnostic and exit nonzero.  */
41
42 #define AT_FUNC_NAME fchownat
43 #define AT_FUNC_F1 lchown
44 #define AT_FUNC_F2 chown
45 #define AT_FUNC_USE_F1_COND flag == AT_SYMLINK_NOFOLLOW
46 #define AT_FUNC_POST_FILE_PARAM_DECLS , uid_t owner, gid_t group, int flag
47 #define AT_FUNC_POST_FILE_ARGS        , owner, group
48 #include "at-func.c"