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/>.
24 #include <sys/types.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/i18n.h>
39 dir_make_path_noalloc(char *dirname
, mode_t mode
)
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, '/');
53 if (mkdir(dirname
, mode
) < 0 && errno
!= EEXIST
)
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.
69 dir_make_path(const char *path
, mode_t mode
)
74 dirname
= m_strdup(path
);
75 rc
= dir_make_path_noalloc(dirname
, mode
);
82 dir_make_path_parent(const char *path
, mode_t mode
)
84 char *dirname
, *slash
;
87 dirname
= m_strdup(path
);
88 slash
= strrchr(dirname
, '/');
91 rc
= dir_make_path_noalloc(dirname
, mode
);
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).
107 dir_sync(DIR *dir
, const char *path
)
109 #ifdef HAVE_FSYNC_DIR
114 ohshite(_("unable to get file descriptor for directory '%s'"),
118 ohshite(_("unable to sync directory '%s'"), path
);
123 * Sync a directory to disk from a pathname.
125 * @param path The name of the directory to sync.
128 dir_sync_path(const char *path
)
130 #ifdef HAVE_FSYNC_DIR
135 ohshite(_("unable to open directory '%s'"), path
);
144 * Sync to disk the parent directory of a pathname.
146 * @param path The child pathname of the directory to sync.
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
, '/');
159 dir_sync_path(dirname
);
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. */
169 dir_file_sync(const char *dir
, const char *filename
)
174 path
= str_fmt("%s/%s", dir
, filename
);
176 fd
= open(path
, O_WRONLY
);
178 ohshite(_("unable to open file '%s'"), path
);
180 ohshite(_("unable to sync file '%s'"), path
);
182 ohshite(_("unable to close file '%s'"), path
);
188 * Sync to disk the contents and the directory specified.
190 * @param path The pathname to sync.
193 dir_sync_contents(const char *path
)
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)
207 dir_file_sync(path
, dent
->d_name
);