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