s3:smb2_sesssetup: cancel and wait for pending requests on logoff
[Samba.git] / lib / util / util_file.c
blob815cc2bf9e6cda7964cb0ef9f28fa6a29b1d4804
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
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.
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_array(mem_ctx, char *, i+2);
286 if (!ret) {
287 talloc_free(p);
288 return NULL;
291 talloc_steal(ret, p);
293 memset(ret, 0, sizeof(ret[0])*(i+2));
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;
302 if (s[0] == '\r') s[0] = 0;
305 /* remove any blank lines at the end */
306 while (i > 0 && ret[i-1][0] == 0) {
307 i--;
310 if (numlines) *numlines = i;
312 return ret;
317 load a file into memory and return an array of pointers to lines in the file
318 must be freed with talloc_free().
320 _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
322 char *p;
323 size_t size;
325 p = file_load(fname, &size, maxsize, mem_ctx);
326 if (!p) return NULL;
328 return file_lines_parse(p, size, numlines, mem_ctx);
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.
336 _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
338 char *p;
339 size_t size;
341 p = fd_load(fd, &size, maxsize, mem_ctx);
342 if (!p) return NULL;
344 return file_lines_parse(p, size, numlines, mem_ctx);
349 take a list of lines and modify them to produce a list where \ continues
350 a line
352 _PUBLIC_ void file_lines_slashcont(char **lines)
354 int i, j;
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];
365 } else {
366 i++;
371 _PUBLIC_ bool file_save_mode(const char *fname, const void *packet,
372 size_t length, mode_t mode)
374 int fd;
375 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, mode);
376 if (fd == -1) {
377 return false;
379 if (write(fd, packet, length) != (size_t)length) {
380 close(fd);
381 return false;
383 close(fd);
384 return true;
388 save a lump of data into a file. Mostly used for debugging
390 _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
392 return file_save_mode(fname, packet, length, 0644);
395 _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
397 char *p;
398 int len, ret;
399 va_list ap2;
401 va_copy(ap2, ap);
402 len = vasprintf(&p, format, ap2);
403 va_end(ap2);
404 if (len <= 0) return len;
405 ret = write(fd, p, len);
406 SAFE_FREE(p);
407 return ret;
410 _PUBLIC_ int fdprintf(int fd, const char *format, ...)
412 va_list ap;
413 int ret;
415 va_start(ap, format);
416 ret = vfdprintf(fd, format, ap);
417 va_end(ap);
418 return ret;
423 try to determine if the filesystem supports large files
425 _PUBLIC_ bool large_file_support(const char *path)
427 int fd;
428 ssize_t ret;
429 char c;
431 fd = open(path, O_RDWR|O_CREAT, 0600);
432 unlink(path);
433 if (fd == -1) {
434 /* have to assume large files are OK */
435 return true;
437 ret = pread(fd, &c, 1, ((uint64_t)1)<<32);
438 close(fd);
439 return ret == 0;
444 compare two files, return true if the two files have the same content
446 bool file_compare(const char *path1, const char *path2)
448 size_t size1, size2;
449 char *p1, *p2;
450 TALLOC_CTX *mem_ctx = talloc_new(NULL);
452 p1 = file_load(path1, &size1, 0, mem_ctx);
453 p2 = file_load(path2, &size2, 0, mem_ctx);
454 if (!p1 || !p2 || size1 != size2) {
455 talloc_free(mem_ctx);
456 return false;
458 if (memcmp(p1, p2, size1) != 0) {
459 talloc_free(mem_ctx);
460 return false;
462 talloc_free(mem_ctx);
463 return true;