summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/dir.c
blob: f03aabc11301d4e6c5555cb598fef95add857527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * libdpkg - Debian packaging suite library routines
 * dir.c - directory handling functions
 *
 * Copyright © 2010 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include <dpkg/dpkg.h>
#include <dpkg/i18n.h>
#include <dpkg/dir.h>

static int
dir_make_path_noalloc(char *dirname, mode_t mode)
{
	char *slash;

	/* Find the first slash, and ignore it, as it will be either the
	 * slash for the root directory, for the current directory in a
	 * relative pathname or its parent. */
	slash = strchr(dirname, '/');

	while (slash != NULL) {
		slash = strchr(slash + 1, '/');
		if (slash)
			*slash = '\0';

		if (mkdir(dirname, mode) < 0 && errno != EEXIST)
			return -1;
		if (slash)
			*slash = '/';
	}

	return 0;
}

/**
 * Create the directory and all its parents if necessary.
 *
 * @param path The pathname to create directories for.
 * @param mode The pathname mode.
 */
int
dir_make_path(const char *path, mode_t mode)
{
	char *dirname;
	int rc;

	dirname = m_strdup(path);
	rc = dir_make_path_noalloc(dirname, mode);
	free(dirname);

	return rc;
}

int
dir_make_path_parent(const char *path, mode_t mode)
{
	char *dirname, *slash;
	int rc;

	dirname = m_strdup(path);
	slash = strrchr(dirname, '/');
	if (slash != NULL) {
		*slash = '\0';
		rc = dir_make_path_noalloc(dirname, mode);
	} else {
		rc = -1;
	}
	free(dirname);

	return rc;
}

/**
 * Sync a directory to disk from a DIR structure.
 *
 * @param dir The directory to sync.
 * @param path The name of the directory to sync (for error messages).
 */
static void
dir_sync(DIR *dir, const char *path)
{
#ifdef HAVE_FSYNC_DIR
	int fd;

	fd = dirfd(dir);
	if (fd < 0)
		ohshite(_("unable to get file descriptor for directory '%s'"),
		        path);

	if (fsync(fd))
		ohshite(_("unable to sync directory '%s'"), path);
#endif
}

/**
 * Sync a directory to disk from a pathname.
 *
 * @param path The name of the directory to sync.
 */
void
dir_sync_path(const char *path)
{
#ifdef HAVE_FSYNC_DIR
	DIR *dir;

	dir = opendir(path);
	if (!dir)
		ohshite(_("unable to open directory '%s'"), path);

	dir_sync(dir, path);

	closedir(dir);
#endif
}

/**
 * Sync to disk the parent directory of a pathname.
 *
 * @param path The child pathname of the directory to sync.
 */
void
dir_sync_path_parent(const char *path)
{
#ifdef HAVE_FSYNC_DIR
	char *dirname, *slash;

	dirname = m_strdup(path);

	slash = strrchr(dirname, '/');
	if (slash != NULL) {
		*slash = '\0';
		dir_sync_path(dirname);
	}

	free(dirname);
#endif
}

/* TODO: Ideally we'd use openat() here, to avoid the path mangling, but
 * it's not widely available yet, so this will do for now. */
static void
dir_file_sync(const char *dir, const char *filename)
{
	char *path;
	int fd;

	path = str_fmt("%s/%s", dir, filename);

	fd = open(path, O_WRONLY);
	if (fd < 0)
		ohshite(_("unable to open file '%s'"), path);
	if (fsync(fd))
		ohshite(_("unable to sync file '%s'"), path);
	if (close(fd))
		ohshite(_("unable to close file '%s'"), path);

	free(path);
}

/**
 * Sync to disk the contents and the directory specified.
 *
 * @param path The pathname to sync.
 */
void
dir_sync_contents(const char *path)
{
	DIR *dir;
	struct dirent *dent;

	dir = opendir(path);
	if (!dir)
		ohshite(_("unable to open directory '%s'"), path);

	while ((dent = readdir(dir)) != NULL) {
		if (strcmp(dent->d_name, ".") == 0 ||
		    strcmp(dent->d_name, "..") == 0)
			continue;

		dir_file_sync(path, dent->d_name);
	}

	dir_sync(dir, path);

	closedir(dir);
}