forked from michaelrsweet/epm
-
Notifications
You must be signed in to change notification settings - Fork 8
/
file.c
384 lines (304 loc) · 10 KB
/
file.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* File functions for the ESP Package Manager (EPM).
*
* Copyright © 2020 by Jim Jagielski
* Copyright 1999-2017 by Michael R Sweet
* Copyright 1999-2010 by Easy Software Products.
*
* 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.
*/
/*
* Include necessary headers...
*/
#include "epm.h"
/*
* 'copy_file()' - Copy a file.
*/
int /* O - 0 on success, -1 on failure */
copy_file(const char *dst, /* I - Destination file */
const char *src, /* I - Source file */
mode_t mode, /* I - Permissions */
uid_t owner, /* I - Owner ID */
gid_t group) /* I - Group ID */
{
FILE *dstfile, /* Destination file */
*srcfile; /* Source file */
char buffer[8192]; /* Copy buffer */
char *slash; /* Pointer to trailing slash */
size_t bytes; /* Number of bytes read/written */
/*
* Check that the destination directory exists...
*/
strlcpy(buffer, dst, sizeof(buffer));
if ((slash = strrchr(buffer, '/')) != NULL)
*slash = '\0';
if (access(buffer, F_OK))
make_directory(buffer, 0755, owner, group);
/*
* Open files...
*/
if ((dstfile = fopen(dst, "wb")) == NULL) {
fprintf(stderr, "epm: Unable to create \"%s\" -\n %s\n", dst,
strerror(errno));
return (-1);
}
if ((srcfile = fopen(src, "rb")) == NULL) {
fclose(dstfile);
unlink(dst);
fprintf(stderr, "epm: Unable to open \"%s\" -\n %s\n", src, strerror(errno));
return (-1);
}
/*
* Copy from src to dst...
*/
while ((bytes = fread(buffer, 1, sizeof(buffer), srcfile)) > 0)
if (fwrite(buffer, 1, bytes, dstfile) < bytes) {
fprintf(stderr, "epm: Unable to write to \"%s\" -\n %s\n", dst,
strerror(errno));
fclose(srcfile);
fclose(dstfile);
unlink(dst);
return (-1);
}
/*
* Close files, change permissions, and return...
*/
fclose(srcfile);
fclose(dstfile);
if (mode)
chmod(dst, mode);
if (!AooMode)
if (owner != (uid_t)-1 && group != (gid_t)-1)
chown(dst, owner, group);
return (0);
}
/*
* 'make_directory()' - Make a directory.
*/
int /* O - 0 = success, -1 = error */
make_directory(const char *directory, /* I - Directory */
mode_t mode, /* I - Permissions */
uid_t owner, /* I - Owner ID */
gid_t group) /* I - Group ID */
{
char buffer[8192], /* Filename buffer */
*bufptr; /* Pointer into buffer */
for (bufptr = buffer; *directory;) {
if (*directory == '/' && bufptr > buffer) {
*bufptr = '\0';
if (access(buffer, F_OK)) {
mkdir(buffer, 0755);
if (mode)
chmod(buffer, mode | 0700);
if (!AooMode)
if (owner != (uid_t)-1 && group != (gid_t)-1)
chown(buffer, owner, group);
}
}
*bufptr++ = *directory++;
}
*bufptr = '\0';
if (access(buffer, F_OK)) {
mkdir(buffer, 0755);
if (mode)
chmod(buffer, mode | 0700);
if (!AooMode)
if (owner != (uid_t)-1 && group != (gid_t)-1)
chown(buffer, owner, group);
}
return (0);
}
/*
* 'make_link()' - Make a symbolic link.
*/
int /* O - 0 = success, -1 = error */
make_link(const char *dst, /* I - Destination file */
const char *src) /* I - Link */
{
char buffer[8192], /* Copy buffer */
*slash; /* Pointer to trailing slash */
/*
* Check that the destination directory exists...
*/
strlcpy(buffer, dst, sizeof(buffer));
if ((slash = strrchr(buffer, '/')) != NULL)
*slash = '\0';
if (access(buffer, F_OK))
make_directory(buffer, 0755, 0, 0);
/*
* Make the symlink...
*/
return (symlink(src, dst));
}
/*
* 'strip_execs()' - Strip symbols from executable files in the distribution.
*/
void strip_execs(dist_t *dist) /* I - Distribution to strip... */
{
int i; /* Looping var */
file_t *file; /* Software file */
FILE *fp; /* File pointer */
char header[4]; /* File header... */
/*
* Loop through the distribution files and strip any executable
* files.
*/
for (i = dist->num_files, file = dist->files; i > 0; i--, file++)
if (tolower(file->type) == 'f' && (file->mode & 0111) &&
strstr(file->options, "nostrip()") == NULL) {
/*
* OK, this file has executable permissions; see if it is a
* script...
*/
if ((fp = fopen(file->src, "rb")) != NULL) {
/*
* Read the first 3 bytes of the file...
*/
if (fread(header, 1, sizeof(header) - 1, fp) == 0)
continue;
header[sizeof(header) - 1] = '\0';
fclose(fp);
/*
* Check for "#!/" or "#" + whitespace at the beginning of the file...
*/
if (!strncmp(header, "#!/", 3) ||
(header[0] == '#' && isspace(header[1] & 255)))
continue;
} else {
/*
* File could not be opened; error out...
*/
fprintf(stderr,
"epm: Unable to open file \"%s\" for destination "
"\"%s\" -\n %s\n",
file->src, file->dst, strerror(errno));
exit(1);
}
/*
* Strip executables...
*/
run_command(NULL, EPM_STRIP " %s", file->src);
}
}
/*
* 'unlink_directory()' - Delete a directory and all of its nodes.
*/
int /* O - 0 on success, -1 on failure */
unlink_directory(const char *directory) /* I - Directory */
{
DIR *dir; /* Directory */
DIRENT *dent; /* Directory entry */
char filename[1024]; /* Filename */
struct stat fileinfo; /* Information on the source file */
/*
* Try opening the source directory...
*/
if ((dir = opendir(directory)) == NULL) {
fprintf(stderr, "epm: Unable to open directory \"%s\": %s\n", directory,
strerror(errno));
return (-1);
}
/*
* Read from the directory...
*/
while ((dent = readdir(dir)) != NULL) {
/*
* Skip "." and ".."...
*/
if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
continue;
/*
* Get file info...
*/
snprintf(filename, sizeof(filename), "%s/%s", directory, dent->d_name);
if (lstat(filename, &fileinfo)) {
fprintf(stderr, "epm: Unable to stat \"%s\": %s\n", filename,
strerror(errno));
continue;
}
/*
* Process accordingly...
*/
if (Verbosity)
puts(filename);
if (S_ISDIR(fileinfo.st_mode)) {
/*
* Directory...
*/
if (unlink_directory(filename))
goto fail;
} else {
/*
* Regular file or symlink...
*/
if (unlink(filename)) {
fprintf(stderr, "epm: Unable to remove \"%s\": %s\n", filename,
strerror(errno));
goto fail;
}
}
}
closedir(dir);
if (rmdir(directory)) {
fprintf(stderr, "epm: Unable to remove \"%s\": %s\n", directory, strerror(errno));
return (-1);
}
return (0);
fail:
closedir(dir);
return (-1);
}
/*
* 'unlink_package()' - Delete a package file.
*/
int /* O - 0 on success, -1 on failure */
unlink_package(const char *ext, /* I - Package filename extension */
const char *prodname, /* I - Product short name */
const char *directory, /* I - Directory for distribution files */
const char *platname, /* I - Platform name */
dist_t *dist, /* I - Distribution information */
const char *subpackage) /* I - Subpackage */
{
char prodfull[255], /* Full name of product */
name[1024], /* Full product name */
filename[1024]; /* File to archive */
/*
* Figure out the full name of the distribution...
*/
if (subpackage)
snprintf(prodfull, sizeof(prodfull), "%s-%s", prodname, subpackage);
else
strlcpy(prodfull, prodname, sizeof(prodfull));
/*
* Then the subdirectory name...
*/
if (dist->release[0])
snprintf(name, sizeof(name), "%s-%s-%s", prodfull, dist->version, dist->release);
else
snprintf(name, sizeof(name), "%s-%s", prodfull, dist->version);
if (platname[0]) {
strlcat(name, "-", sizeof(name));
strlcat(name, platname, sizeof(name));
}
strlcat(name, ".", sizeof(name));
strlcat(name, ext, sizeof(name));
/*
* Remove the package file...
*/
snprintf(filename, sizeof(filename), "%s/%s", directory, name);
if (unlink(filename)) {
fprintf(stderr, "epm: Unable to remove \"%s\": %s\n", filename, strerror(errno));
return (-1);
} else
return (0);
}