summaryrefslogtreecommitdiffstats
path: root/libopkg/file_util.c
blob: 82578bd696723029b81a4da43ed8866b0d70e563 (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
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
/* file_util.c - convenience routines for common stat operations

   Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>

   Carl D. Worth
   Copyright (C) 2001 University of Southern California

   This program 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, or (at
   your option) any later version.

   This program 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.
*/

#include "config.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

#include "sprintf_alloc.h"
#include "file_util.h"
#include "md5.h"
#include "libbb/libbb.h"

#if defined HAVE_SHA256
#include "sha256.h"
#endif

int
file_exists(const char *file_name)
{
	struct stat st;

	if (stat(file_name, &st) == -1)
		return 0;

	return 1;
}

int
file_is_dir(const char *file_name)
{
	struct stat st;

	if (stat(file_name, &st) == -1)
		return 0;

	return S_ISDIR(st.st_mode);
}

int
file_is_symlink(const char *file_name)
{
	struct stat st;

	if (lstat(file_name, &st) == -1)
		return 0;

	return S_ISLNK(st.st_mode);
}

/* read a single line from a file, stopping at a newline or EOF.
   If a newline is read, it will appear in the resulting string.
   Return value is a malloc'ed char * which should be freed at
   some point by the caller.

   Return value is NULL if the file is at EOF when called.
*/
char *
file_read_line_alloc(FILE *fp)
{
	char buf[BUFSIZ];
	unsigned int buf_len;
	char *line = NULL;
	unsigned int line_size = 0;
	int got_nl = 0;

	buf[0] = '\0';

	while (fgets(buf, BUFSIZ, fp)) {
		buf_len = strlen(buf);
		if (buf[buf_len - 1] == '\n') {
			buf_len--;
			buf[buf_len] = '\0';
			got_nl = 1;
		}
		if (line) {
			line_size += buf_len;
			line = xrealloc(line, line_size+1);
			strncat(line, buf, line_size);
		} else {
			line_size = buf_len + 1;
			line = xstrdup(buf);
		}
		if (got_nl)
			break;
	}

	return line;
}

int
file_move(const char *src, const char *dest)
{
	int err;

	err = rename(src, dest);
	if (err == -1) {
		if (errno == EXDEV) {
			/* src & dest live on different file systems */
			err = file_copy(src, dest);
			if (err == 0)
				unlink(src);
		} else {
			opkg_perror(ERROR, "Failed to rename %s to %s",
				src, dest);
		}
	}

	return err;
}

int
file_copy(const char *src, const char *dest)
{
	int err;

	err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
	if (err)
		opkg_msg(ERROR, "Failed to copy file %s to %s.\n",
				src, dest);

	return err;
}

int
file_mkdir_hier(const char *path, long mode)
{
	return make_directory(path, mode, FILEUTILS_RECUR);
}

char *file_md5sum_alloc(const char *file_name)
{
    static const int md5sum_bin_len = 16;
    static const int md5sum_hex_len = 32;

    static const unsigned char bin2hex[16] = {
	'0', '1', '2', '3',
	'4', '5', '6', '7',
	'8', '9', 'a', 'b',
	'c', 'd', 'e', 'f'
    };

    int i, err;
    FILE *file;
    char *md5sum_hex;
    unsigned char md5sum_bin[md5sum_bin_len];

    md5sum_hex = xcalloc(1, md5sum_hex_len + 1);

    file = fopen(file_name, "r");
    if (file == NULL) {
	opkg_perror(ERROR, "Failed to open file %s", file_name);
	free(md5sum_hex);
	return NULL;
    }

    err = md5_stream(file, md5sum_bin);
    if (err) {
	opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
	fclose(file);
	free(md5sum_hex);
	return NULL;
    }

    fclose(file);

    for (i=0; i < md5sum_bin_len; i++) {
	md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
	md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
    }

    md5sum_hex[md5sum_hex_len] = '\0';

    return md5sum_hex;
}

#ifdef HAVE_SHA256
char *file_sha256sum_alloc(const char *file_name)
{
    static const int sha256sum_bin_len = 32;
    static const int sha256sum_hex_len = 64;

    static const unsigned char bin2hex[16] = {
	'0', '1', '2', '3',
	'4', '5', '6', '7',
	'8', '9', 'a', 'b',
	'c', 'd', 'e', 'f'
    };

    int i, err;
    FILE *file;
    char *sha256sum_hex;
    unsigned char sha256sum_bin[sha256sum_bin_len];

    sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);

    file = fopen(file_name, "r");
    if (file == NULL) {
	opkg_perror(ERROR, "Failed to open file %s", file_name);
	free(sha256sum_hex);
	return NULL;
    }

    err = sha256_stream(file, sha256sum_bin);
    if (err) {
	opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", file_name);
	fclose(file);
	free(sha256sum_hex);
	return NULL;
    }

    fclose(file);

    for (i=0; i < sha256sum_bin_len; i++) {
	sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4];
	sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf];
    }

    sha256sum_hex[sha256sum_hex_len] = '\0';

    return sha256sum_hex;
}

#endif


int
rm_r(const char *path)
{
	int ret = 0;
	DIR *dir;
	struct dirent *dent;

	if (path == NULL) {
		opkg_perror(ERROR, "Missing directory parameter");
		return -1;
	}

	dir = opendir(path);
	if (dir == NULL) {
		opkg_perror(ERROR, "Failed to open dir %s", path);
		return -1;
	}

	if (fchdir(dirfd(dir)) == -1) {
		opkg_perror(ERROR, "Failed to change to dir %s", path);
		closedir(dir);
		return -1;
	}

	while (1) {
		errno = 0;
		if ((dent = readdir(dir)) == NULL) {
			if (errno) {
				opkg_perror(ERROR, "Failed to read dir %s",
						path);
				ret = -1;
			}
			break;
		}

		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
			continue;

#ifdef _BSD_SOURCE
		if (dent->d_type == DT_DIR) {
			if ((ret = rm_r(dent->d_name)) == -1)
				break;
			continue;
		} else if (dent->d_type == DT_UNKNOWN)
#endif
		{
			struct stat st;
			if ((ret = lstat(dent->d_name, &st)) == -1) {
				opkg_perror(ERROR, "Failed to lstat %s",
						dent->d_name);
				break;
			}
			if (S_ISDIR(st.st_mode)) {
				if ((ret = rm_r(dent->d_name)) == -1)
					break;
				continue;
			}
		}

		if ((ret = unlink(dent->d_name)) == -1) {
			opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
			break;
		}
	}

	if (chdir("..") == -1) {
		ret = -1;
		opkg_perror(ERROR, "Failed to change to dir %s/..", path);
	}

	if (rmdir(path) == -1 ) {
		ret = -1;
		opkg_perror(ERROR, "Failed to remove dir %s", path);
	}

	if (closedir(dir) == -1) {
		ret = -1;
		opkg_perror(ERROR, "Failed to close dir %s", path);
	}

	return ret;
}