aboutsummaryrefslogtreecommitdiffstats
path: root/src/wgtpkg-workdir.c
blob: e6632f4e5ad541ef37b45bb527846ea9578be5bd (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 Copyright 2015 IoT.bzh

 author: José Bollo <jose.bollo@iot.bzh>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#define _GNU_SOURCE

#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>

#include "verbose.h"
#include "wgtpkg.h"
#include "utils-dir.h"

static const int dirmode = 0755;
char workdir[PATH_MAX] = { 0, };
int workdirfd = -1;

/* removes the working directory */
void remove_workdir()
{
	assert(workdirfd >= 0);
	remove_directory_content_fd(workdirfd);
	close(workdirfd);
	workdirfd = -1;
	rmdir(workdir);
	workdir[0] = 0;
}

static int set_real_workdir(const char *name, int create)
{
	int rc, dirfd;
	size_t length;

	/* check the length */
	length = strlen(name);
	if (length >= sizeof workdir) {
		ERROR("workdir name too long");
		errno = EINVAL;
		return -1;
	}

	/* opens the directory */
	dirfd = openat(AT_FDCWD, name, O_DIRECTORY|O_RDONLY);
	if (dirfd < 0) {
		if (!create || errno != ENOENT) {
			ERROR("no workdir %s", name);
			return -1;
		}
		rc = mkdir(name, dirmode);
		if (rc) {
			ERROR("can't create workdir %s", name);
			return -1;
		}
		dirfd = open(name, O_PATH|O_DIRECTORY);
		if (dirfd < 0) {
			ERROR("can't open workdir %s", name);
			return -1;
		}
	}

	/* close the previous directory if any */
	if (workdirfd >= 0)
		close(workdirfd);
	workdirfd = dirfd;
	memcpy(workdir, name, 1+length);
	return 0;
}

int set_workdir(const char *name, int create)
{
	char *rp;
	int rc;

	if (name[0] == '/')
		return set_real_workdir(name, create);

	rp = realpath(name, NULL);
	if (!rp) {
		ERROR("realpath failed for %s", name);
		return -1;
	}
	rc = set_real_workdir(rp, create);
	free(rp);
	return rc;
}

static int make_real_workdir_base(const char *root, const char *prefix, int reuse)
{
	int i, n, r, l;

	n = snprintf(workdir, sizeof workdir, "%s/%s", root, prefix);
	if (n >= sizeof workdir) {
		ERROR("workdir prefix too long");
		errno = EINVAL;
		return -1;
	}
	r = (int)(sizeof workdir) - n;

	if (workdirfd >= 0)
		close(workdirfd);
	workdirfd = -1;

	/* create a temporary directory */
	for (i = 0 ; ; i++) {
		if (i == INT_MAX) {
			ERROR("exhaustion of workdirs");
			return -1;
		}
		l = snprintf(workdir + n, r, "%d", i);
		if (l >= r) {
			ERROR("computed workdir too long");
			errno = EINVAL;
			return -1;
		}
		if (!mkdir(workdir, dirmode))
			break;
		if (errno != EEXIST) {
			ERROR("error in creation of workdir %s: %m", workdir);
			return -1;
		}
		if (reuse)
			break;
	}
	workdirfd = openat(AT_FDCWD, workdir, O_RDONLY|O_DIRECTORY);
	if (workdirfd < 0) {
		ERROR("error in onnection to workdir %s: %m", workdir);
		rmdir(workdir);
		return -1;
	}

	return 0;
}

int make_workdir_base(const char *root, const char *prefix, int reuse)
{
	char *rp;
	int rc;

	if (root[0] == '/')
		return make_real_workdir_base(root, prefix, reuse);

	rp = realpath(root, NULL);
	if (!rp) {
		ERROR("realpath failed for %s", root);
		return -1;
	}
	rc = make_real_workdir_base(rp, prefix, reuse);
	free(rp);
	return rc;
}

int make_workdir(int reuse)
{
	return make_workdir_base(".", "PACK", reuse);
}

static int move_real_workdir(const char *dest, int parents, int force)
{
	int rc, len;
	struct stat s;
	char *copy;
	const char *iter;

	/* check length */
	if (strlen(dest) >= sizeof workdir) {
		ERROR("destination dirname too long");
		errno = EINVAL;
		return -1;
	}

	/* if an existing directory exist remove it if force */
	rc = stat(dest, &s);
	if (rc == 0) {
		if (!S_ISDIR(s.st_mode)) {
			ERROR("in move_real_workdir, can't overwrite regular file %s", dest);
			errno = EEXIST;
			return -1;
		}
		if (!force) {
			ERROR("in move_real_workdir, can't overwrite regular file %s", dest);
			errno = EEXIST;
			return -1;
		}
		rc = remove_directory_content(dest);
		if (rc) {
			ERROR("in move_real_workdir, can't clean dir %s", dest);
			return rc;
		}
		rc = rmdir(dest);
		if (rc) {
			ERROR("in move_real_workdir, can't remove dir %s", dest);
			return rc;
		}
	} else {
		/* check parent of dest */
		iter = strrchr(dest, '/');
		len = iter ? iter - dest : 0;
		if (len) {
			/* is parent existing? */
			copy = strndupa(dest, len);
			rc = stat(copy, &s);
			if (!rc) {
				/* found an entry */
				if (!S_ISDIR(s.st_mode)) {
					ERROR("in move_real_workdir, '%s' isn't a directory", copy);
					errno = ENOTDIR;
					return -1;
				}
			} else if (!parents) {
				/* parent entry not found but not allowed to create it */
				ERROR("in move_real_workdir, parent directory '%s' not found: %m", copy);
				return -1;
			} else if (create_directory(copy, dirmode, 1)) {
				ERROR("in move_real_workdir, creation of directory %s failed: %m", copy);
				return -1;
			}
		}
	}

	/* try to rename now */
	close(workdirfd);
	workdirfd = -1;
	rc = renameat(AT_FDCWD, workdir, AT_FDCWD, dest);
	if (rc) {
		ERROR("in move_real_workdir, renameat failed %s -> %s: %m", workdir, dest);
		return -1;
	}

	return set_real_workdir(dest, 0);
}

int move_workdir(const char *dest, int parents, int force)
{
	char *rp;
	int rc;

	if (dest[0] == '/')
		return move_real_workdir(dest, parents, force);

	rp = realpath(dest, NULL);
	if (!rp) {
		ERROR("realpath failed for %s", dest);
		return -1;
	}
	rc = move_real_workdir(rp, parents, force);
	free(rp);
	return rc;
}