f4325b813bb76ccbb69b73f9c565585bb001df06
[kai/samba.git] / source3 / lib / util_file.c
1 /*
2  * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3  * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  * 
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 675
17  * Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "includes.h"
21
22 extern int DEBUGLEVEL;
23
24 static int gotalarm;
25
26 /***************************************************************
27  Signal function to tell us we timed out.
28 ****************************************************************/
29
30 static void gotalarm_sig(void)
31 {
32   gotalarm = 1;
33 }
34
35 /***************************************************************
36  Lock or unlock a fd for a known lock type. Abandon after waitsecs 
37  seconds.
38 ****************************************************************/
39
40 BOOL do_file_lock(int fd, int waitsecs, int type)
41 {
42   SMB_STRUCT_FLOCK lock;
43   int             ret;
44
45   gotalarm = 0;
46   CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
47
48   lock.l_type = type;
49   lock.l_whence = SEEK_SET;
50   lock.l_start = 0;
51   lock.l_len = 1;
52   lock.l_pid = 0;
53
54   alarm(5);
55   ret = fcntl(fd, SMB_F_SETLKW, &lock);
56   alarm(0);
57   CatchSignal(SIGALRM, SIGNAL_CAST SIG_DFL);
58
59   if (gotalarm) {
60     DEBUG(0, ("do_file_lock: failed to %s file.\n",
61                 type == F_UNLCK ? "unlock" : "lock"));
62     return False;
63   }
64
65   return (ret == 0);
66 }
67
68
69 /***************************************************************
70  Lock an fd. Abandon after waitsecs seconds.
71 ****************************************************************/
72
73 BOOL file_lock(int fd, int type, int secs, int *plock_depth)
74 {
75   if (fd < 0)
76     return False;
77
78   (*plock_depth)++;
79
80   if ((*plock_depth) == 0)
81   {
82     if (!do_file_lock(fd, secs, type)) {
83       DEBUG(10,("file_lock: locking file failed, error = %s.\n",
84                  strerror(errno)));
85       return False;
86     }
87   }
88
89   return True;
90 }
91
92 /***************************************************************
93  Unlock an fd. Abandon after waitsecs seconds.
94 ****************************************************************/
95
96 BOOL file_unlock(int fd, int *plock_depth)
97 {
98   BOOL ret=True;
99
100   if(*plock_depth == 1)
101     ret = do_file_lock(fd, 5, F_UNLCK);
102
103   (*plock_depth)--;
104
105   if(!ret)
106     DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n",
107                  strerror(errno)));
108   return ret;
109 }
110
111 /***************************************************************
112  locks a file for enumeration / modification.
113  update to be set = True if modification is required.
114 ****************************************************************/
115
116 void *startfileent(char *pfile, char *s_readbuf, int bufsize,
117                                 int *file_lock_depth, BOOL update)
118 {
119   FILE *fp = NULL;
120
121   if (!*pfile)
122  {
123     DEBUG(0, ("startfileent: No file set\n"));
124     return (NULL);
125   }
126   DEBUG(10, ("startfileent: opening file %s\n", pfile));
127
128   fp = sys_fopen(pfile, update ? "r+b" : "rb");
129
130   if (fp == NULL) {
131     DEBUG(0, ("startfileent: unable to open file %s\n", pfile));
132     return NULL;
133   }
134
135   /* Set a buffer to do more efficient reads */
136   setvbuf(fp, s_readbuf, _IOFBF, bufsize);
137
138   if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth))
139   {
140     DEBUG(0, ("startfileent: unable to lock file %s\n", pfile));
141     fclose(fp);
142     return NULL;
143   }
144
145   /* Make sure it is only rw by the owner */
146   chmod(pfile, 0600);
147
148   /* We have a lock on the file. */
149   return (void *)fp;
150 }
151
152 /***************************************************************
153  End enumeration of the file.
154 ****************************************************************/
155 void endfileent(void *vp, int *file_lock_depth)
156 {
157   FILE *fp = (FILE *)vp;
158
159   file_unlock(fileno(fp), file_lock_depth);
160   fclose(fp);
161   DEBUG(7, ("endfileent: closed file.\n"));
162 }
163
164 /*************************************************************************
165  Return the current position in the file list as an SMB_BIG_UINT.
166  This must be treated as an opaque token.
167 *************************************************************************/
168 SMB_BIG_UINT getfilepwpos(void *vp)
169 {
170   return (SMB_BIG_UINT)sys_ftell((FILE *)vp);
171 }
172
173 /*************************************************************************
174  Set the current position in the file list from an SMB_BIG_UINT.
175  This must be treated as an opaque token.
176 *************************************************************************/
177 BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok)
178 {
179   return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET);
180 }
181
182 /*************************************************************************
183  gets a line out of a file.
184  lines with "#" at the front are ignored.
185 *************************************************************************/
186 int getfileline(void *vp, char *linebuf, int linebuf_size)
187 {
188         /* Static buffers we will return. */
189         FILE *fp = (FILE *)vp;
190         unsigned char   c;
191         size_t            linebuf_len;
192
193         if (fp == NULL)
194         {
195                 DEBUG(0,("getfileline: Bad file pointer.\n"));
196                 return -1;
197         }
198
199         /*
200          * Scan the file, a line at a time.
201          */
202         while (!feof(fp))
203         {
204                 linebuf[0] = '\0';
205
206                 fgets(linebuf, linebuf_size, fp);
207                 if (ferror(fp))
208                 {
209                         return -1;
210                 }
211
212                 /*
213                  * Check if the string is terminated with a newline - if not
214                  * then we must keep reading and discard until we get one.
215                  */
216
217                 linebuf_len = strlen(linebuf);
218                 if (linebuf[linebuf_len - 1] != '\n')
219                 {
220                         c = '\0';
221                         while (!ferror(fp) && !feof(fp))
222                         {
223                                 c = fgetc(fp);
224                                 if (c == '\n')
225                                 {
226                                         break;
227                                 }
228                         }
229                 }
230                 else
231                 {
232                         linebuf[linebuf_len - 1] = '\0';
233                 }
234
235 #ifdef DEBUG_PASSWORD
236                 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
237 #endif
238                 if ((linebuf[0] == 0) && feof(fp))
239                 {
240                         DEBUG(4, ("getfileline: end of file reached\n"));
241                         return 0;
242                 }
243
244                 if (linebuf[0] == '#' || linebuf[0] == '\0')
245                 {
246                         DEBUG(6, ("getfileline: skipping comment or blank line\n"));
247                         continue;
248                 }
249
250                 return linebuf_len;
251         }
252         return -1;
253 }
254
255
256 /****************************************************************************
257 read a line from a file with possible \ continuation chars. 
258 Blanks at the start or end of a line are stripped.
259 The string will be allocated if s2 is NULL
260 ****************************************************************************/
261 char *fgets_slash(char *s2,int maxlen,FILE *f)
262 {
263   char *s=s2;
264   int len = 0;
265   int c;
266   BOOL start_of_line = True;
267
268   if (feof(f))
269     return(NULL);
270
271   if (!s2)
272     {
273       maxlen = MIN(maxlen,8);
274       s = (char *)Realloc(s,maxlen);
275     }
276
277   if (!s || maxlen < 2) return(NULL);
278
279   *s = 0;
280
281   while (len < maxlen-1)
282     {
283       c = getc(f);
284       switch (c)
285         {
286         case '\r':
287           break;
288         case '\n':
289           while (len > 0 && s[len-1] == ' ')
290             {
291               s[--len] = 0;
292             }
293           if (len > 0 && s[len-1] == '\\')
294             {
295               s[--len] = 0;
296               start_of_line = True;
297               break;
298             }
299           return(s);
300         case EOF:
301           if (len <= 0 && !s2) 
302             free(s);
303           return(len>0?s:NULL);
304         case ' ':
305           if (start_of_line)
306             break;
307         default:
308           start_of_line = False;
309           s[len++] = c;
310           s[len] = 0;
311         }
312       if (!s2 && len > maxlen-3)
313         {
314           maxlen *= 2;
315           s = (char *)Realloc(s,maxlen);
316           if (!s) return(NULL);
317         }
318     }
319   return(s);
320 }
321
322 /****************************************************************************
323 checks if a file has changed since last read
324 ****************************************************************************/
325 BOOL file_modified(const char *filename, time_t *lastmodified)
326 {
327         SMB_STRUCT_STAT st;
328
329         if (sys_stat(filename, &st) != 0)
330         {
331                 DEBUG(0, ("file_changed: Unable to stat file %s. Error was %s\n",
332                           filename, strerror(errno) ));
333                 return False;
334         }
335
336         if(st.st_mtime <= *lastmodified)
337         {
338                 DEBUG(20, ("file_modified: %s not modified\n", filename));
339                 return False;
340         }
341
342         DEBUG(20, ("file_modified: %s modified\n", filename));
343         *lastmodified = st.st_mtime;
344         return True;
345 }
346
347 /***************************************************************************
348 opens a file if modified otherwise returns NULL
349 ***************************************************************************/
350 void *open_file_if_modified(const char *filename, char *mode, time_t *lastmodified)
351 {
352         FILE *f;
353
354         if (!file_modified(filename, lastmodified))
355         {
356                 return NULL;
357         }
358
359         if( (f = fopen(filename, mode)) == NULL)
360         {
361                 DEBUG(0, ("open_file_if_modified: can't open file %s. Error was %s\n",
362                           filename, strerror(errno)));
363                 return NULL;
364         }
365
366         return (void *)f;
367 }