fad4178901f8f9aa47961d2131acfeaa27501830
[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 int file_exists(const char *file_name)
29 {
30     int err;
31     struct stat stat_buf;
32
33     err = stat(file_name, &stat_buf);
34     if (err == 0) {
35         return 1;
36     } else {
37         return 0;
38     }
39 }
40
41 int file_is_dir(const char *file_name)
42 {
43     int err;
44     struct stat stat_buf;
45
46     err = stat(file_name, &stat_buf);
47     if (err) {
48         return 0;
49     }
50
51     return S_ISDIR(stat_buf.st_mode);
52 }
53
54 /* read a single line from a file, stopping at a newline or EOF.
55    If a newline is read, it will appear in the resulting string.
56    Return value is a malloc'ed char * which should be freed at
57    some point by the caller.
58
59    Return value is NULL if the file is at EOF when called.
60 */
61 #define FILE_READ_LINE_BUF_SIZE 1024
62 char *file_read_line_alloc(FILE *file)
63 {
64     char buf[FILE_READ_LINE_BUF_SIZE];
65     int buf_len;
66     char *line = NULL;
67     int line_size = 0;
68
69     memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
70     while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
71         buf_len = strlen(buf);
72         if (line) {
73             line_size += buf_len;
74             line = realloc(line, line_size);
75             if (line == NULL) {
76                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
77                 break;
78             }
79             strcat(line, buf);
80         } else {
81             line_size = buf_len + 1;
82             line = strdup(buf);
83         }
84         if (buf[buf_len - 1] == '\n') {
85             break;
86         }
87     }
88
89     return line;
90 }
91
92 int file_move(const char *src, const char *dest)
93 {
94     int err;
95
96     err = rename(src, dest);
97
98     if (err && errno == EXDEV) {
99         err = file_copy(src, dest);
100         unlink(src);
101     } else if (err) {
102         fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
103                 __FUNCTION__, src, dest, strerror(errno));
104     }
105
106     return err;
107 }
108
109 /* I put these here to keep libbb dependencies from creeping all over
110    the opkg code */
111 int file_copy(const char *src, const char *dest)
112 {
113     int err;
114
115     err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
116     if (err) {
117         fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
118                 __FUNCTION__, src, dest);
119     }
120
121     return err;
122 }
123
124 int file_mkdir_hier(const char *path, long mode)
125 {
126     return make_directory(path, mode, FILEUTILS_RECUR);
127 }
128
129 char *file_md5sum_alloc(const char *file_name)
130 {
131     static const int md5sum_bin_len = 16;
132     static const int md5sum_hex_len = 32;
133
134     static const unsigned char bin2hex[16] = {
135         '0', '1', '2', '3',
136         '4', '5', '6', '7',
137         '8', '9', 'a', 'b',
138         'c', 'd', 'e', 'f'
139     };
140
141     int i, err;
142     FILE *file;
143     char *md5sum_hex;
144     unsigned char md5sum_bin[md5sum_bin_len];
145
146     md5sum_hex = calloc(1, md5sum_hex_len + 1);
147     if (md5sum_hex == NULL) {
148         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
149         return strdup("");
150     }
151
152     file = fopen(file_name, "r");
153     if (file == NULL) {
154         fprintf(stderr, "%s: Failed to open file %s: %s\n",
155                 __FUNCTION__, file_name, strerror(errno));
156         return strdup("");
157     }
158
159     err = md5_stream(file, md5sum_bin);
160     if (err) {
161         fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
162                 __FUNCTION__, file_name, strerror(err));
163         return strdup("");
164     }
165
166     fclose(file);
167
168     for (i=0; i < md5sum_bin_len; i++) {
169         md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
170         md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
171     }
172     
173     md5sum_hex[md5sum_hex_len] = '\0';
174     
175     return md5sum_hex;
176 }
177