Remove some strdup abuse.
[oweals/opkg-lede.git] / libopkg / file_util.c
1 /* file_util.c - convenience routines for common stat operations
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 #include "sprintf_alloc.h"
23 #include "file_util.h"
24 #include "md5.h"
25 #include "libbb/libbb.h"
26 #undef strlen
27
28 #if defined HAVE_SHA256
29 #include "sha256.h"
30 #endif
31
32 int file_exists(const char *file_name)
33 {
34     int err;
35     struct stat stat_buf;
36
37     err = stat(file_name, &stat_buf);
38     if (err == 0) {
39         return 1;
40     } else {
41         return 0;
42     }
43 }
44
45 int file_is_dir(const char *file_name)
46 {
47     int err;
48     struct stat stat_buf;
49
50     err = stat(file_name, &stat_buf);
51     if (err) {
52         return 0;
53     }
54
55     return S_ISDIR(stat_buf.st_mode);
56 }
57
58 /* read a single line from a file, stopping at a newline or EOF.
59    If a newline is read, it will appear in the resulting string.
60    Return value is a malloc'ed char * which should be freed at
61    some point by the caller.
62
63    Return value is NULL if the file is at EOF when called.
64 */
65 #define FILE_READ_LINE_BUF_SIZE 1024
66 char *file_read_line_alloc(FILE *file)
67 {
68     char buf[FILE_READ_LINE_BUF_SIZE];
69     int buf_len;
70     char *line = NULL;
71     int line_size = 0;
72
73     memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
74     while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
75         buf_len = strlen(buf);
76         if (line) {
77             line_size += buf_len;
78             line = realloc(line, line_size);
79             if (line == NULL) {
80                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
81                 break;
82             }
83             strcat(line, buf);
84         } else {
85             line_size = buf_len + 1;
86             line = xstrdup(buf);
87         }
88         if (buf[buf_len - 1] == '\n') {
89             break;
90         }
91     }
92
93     return line;
94 }
95
96 int file_move(const char *src, const char *dest)
97 {
98     int err;
99
100     err = rename(src, dest);
101
102     if (err && errno == EXDEV) {
103         err = file_copy(src, dest);
104         unlink(src);
105     } else if (err) {
106         fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
107                 __FUNCTION__, src, dest, strerror(errno));
108     }
109
110     return err;
111 }
112
113 /* I put these here to keep libbb dependencies from creeping all over
114    the opkg code */
115 int file_copy(const char *src, const char *dest)
116 {
117     int err;
118
119     err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
120     if (err) {
121         fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
122                 __FUNCTION__, src, dest);
123     }
124
125     return err;
126 }
127
128 int file_mkdir_hier(const char *path, long mode)
129 {
130     return make_directory(path, mode, FILEUTILS_RECUR);
131 }
132
133 char *file_md5sum_alloc(const char *file_name)
134 {
135     static const int md5sum_bin_len = 16;
136     static const int md5sum_hex_len = 32;
137
138     static const unsigned char bin2hex[16] = {
139         '0', '1', '2', '3',
140         '4', '5', '6', '7',
141         '8', '9', 'a', 'b',
142         'c', 'd', 'e', 'f'
143     };
144
145     int i, err;
146     FILE *file;
147     char *md5sum_hex;
148     unsigned char md5sum_bin[md5sum_bin_len];
149
150     md5sum_hex = calloc(1, md5sum_hex_len + 1);
151     if (md5sum_hex == NULL) {
152         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
153         return NULL;
154     }
155
156     file = fopen(file_name, "r");
157     if (file == NULL) {
158         fprintf(stderr, "%s: Failed to open file %s: %s\n",
159                 __FUNCTION__, file_name, strerror(errno));
160         free(md5sum_hex);
161         return NULL;
162     }
163
164     err = md5_stream(file, md5sum_bin);
165     if (err) {
166         fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
167                 __FUNCTION__, file_name, strerror(err));
168         fclose(file);
169         free(md5sum_hex);
170         return NULL;
171     }
172
173     fclose(file);
174
175     for (i=0; i < md5sum_bin_len; i++) {
176         md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
177         md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
178     }
179     
180     md5sum_hex[md5sum_hex_len] = '\0';
181     
182     return md5sum_hex;
183 }
184
185 #ifdef HAVE_SHA256
186 char *file_sha256sum_alloc(const char *file_name)
187 {
188     static const int sha256sum_bin_len = 32;
189     static const int sha256sum_hex_len = 64;
190
191     static const unsigned char bin2hex[16] = {
192         '0', '1', '2', '3',
193         '4', '5', '6', '7',
194         '8', '9', 'a', 'b',
195         'c', 'd', 'e', 'f'
196     };
197
198     int i, err;
199     FILE *file;
200     char *sha256sum_hex;
201     unsigned char sha256sum_bin[sha256sum_bin_len];
202
203     sha256sum_hex = calloc(1, sha256sum_hex_len + 1);
204     if (sha256sum_hex == NULL) {
205         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
206         return NULL;
207     }
208
209     file = fopen(file_name, "r");
210     if (file == NULL) {
211         fprintf(stderr, "%s: Failed to open file %s: %s\n",
212                 __FUNCTION__, file_name, strerror(errno));
213         free(sha256sum_hex);
214         return NULL;
215     }
216
217     err = sha256_stream(file, sha256sum_bin);
218     if (err) {
219         fprintf(stderr, "%s: ERROR computing sha256sum for %s: %s\n",
220                 __FUNCTION__, file_name, strerror(err));
221         fclose(file);
222         free(sha256sum_hex);
223         return NULL;
224     }
225
226     fclose(file);
227
228     for (i=0; i < sha256sum_bin_len; i++) {
229         sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4];
230         sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf];
231     }
232     
233     sha256sum_hex[sha256sum_hex_len] = '\0';
234     
235     return sha256sum_hex;
236 }
237
238 #endif