po: Update German man pages translation
[dpkg.git] / lib / dpkg / dir.c
blobf03aabc11301d4e6c5555cb598fef95add857527
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * dir.c - directory handling functions
5 * Copyright © 2010 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <unistd.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/i18n.h>
36 #include <dpkg/dir.h>
38 static int
39 dir_make_path_noalloc(char *dirname, mode_t mode)
41 char *slash;
43 /* Find the first slash, and ignore it, as it will be either the
44 * slash for the root directory, for the current directory in a
45 * relative pathname or its parent. */
46 slash = strchr(dirname, '/');
48 while (slash != NULL) {
49 slash = strchr(slash + 1, '/');
50 if (slash)
51 *slash = '\0';
53 if (mkdir(dirname, mode) < 0 && errno != EEXIST)
54 return -1;
55 if (slash)
56 *slash = '/';
59 return 0;
62 /**
63 * Create the directory and all its parents if necessary.
65 * @param path The pathname to create directories for.
66 * @param mode The pathname mode.
68 int
69 dir_make_path(const char *path, mode_t mode)
71 char *dirname;
72 int rc;
74 dirname = m_strdup(path);
75 rc = dir_make_path_noalloc(dirname, mode);
76 free(dirname);
78 return rc;
81 int
82 dir_make_path_parent(const char *path, mode_t mode)
84 char *dirname, *slash;
85 int rc;
87 dirname = m_strdup(path);
88 slash = strrchr(dirname, '/');
89 if (slash != NULL) {
90 *slash = '\0';
91 rc = dir_make_path_noalloc(dirname, mode);
92 } else {
93 rc = -1;
95 free(dirname);
97 return rc;
101 * Sync a directory to disk from a DIR structure.
103 * @param dir The directory to sync.
104 * @param path The name of the directory to sync (for error messages).
106 static void
107 dir_sync(DIR *dir, const char *path)
109 #ifdef HAVE_FSYNC_DIR
110 int fd;
112 fd = dirfd(dir);
113 if (fd < 0)
114 ohshite(_("unable to get file descriptor for directory '%s'"),
115 path);
117 if (fsync(fd))
118 ohshite(_("unable to sync directory '%s'"), path);
119 #endif
123 * Sync a directory to disk from a pathname.
125 * @param path The name of the directory to sync.
127 void
128 dir_sync_path(const char *path)
130 #ifdef HAVE_FSYNC_DIR
131 DIR *dir;
133 dir = opendir(path);
134 if (!dir)
135 ohshite(_("unable to open directory '%s'"), path);
137 dir_sync(dir, path);
139 closedir(dir);
140 #endif
144 * Sync to disk the parent directory of a pathname.
146 * @param path The child pathname of the directory to sync.
148 void
149 dir_sync_path_parent(const char *path)
151 #ifdef HAVE_FSYNC_DIR
152 char *dirname, *slash;
154 dirname = m_strdup(path);
156 slash = strrchr(dirname, '/');
157 if (slash != NULL) {
158 *slash = '\0';
159 dir_sync_path(dirname);
162 free(dirname);
163 #endif
166 /* TODO: Ideally we'd use openat() here, to avoid the path mangling, but
167 * it's not widely available yet, so this will do for now. */
168 static void
169 dir_file_sync(const char *dir, const char *filename)
171 char *path;
172 int fd;
174 path = str_fmt("%s/%s", dir, filename);
176 fd = open(path, O_WRONLY);
177 if (fd < 0)
178 ohshite(_("unable to open file '%s'"), path);
179 if (fsync(fd))
180 ohshite(_("unable to sync file '%s'"), path);
181 if (close(fd))
182 ohshite(_("unable to close file '%s'"), path);
184 free(path);
188 * Sync to disk the contents and the directory specified.
190 * @param path The pathname to sync.
192 void
193 dir_sync_contents(const char *path)
195 DIR *dir;
196 struct dirent *dent;
198 dir = opendir(path);
199 if (!dir)
200 ohshite(_("unable to open directory '%s'"), path);
202 while ((dent = readdir(dir)) != NULL) {
203 if (strcmp(dent->d_name, ".") == 0 ||
204 strcmp(dent->d_name, "..") == 0)
205 continue;
207 dir_file_sync(path, dent->d_name);
210 dir_sync(dir, path);
212 closedir(dir);