source: trunk/server/lib/util/util_file.c

Last change on this file was 980, checked in by Silvan Scherrer, 9 years ago

samba server: adjust some mmap stuff and fix a Makefile issue

File size: 9.0 KB
Line 
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 "includes.h"
23#include "system/shmem.h"
24#include "system/filesys.h"
25#if _SAMBA_BUILD_ == 3
26#undef malloc
27#undef realloc
28#endif
29
30/**
31 * @file
32 * @brief File-related utility functions
33 */
34
35/**
36read a line from a file with possible \ continuation chars.
37Blanks at the start or end of a line are stripped.
38The string will be allocated if s2 is NULL
39**/
40_PUBLIC_ char *fgets_slash(char *s2,int maxlen,XFILE *f)
41{
42 char *s=s2;
43 int len = 0;
44 int c;
45 bool start_of_line = true;
46
47 if (x_feof(f))
48 return(NULL);
49
50 if (maxlen <2) return(NULL);
51
52 if (!s2)
53 {
54 maxlen = MIN(maxlen,8);
55 s = (char *)malloc(maxlen);
56 }
57
58 if (!s) return(NULL);
59
60 *s = 0;
61
62 while (len < maxlen-1)
63 {
64 c = x_getc(f);
65 switch (c)
66 {
67 case '\r':
68 break;
69 case '\n':
70 while (len > 0 && s[len-1] == ' ')
71 {
72 s[--len] = 0;
73 }
74 if (len > 0 && s[len-1] == '\\')
75 {
76 s[--len] = 0;
77 start_of_line = true;
78 break;
79 }
80 return(s);
81 case EOF:
82 if (len <= 0 && !s2)
83 SAFE_FREE(s);
84 return(len>0?s:NULL);
85 case ' ':
86 if (start_of_line)
87 break;
88 /* fall through */
89 default:
90 start_of_line = false;
91 s[len++] = c;
92 s[len] = 0;
93 }
94 if (!s2 && len > maxlen-3)
95 {
96 char *t;
97
98 maxlen *= 2;
99 t = realloc_p(s, char, maxlen);
100 if (!t) {
101 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
102 SAFE_FREE(s);
103 return(NULL);
104 } else s = t;
105 }
106 }
107 return(s);
108}
109
110/**
111 * Read one line (data until next newline or eof) and allocate it
112 */
113_PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
114{
115 char *data = NULL;
116 ssize_t alloc_size = 0, offset = 0, ret;
117 int p;
118
119 if (hint <= 0) hint = 0x100;
120
121 do {
122 alloc_size += hint;
123
124 data = talloc_realloc(mem_ctx, data, char, alloc_size);
125
126 if (!data)
127 return NULL;
128
129 ret = read(fd, data + offset, hint);
130
131 if (ret == 0) {
132 return NULL;
133 }
134
135 if (ret == -1) {
136 talloc_free(data);
137 return NULL;
138 }
139
140 /* Find newline */
141 for (p = 0; p < ret; p++) {
142 if (data[offset + p] == '\n')
143 break;
144 }
145
146 if (p < ret) {
147 data[offset + p] = '\0';
148
149 /* Go back to position of newline */
150 lseek(fd, p - ret + 1, SEEK_CUR);
151 return data;
152 }
153
154 offset += ret;
155
156 } while (ret == hint);
157
158 data[offset] = '\0';
159
160 return data;
161}
162
163
164/**
165load a file into memory from a fd.
166**/
167_PUBLIC_ char *fd_load(int fd, size_t *psize, size_t maxsize, TALLOC_CTX *mem_ctx)
168{
169 struct stat sbuf;
170 char *p;
171 size_t size;
172
173 if (fstat(fd, &sbuf) != 0) return NULL;
174
175 size = sbuf.st_size;
176
177 if (maxsize) {
178 size = MIN(size, maxsize);
179 }
180
181 p = (char *)talloc_size(mem_ctx, size+1);
182 if (!p) return NULL;
183
184 if (read(fd, p, size) != size) {
185 talloc_free(p);
186 return NULL;
187 }
188 p[size] = 0;
189
190 if (psize) *psize = size;
191
192 return p;
193}
194
195/**
196load a file into memory
197**/
198_PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx)
199{
200 int fd;
201 char *p;
202
203 if (!fname || !*fname) return NULL;
204
205#ifdef __OS2__
206 fd = open(fname, O_RDONLY | O_BINARY);
207#else
208 fd = open(fname,O_RDONLY);
209#endif
210 if (fd == -1) return NULL;
211
212 p = fd_load(fd, size, maxsize, mem_ctx);
213
214 close(fd);
215
216 return p;
217}
218
219
220/**
221mmap (if possible) or read a file
222**/
223_PUBLIC_ void *map_file(const char *fname, size_t size)
224{
225 size_t s2 = 0;
226 void *p = NULL;
227#ifdef HAVE_MMAP
228 int fd;
229#ifdef __OS2__
230 fd = open(fname, O_RDONLY | O_BINARY, 0);
231#else
232 fd = open(fname, O_RDONLY, 0);
233#endif
234 if (fd == -1) {
235 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
236 return NULL;
237 }
238 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
239 close(fd);
240 if (p == MAP_FAILED) {
241 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
242 return NULL;
243 }
244#endif
245 if (!p) {
246 p = file_load(fname, &s2, 0, NULL);
247 if (!p) return NULL;
248 if (s2 != size) {
249 DEBUG(1,("incorrect size for %s - got %d expected %d\n",
250 fname, (int)s2, (int)size));
251 talloc_free(p);
252 return NULL;
253 }
254 }
255
256 return p;
257}
258
259/**
260 unmap or free memory
261**/
262
263bool unmap_file(void *start, size_t size)
264{
265#ifdef HAVE_MMAP
266 if (munmap( start, size ) != 0) {
267 DEBUG( 1, ("map_file: Failed to unmap address %p "
268 "of size %u - %s\n",
269 start, (unsigned int)size, strerror(errno) ));
270 return false;
271 }
272 return true;
273#else
274 talloc_free(start);
275 return true;
276#endif
277}
278
279/**
280parse a buffer into lines
281'p' will be freed on error, and otherwise will be made a child of the returned array
282**/
283char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
284{
285 int i;
286 char *s, **ret;
287
288 if (!p) return NULL;
289
290 for (s = p, i=0; s < p+size; s++) {
291 if (s[0] == '\n') i++;
292 }
293
294 ret = talloc_array(mem_ctx, char *, i+2);
295 if (!ret) {
296 talloc_free(p);
297 return NULL;
298 }
299
300 talloc_steal(ret, p);
301
302 memset(ret, 0, sizeof(ret[0])*(i+2));
303
304 ret[0] = p;
305 for (s = p, i=0; s < p+size; s++) {
306 if (s[0] == '\n') {
307 s[0] = 0;
308 i++;
309 ret[i] = s+1;
310 }
311 if (s[0] == '\r') s[0] = 0;
312 }
313
314 /* remove any blank lines at the end */
315 while (i > 0 && ret[i-1][0] == 0) {
316 i--;
317 }
318
319 if (numlines) *numlines = i;
320
321 return ret;
322}
323
324
325/**
326load a file into memory and return an array of pointers to lines in the file
327must be freed with talloc_free().
328**/
329_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
330{
331 char *p;
332 size_t size;
333
334 p = file_load(fname, &size, maxsize, mem_ctx);
335 if (!p) return NULL;
336
337 return file_lines_parse(p, size, numlines, mem_ctx);
338}
339
340/**
341load a fd into memory and return an array of pointers to lines in the file
342must be freed with talloc_free(). If convert is true calls unix_to_dos on
343the list.
344**/
345_PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
346{
347 char *p;
348 size_t size;
349
350 p = fd_load(fd, &size, maxsize, mem_ctx);
351 if (!p) return NULL;
352
353 return file_lines_parse(p, size, numlines, mem_ctx);
354}
355
356
357/**
358take a list of lines and modify them to produce a list where \ continues
359a line
360**/
361_PUBLIC_ void file_lines_slashcont(char **lines)
362{
363 int i, j;
364
365 for (i=0; lines[i];) {
366 int len = strlen(lines[i]);
367 if (lines[i][len-1] == '\\') {
368 lines[i][len-1] = ' ';
369 if (lines[i+1]) {
370 char *p = &lines[i][len];
371 while (p < lines[i+1]) *p++ = ' ';
372 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
373 }
374 } else {
375 i++;
376 }
377 }
378}
379
380/**
381 save a lump of data into a file. Mostly used for debugging
382*/
383_PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
384{
385 int fd;
386 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
387 if (fd == -1) {
388 return false;
389 }
390 if (write(fd, packet, length) != (size_t)length) {
391 close(fd);
392 return false;
393 }
394 close(fd);
395 return true;
396}
397
398_PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
399{
400 char *p;
401 int len, ret;
402 va_list ap2;
403
404 va_copy(ap2, ap);
405 len = vasprintf(&p, format, ap2);
406 va_end(ap2);
407 if (len <= 0) return len;
408 ret = write(fd, p, len);
409 SAFE_FREE(p);
410 return ret;
411}
412
413_PUBLIC_ int fdprintf(int fd, const char *format, ...)
414{
415 va_list ap;
416 int ret;
417
418 va_start(ap, format);
419 ret = vfdprintf(fd, format, ap);
420 va_end(ap);
421 return ret;
422}
423
424
425/*
426 try to determine if the filesystem supports large files
427*/
428_PUBLIC_ bool large_file_support(const char *path)
429{
430 int fd;
431 ssize_t ret;
432 char c;
433
434 fd = open(path, O_RDWR|O_CREAT, 0600);
435 unlink(path);
436 if (fd == -1) {
437 /* have to assume large files are OK */
438 return true;
439 }
440 ret = pread(fd, &c, 1, ((uint64_t)1)<<32);
441 close(fd);
442 return ret == 0;
443}
444
445
446/*
447 compare two files, return true if the two files have the same content
448 */
449bool file_compare(const char *path1, const char *path2)
450{
451 size_t size1, size2;
452 char *p1, *p2;
453 TALLOC_CTX *mem_ctx = talloc_new(NULL);
454
455 p1 = file_load(path1, &size1, 0, mem_ctx);
456 p2 = file_load(path2, &size2, 0, mem_ctx);
457 if (!p1 || !p2 || size1 != size2) {
458 talloc_free(mem_ctx);
459 return false;
460 }
461 if (memcmp(p1, p2, size1) != 0) {
462 talloc_free(mem_ctx);
463 return false;
464 }
465 talloc_free(mem_ctx);
466 return true;
467}
Note: See TracBrowser for help on using the repository browser.