1 | /*
|
---|
2 | * Unix SMB/CIFS implementation.
|
---|
3 | * SMB parameters and setup
|
---|
4 | * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
|
---|
5 | *
|
---|
6 | * Added afdgets() Jelmer Vernooij 2005
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or modify it under
|
---|
9 | * the terms of the GNU General Public License as published by the Free
|
---|
10 | * Software Foundation; either version 3 of the License, or (at your option)
|
---|
11 | * any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
16 | * more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License along with
|
---|
19 | * this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "replace.h"
|
---|
23 | #include "system/shmem.h"
|
---|
24 | #include "system/filesys.h"
|
---|
25 | #include <talloc.h>
|
---|
26 | #include "lib/util/samba_util.h"
|
---|
27 | #include "lib/util/debug.h"
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @file
|
---|
31 | * @brief File-related utility functions
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | read a line from a file with possible \ continuation chars.
|
---|
36 | Blanks at the start or end of a line are stripped.
|
---|
37 | The string will be allocated if s2 is NULL
|
---|
38 | **/
|
---|
39 | _PUBLIC_ char *fgets_slash(char *s2,int maxlen,XFILE *f)
|
---|
40 | {
|
---|
41 | char *s=s2;
|
---|
42 | int len = 0;
|
---|
43 | int c;
|
---|
44 | bool start_of_line = true;
|
---|
45 |
|
---|
46 | if (x_feof(f))
|
---|
47 | return(NULL);
|
---|
48 |
|
---|
49 | if (maxlen <2) return(NULL);
|
---|
50 |
|
---|
51 | if (!s2)
|
---|
52 | {
|
---|
53 | maxlen = MIN(maxlen,8);
|
---|
54 | s = (char *)malloc(maxlen);
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (!s) return(NULL);
|
---|
58 |
|
---|
59 | *s = 0;
|
---|
60 |
|
---|
61 | while (len < maxlen-1)
|
---|
62 | {
|
---|
63 | c = x_getc(f);
|
---|
64 | switch (c)
|
---|
65 | {
|
---|
66 | case '\r':
|
---|
67 | break;
|
---|
68 | case '\n':
|
---|
69 | while (len > 0 && s[len-1] == ' ')
|
---|
70 | {
|
---|
71 | s[--len] = 0;
|
---|
72 | }
|
---|
73 | if (len > 0 && s[len-1] == '\\')
|
---|
74 | {
|
---|
75 | s[--len] = 0;
|
---|
76 | start_of_line = true;
|
---|
77 | break;
|
---|
78 | }
|
---|
79 | return(s);
|
---|
80 | case EOF:
|
---|
81 | if (len <= 0 && !s2)
|
---|
82 | SAFE_FREE(s);
|
---|
83 | return(len>0?s:NULL);
|
---|
84 | case ' ':
|
---|
85 | if (start_of_line)
|
---|
86 | break;
|
---|
87 | /* fall through */
|
---|
88 | default:
|
---|
89 | start_of_line = false;
|
---|
90 | s[len++] = c;
|
---|
91 | s[len] = 0;
|
---|
92 | }
|
---|
93 | if (!s2 && len > maxlen-3)
|
---|
94 | {
|
---|
95 | char *t;
|
---|
96 |
|
---|
97 | maxlen *= 2;
|
---|
98 | t = realloc_p(s, char, maxlen);
|
---|
99 | if (!t) {
|
---|
100 | DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
|
---|
101 | SAFE_FREE(s);
|
---|
102 | return(NULL);
|
---|
103 | } else s = t;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return(s);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Read one line (data until next newline or eof) and allocate it
|
---|
111 | */
|
---|
112 | _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
|
---|
113 | {
|
---|
114 | char *data = NULL;
|
---|
115 | ssize_t alloc_size = 0, offset = 0, ret;
|
---|
116 | int p;
|
---|
117 |
|
---|
118 | if (hint <= 0) hint = 0x100;
|
---|
119 |
|
---|
120 | do {
|
---|
121 | alloc_size += hint;
|
---|
122 |
|
---|
123 | data = talloc_realloc(mem_ctx, data, char, alloc_size);
|
---|
124 |
|
---|
125 | if (!data)
|
---|
126 | return NULL;
|
---|
127 |
|
---|
128 | ret = read(fd, data + offset, hint);
|
---|
129 |
|
---|
130 | if (ret == 0) {
|
---|
131 | return NULL;
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (ret == -1) {
|
---|
135 | talloc_free(data);
|
---|
136 | return NULL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* Find newline */
|
---|
140 | for (p = 0; p < ret; p++) {
|
---|
141 | if (data[offset + p] == '\n')
|
---|
142 | break;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (p < ret) {
|
---|
146 | data[offset + p] = '\0';
|
---|
147 |
|
---|
148 | /* Go back to position of newline */
|
---|
149 | lseek(fd, p - ret + 1, SEEK_CUR);
|
---|
150 | return data;
|
---|
151 | }
|
---|
152 |
|
---|
153 | offset += ret;
|
---|
154 |
|
---|
155 | } while (ret == hint);
|
---|
156 |
|
---|
157 | data[offset] = '\0';
|
---|
158 |
|
---|
159 | return data;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | load a file into memory from a fd.
|
---|
165 | **/
|
---|
166 | _PUBLIC_ char *fd_load(int fd, size_t *psize, size_t maxsize, TALLOC_CTX *mem_ctx)
|
---|
167 | {
|
---|
168 | struct stat sbuf;
|
---|
169 | char *p;
|
---|
170 | size_t size;
|
---|
171 |
|
---|
172 | if (fstat(fd, &sbuf) != 0) return NULL;
|
---|
173 |
|
---|
174 | size = sbuf.st_size;
|
---|
175 |
|
---|
176 | if (maxsize) {
|
---|
177 | size = MIN(size, maxsize);
|
---|
178 | }
|
---|
179 |
|
---|
180 | p = (char *)talloc_size(mem_ctx, size+1);
|
---|
181 | if (!p) return NULL;
|
---|
182 |
|
---|
183 | if (read(fd, p, size) != size) {
|
---|
184 | talloc_free(p);
|
---|
185 | return NULL;
|
---|
186 | }
|
---|
187 | p[size] = 0;
|
---|
188 |
|
---|
189 | if (psize) *psize = size;
|
---|
190 |
|
---|
191 | return p;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | load a file into memory
|
---|
196 | **/
|
---|
197 | _PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx)
|
---|
198 | {
|
---|
199 | int fd;
|
---|
200 | char *p;
|
---|
201 |
|
---|
202 | if (!fname || !*fname) return NULL;
|
---|
203 |
|
---|
204 | fd = open(fname,O_RDONLY);
|
---|
205 | if (fd == -1) return NULL;
|
---|
206 |
|
---|
207 | p = fd_load(fd, size, maxsize, mem_ctx);
|
---|
208 |
|
---|
209 | close(fd);
|
---|
210 |
|
---|
211 | return p;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | parse a buffer into lines
|
---|
216 | 'p' will be freed on error, and otherwise will be made a child of the returned array
|
---|
217 | **/
|
---|
218 | char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
|
---|
219 | {
|
---|
220 | int i;
|
---|
221 | char *s, **ret;
|
---|
222 |
|
---|
223 | if (!p) return NULL;
|
---|
224 |
|
---|
225 | for (s = p, i=0; s < p+size; s++) {
|
---|
226 | if (s[0] == '\n') i++;
|
---|
227 | }
|
---|
228 |
|
---|
229 | ret = talloc_zero_array(mem_ctx, char *, i+2);
|
---|
230 | if (!ret) {
|
---|
231 | talloc_free(p);
|
---|
232 | return NULL;
|
---|
233 | }
|
---|
234 |
|
---|
235 | talloc_steal(ret, p);
|
---|
236 |
|
---|
237 | ret[0] = p;
|
---|
238 | for (s = p, i=0; s < p+size; s++) {
|
---|
239 | if (s[0] == '\n') {
|
---|
240 | s[0] = 0;
|
---|
241 | i++;
|
---|
242 | ret[i] = s+1;
|
---|
243 | }
|
---|
244 | if (s[0] == '\r') s[0] = 0;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /* remove any blank lines at the end */
|
---|
248 | while (i > 0 && ret[i-1][0] == 0) {
|
---|
249 | i--;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (numlines) *numlines = i;
|
---|
253 |
|
---|
254 | return ret;
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /**
|
---|
259 | load a file into memory and return an array of pointers to lines in the file
|
---|
260 | must be freed with talloc_free().
|
---|
261 | **/
|
---|
262 | _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
|
---|
263 | {
|
---|
264 | char *p;
|
---|
265 | size_t size;
|
---|
266 |
|
---|
267 | p = file_load(fname, &size, maxsize, mem_ctx);
|
---|
268 | if (!p) return NULL;
|
---|
269 |
|
---|
270 | return file_lines_parse(p, size, numlines, mem_ctx);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | load a fd into memory and return an array of pointers to lines in the file
|
---|
275 | must be freed with talloc_free(). If convert is true calls unix_to_dos on
|
---|
276 | the list.
|
---|
277 | **/
|
---|
278 | _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
|
---|
279 | {
|
---|
280 | char *p;
|
---|
281 | size_t size;
|
---|
282 |
|
---|
283 | p = fd_load(fd, &size, maxsize, mem_ctx);
|
---|
284 | if (!p) return NULL;
|
---|
285 |
|
---|
286 | return file_lines_parse(p, size, numlines, mem_ctx);
|
---|
287 | }
|
---|
288 |
|
---|
289 | _PUBLIC_ bool file_save_mode(const char *fname, const void *packet,
|
---|
290 | size_t length, mode_t mode)
|
---|
291 | {
|
---|
292 | int fd;
|
---|
293 | fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, mode);
|
---|
294 | if (fd == -1) {
|
---|
295 | return false;
|
---|
296 | }
|
---|
297 | if (write(fd, packet, length) != (size_t)length) {
|
---|
298 | close(fd);
|
---|
299 | return false;
|
---|
300 | }
|
---|
301 | close(fd);
|
---|
302 | return true;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | save a lump of data into a file. Mostly used for debugging
|
---|
307 | */
|
---|
308 | _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
|
---|
309 | {
|
---|
310 | return file_save_mode(fname, packet, length, 0644);
|
---|
311 | }
|
---|
312 |
|
---|
313 | _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
|
---|
314 | {
|
---|
315 | char *p;
|
---|
316 | int len, ret;
|
---|
317 | va_list ap2;
|
---|
318 |
|
---|
319 | va_copy(ap2, ap);
|
---|
320 | len = vasprintf(&p, format, ap2);
|
---|
321 | va_end(ap2);
|
---|
322 | if (len <= 0) return len;
|
---|
323 | ret = write(fd, p, len);
|
---|
324 | SAFE_FREE(p);
|
---|
325 | return ret;
|
---|
326 | }
|
---|
327 |
|
---|
328 | _PUBLIC_ int fdprintf(int fd, const char *format, ...)
|
---|
329 | {
|
---|
330 | va_list ap;
|
---|
331 | int ret;
|
---|
332 |
|
---|
333 | va_start(ap, format);
|
---|
334 | ret = vfdprintf(fd, format, ap);
|
---|
335 | va_end(ap);
|
---|
336 | return ret;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | /*
|
---|
341 | compare two files, return true if the two files have the same content
|
---|
342 | */
|
---|
343 | bool file_compare(const char *path1, const char *path2)
|
---|
344 | {
|
---|
345 | size_t size1, size2;
|
---|
346 | char *p1, *p2;
|
---|
347 | TALLOC_CTX *mem_ctx = talloc_new(NULL);
|
---|
348 |
|
---|
349 | p1 = file_load(path1, &size1, 0, mem_ctx);
|
---|
350 | p2 = file_load(path2, &size2, 0, mem_ctx);
|
---|
351 | if (!p1 || !p2 || size1 != size2) {
|
---|
352 | talloc_free(mem_ctx);
|
---|
353 | return false;
|
---|
354 | }
|
---|
355 | if (memcmp(p1, p2, size1) != 0) {
|
---|
356 | talloc_free(mem_ctx);
|
---|
357 | return false;
|
---|
358 | }
|
---|
359 | talloc_free(mem_ctx);
|
---|
360 | return true;
|
---|
361 | }
|
---|