tdb2:tdbtorture: use TEST_DATA_PREFIX for files
[ira/wip.git] / lib / util / util_file.c
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 /**
216 mmap (if possible) or read a file
217 **/
218 _PUBLIC_ void *map_file(const char *fname, size_t size)
219 {
220         size_t s2 = 0;
221         void *p = NULL;
222 #ifdef HAVE_MMAP
223         int fd;
224         fd = open(fname, O_RDONLY, 0);
225         if (fd == -1) {
226                 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
227                 return NULL;
228         }
229         p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
230         close(fd);
231         if (p == MAP_FAILED) {
232                 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
233                 return NULL;
234         }
235 #endif
236         if (!p) {
237                 p = file_load(fname, &s2, 0, NULL);
238                 if (!p) return NULL;
239                 if (s2 != size) {
240                         DEBUG(1,("incorrect size for %s - got %d expected %d\n",
241                                  fname, (int)s2, (int)size));
242                         talloc_free(p);
243                         return NULL;
244                 }
245         }
246
247         return p;
248 }
249
250 /**
251  unmap or free memory
252 **/
253
254 bool unmap_file(void *start, size_t size)
255 {
256 #ifdef HAVE_MMAP
257         if (munmap( start, size ) != 0) {
258                 DEBUG( 1, ("map_file: Failed to unmap address %p "
259                         "of size %u - %s\n", 
260                         start, (unsigned int)size, strerror(errno) ));
261                 return false;
262         }
263         return true;
264 #else
265         talloc_free(start);
266         return true;
267 #endif
268 }
269
270 /**
271 parse a buffer into lines
272 'p' will be freed on error, and otherwise will be made a child of the returned array
273 **/
274 char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
275 {
276         int i;
277         char *s, **ret;
278
279         if (!p) return NULL;
280
281         for (s = p, i=0; s < p+size; s++) {
282                 if (s[0] == '\n') i++;
283         }
284
285         ret = talloc_array(mem_ctx, char *, i+2);
286         if (!ret) {
287                 talloc_free(p);
288                 return NULL;
289         }       
290         
291         talloc_steal(ret, p);
292         
293         memset(ret, 0, sizeof(ret[0])*(i+2));
294
295         ret[0] = p;
296         for (s = p, i=0; s < p+size; s++) {
297                 if (s[0] == '\n') {
298                         s[0] = 0;
299                         i++;
300                         ret[i] = s+1;
301                 }
302                 if (s[0] == '\r') s[0] = 0;
303         }
304
305         /* remove any blank lines at the end */
306         while (i > 0 && ret[i-1][0] == 0) {
307                 i--;
308         }
309
310         if (numlines) *numlines = i;
311
312         return ret;
313 }
314
315
316 /**
317 load a file into memory and return an array of pointers to lines in the file
318 must be freed with talloc_free(). 
319 **/
320 _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
321 {
322         char *p;
323         size_t size;
324
325         p = file_load(fname, &size, maxsize, mem_ctx);
326         if (!p) return NULL;
327
328         return file_lines_parse(p, size, numlines, mem_ctx);
329 }
330
331 /**
332 load a fd into memory and return an array of pointers to lines in the file
333 must be freed with talloc_free(). If convert is true calls unix_to_dos on
334 the list.
335 **/
336 _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
337 {
338         char *p;
339         size_t size;
340
341         p = fd_load(fd, &size, maxsize, mem_ctx);
342         if (!p) return NULL;
343
344         return file_lines_parse(p, size, numlines, mem_ctx);
345 }
346
347
348 /**
349 take a list of lines and modify them to produce a list where \ continues
350 a line
351 **/
352 _PUBLIC_ void file_lines_slashcont(char **lines)
353 {
354         int i, j;
355
356         for (i=0; lines[i];) {
357                 int len = strlen(lines[i]);
358                 if (lines[i][len-1] == '\\') {
359                         lines[i][len-1] = ' ';
360                         if (lines[i+1]) {
361                                 char *p = &lines[i][len];
362                                 while (p < lines[i+1]) *p++ = ' ';
363                                 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
364                         }
365                 } else {
366                         i++;
367                 }
368         }
369 }
370
371 /**
372   save a lump of data into a file. Mostly used for debugging 
373 */
374 _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
375 {
376         int fd;
377         fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
378         if (fd == -1) {
379                 return false;
380         }
381         if (write(fd, packet, length) != (size_t)length) {
382                 close(fd);
383                 return false;
384         }
385         close(fd);
386         return true;
387 }
388
389 _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
390 {
391         char *p;
392         int len, ret;
393         va_list ap2;
394
395         va_copy(ap2, ap);
396         len = vasprintf(&p, format, ap2);
397         va_end(ap2);
398         if (len <= 0) return len;
399         ret = write(fd, p, len);
400         SAFE_FREE(p);
401         return ret;
402 }
403
404 _PUBLIC_ int fdprintf(int fd, const char *format, ...)
405 {
406         va_list ap;
407         int ret;
408
409         va_start(ap, format);
410         ret = vfdprintf(fd, format, ap);
411         va_end(ap);
412         return ret;
413 }
414
415
416 /*
417   try to determine if the filesystem supports large files
418 */
419 _PUBLIC_ bool large_file_support(const char *path)
420 {
421         int fd;
422         ssize_t ret;
423         char c;
424
425         fd = open(path, O_RDWR|O_CREAT, 0600);
426         unlink(path);
427         if (fd == -1) {
428                 /* have to assume large files are OK */
429                 return true;
430         }
431         ret = pread(fd, &c, 1, ((uint64_t)1)<<32);
432         close(fd);
433         return ret == 0;
434 }
435
436
437 /*
438   compare two files, return true if the two files have the same content
439  */
440 bool file_compare(const char *path1, const char *path2)
441 {
442         size_t size1, size2;
443         char *p1, *p2;
444         TALLOC_CTX *mem_ctx = talloc_new(NULL);
445
446         p1 = file_load(path1, &size1, 0, mem_ctx);
447         p2 = file_load(path2, &size2, 0, mem_ctx);
448         if (!p1 || !p2 || size1 != size2) {
449                 talloc_free(mem_ctx);
450                 return false;
451         }
452         if (memcmp(p1, p2, size1) != 0) {
453                 talloc_free(mem_ctx);
454                 return false;
455         }
456         talloc_free(mem_ctx);
457         return true;
458 }