1 /* listfile.c -- run a function in a specific directory
2 Copyright (C) 2007-2021 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 3 of the License, or
7 (at your option) 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, see <https://www.gnu.org/licenses/>.
16 */
17
18 /* This file was written by James Youngman, based on gnulib'c at-func.c.
19 */
20
21 /* config.h must be included first. */
22 #include <config.h>
23
24 /* system headers. */
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <sys/stat.h>
28
29 /* gnulib headers. */
30 #include "fcntl--.h"
31 #include "openat.h"
32 #include "save-cwd.h"
33
34 /* find headers. */
35 #include "dircallback.h"
36
37 int
run_in_dir(const struct saved_cwd * there,int (* callback)(void *),void * usercontext)38 run_in_dir (const struct saved_cwd *there,
39 int (*callback)(void*), void *usercontext)
40 {
41 int err = -1;
42 int saved_errno = 0;
43 struct saved_cwd here;
44 if (0 == save_cwd (&here))
45 {
46 if (0 == restore_cwd (there))
47 {
48 err = callback(usercontext);
49 saved_errno = (err < 0 ? errno : 0);
50 }
51 else
52 {
53 openat_restore_fail (errno);
54 }
55
56 if (restore_cwd (&here) != 0)
57 openat_restore_fail (errno);
58
59 free_cwd (&here);
60 }
61 else
62 {
63 openat_save_fail (errno);
64 }
65 if (saved_errno)
66 errno = saved_errno;
67 return err;
68 }
69
70
71 int
run_in_dirfd(int dir_fd,int (* callback)(void *),void * usercontext)72 run_in_dirfd (int dir_fd, int (*callback)(void*), void *usercontext)
73 {
74 if (dir_fd == AT_FDCWD)
75 {
76 return (*callback)(usercontext);
77 }
78 else
79 {
80 struct saved_cwd saved_cwd;
81 int saved_errno;
82 int err;
83
84 if (save_cwd (&saved_cwd) != 0)
85 openat_save_fail (errno);
86
87 if (fchdir (dir_fd) != 0)
88 {
89 saved_errno = errno;
90 free_cwd (&saved_cwd);
91 errno = saved_errno;
92 return -1;
93 }
94
95 err = (*callback)(usercontext);
96 saved_errno = (err < 0 ? errno : 0);
97
98 if (restore_cwd (&saved_cwd) != 0)
99 openat_restore_fail (errno);
100
101 free_cwd (&saved_cwd);
102
103 if (saved_errno)
104 errno = saved_errno;
105 return err;
106 }
107 }
108