backupkey: Handle more clearly the case where we find the secret, but it has no value
[Samba.git] / lib / util / util_file.c
blobb9d7bdd9d1f96c74e4f4e58b7857a0664865d67d
1 /*
2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
6 * Added afdgets() Jelmer Vernooij 2005
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.
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.
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/>.
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"
29 /**
30 * @file
31 * @brief File-related utility functions
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)
41 char *s=s2;
42 int len = 0;
43 int c;
44 bool start_of_line = true;
46 if (x_feof(f))
47 return(NULL);
49 if (maxlen <2) return(NULL);
51 if (!s2)
53 maxlen = MIN(maxlen,8);
54 s = (char *)malloc(maxlen);
57 if (!s) return(NULL);
59 *s = 0;
61 while (len < maxlen-1)
63 c = x_getc(f);
64 switch (c)
66 case '\r':
67 break;
68 case '\n':
69 while (len > 0 && s[len-1] == ' ')
71 s[--len] = 0;
73 if (len > 0 && s[len-1] == '\\')
75 s[--len] = 0;
76 start_of_line = true;
77 break;
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;
93 if (!s2 && len > maxlen-3)
95 char *t;
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;
106 return(s);
110 * Read one line (data until next newline or eof) and allocate it
112 _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
114 char *data = NULL;
115 ssize_t alloc_size = 0, offset = 0, ret;
116 int p;
118 if (hint <= 0) hint = 0x100;
120 do {
121 alloc_size += hint;
123 data = talloc_realloc(mem_ctx, data, char, alloc_size);
125 if (!data)
126 return NULL;
128 ret = read(fd, data + offset, hint);
130 if (ret == 0) {
131 return NULL;
134 if (ret == -1) {
135 talloc_free(data);
136 return NULL;
139 /* Find newline */
140 for (p = 0; p < ret; p++) {
141 if (data[offset + p] == '\n')
142 break;
145 if (p < ret) {
146 data[offset + p] = '\0';
148 /* Go back to position of newline */
149 lseek(fd, p - ret + 1, SEEK_CUR);
150 return data;
153 offset += ret;
155 } while (ret == hint);
157 data[offset] = '\0';
159 return data;
164 load a file into memory from a fd.
166 _PUBLIC_ char *fd_load(int fd, size_t *psize, size_t maxsize, TALLOC_CTX *mem_ctx)
168 struct stat sbuf;
169 char *p;
170 size_t size;
172 if (fstat(fd, &sbuf) != 0) return NULL;
174 size = sbuf.st_size;
176 if (maxsize) {
177 size = MIN(size, maxsize);
180 p = (char *)talloc_size(mem_ctx, size+1);
181 if (!p) return NULL;
183 if (read(fd, p, size) != size) {
184 talloc_free(p);
185 return NULL;
187 p[size] = 0;
189 if (psize) *psize = size;
191 return p;
195 load a file into memory
197 _PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx)
199 int fd;
200 char *p;
202 if (!fname || !*fname) return NULL;
204 fd = open(fname,O_RDONLY);
205 if (fd == -1) return NULL;
207 p = fd_load(fd, size, maxsize, mem_ctx);
209 close(fd);
211 return p;
216 mmap (if possible) or read a file
218 _PUBLIC_ void *map_file(const char *fname, size_t size)
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;
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;
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;
247 return p;
251 unmap or free memory
254 bool unmap_file(void *start, size_t size)
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;
263 return true;
264 #else
265 talloc_free(start);
266 return true;
267 #endif
271 parse a buffer into lines
272 'p' will be freed on error, and otherwise will be made a child of the returned array
274 char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
276 int i;
277 char *s, **ret;
279 if (!p) return NULL;
281 for (s = p, i=0; s < p+size; s++) {
282 if (s[0] == '\n') i++;
285 ret = talloc_zero_array(mem_ctx, char *, i+2);
286 if (!ret) {
287 talloc_free(p);
288 return NULL;
291 talloc_steal(ret, p);
293 ret[0] = p;
294 for (s = p, i=0; s < p+size; s++) {
295 if (s[0] == '\n') {
296 s[0] = 0;
297 i++;
298 ret[i] = s+1;
300 if (s[0] == '\r') s[0] = 0;
303 /* remove any blank lines at the end */
304 while (i > 0 && ret[i-1][0] == 0) {
305 i--;
308 if (numlines) *numlines = i;
310 return ret;
315 load a file into memory and return an array of pointers to lines in the file
316 must be freed with talloc_free().
318 _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
320 char *p;
321 size_t size;
323 p = file_load(fname, &size, maxsize, mem_ctx);
324 if (!p) return NULL;
326 return file_lines_parse(p, size, numlines, mem_ctx);
330 load a fd into memory and return an array of pointers to lines in the file
331 must be freed with talloc_free(). If convert is true calls unix_to_dos on
332 the list.
334 _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
336 char *p;
337 size_t size;
339 p = fd_load(fd, &size, maxsize, mem_ctx);
340 if (!p) return NULL;
342 return file_lines_parse(p, size, numlines, mem_ctx);
345 _PUBLIC_ bool file_save_mode(const char *fname, const void *packet,
346 size_t length, mode_t mode)
348 int fd;
349 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, mode);
350 if (fd == -1) {
351 return false;
353 if (write(fd, packet, length) != (size_t)length) {
354 close(fd);
355 return false;
357 close(fd);
358 return true;
362 save a lump of data into a file. Mostly used for debugging
364 _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
366 return file_save_mode(fname, packet, length, 0644);
369 _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
371 char *p;
372 int len, ret;
373 va_list ap2;
375 va_copy(ap2, ap);
376 len = vasprintf(&p, format, ap2);
377 va_end(ap2);
378 if (len <= 0) return len;
379 ret = write(fd, p, len);
380 SAFE_FREE(p);
381 return ret;
384 _PUBLIC_ int fdprintf(int fd, const char *format, ...)
386 va_list ap;
387 int ret;
389 va_start(ap, format);
390 ret = vfdprintf(fd, format, ap);
391 va_end(ap);
392 return ret;
397 compare two files, return true if the two files have the same content
399 bool file_compare(const char *path1, const char *path2)
401 size_t size1, size2;
402 char *p1, *p2;
403 TALLOC_CTX *mem_ctx = talloc_new(NULL);
405 p1 = file_load(path1, &size1, 0, mem_ctx);
406 p2 = file_load(path2, &size2, 0, mem_ctx);
407 if (!p1 || !p2 || size1 != size2) {
408 talloc_free(mem_ctx);
409 return false;
411 if (memcmp(p1, p2, size1) != 0) {
412 talloc_free(mem_ctx);
413 return false;
415 talloc_free(mem_ctx);
416 return true;