/[monit]/monit/file.c
ViewVC logotype

Diff of /monit/file.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by martinp, Fri Jan 28 10:39:33 2005 UTC revision 1.5 by martinp, Sat Nov 5 14:09:00 2005 UTC
# Line 55  Line 55 
55  #include <fcntl.h>  #include <fcntl.h>
56  #endif  #endif
57    
58    #ifdef HAVE_DIRENT_H
59    #include <dirent.h>
60    #endif
61    
62  #include "monitor.h"  #include "monitor.h"
63    
64  /**  /**
# Line 315  int File_checkStat(char *filename, char Line 319  int File_checkStat(char *filename, char
319    
320  }  }
321    
322    
323    /**
324     * Check whether the specified directory exist or create it using
325     * specified mode.
326     * @param path The fully qualified path to the directory
327     * @param mode The permission for the directory
328     * @return TRUE if the passed otherwise FALSE
329     */
330    int File_checkQueueDirectory(char *path, mode_t mode) {
331    
332      struct stat st;
333    
334      if(stat(path, &st)) {
335        if(errno == ENOENT) {
336          if(mkdir(path, mode)) {
337            log("%s: cannot create the directory %s -- %s\n", prog, path, STRERROR);
338            return FALSE;
339          }
340        } else {
341          log("%s: cannot read the directory %s -- %s\n", prog, path, STRERROR);
342          return FALSE;
343        }
344      } else if(! S_ISDIR(st.st_mode)) {
345        log("%s: the %s is not the directory\n", prog, path);
346        return FALSE;
347      }
348      return TRUE;
349    }
350    
351    
352    /**
353     * Check the queue size limit.
354     * @param path The fully qualified path to the directory
355     * @param limit The queue limit
356     * @return TRUE if the passed otherwise FALSE
357     */
358    int File_checkQueueLimit(char *path, int limit) {
359    
360      int            used = 0;
361      DIR           *dir = NULL;
362      struct dirent *de = NULL;
363    
364      if(limit <= 0) {
365        log("%s: event queue full\n", prog);
366        return FALSE;
367      }
368    
369      if(! (dir = opendir(path)) ) {
370        log("%s: cannot open the directory %s -- %s\n", prog, path, STRERROR);
371        return FALSE;
372      }
373      while( (de = readdir(dir)) ) {
374        struct stat st;
375    
376        if(!stat(de->d_name, &st) && S_ISREG(st.st_mode) && ++used > limit) {
377          log("%s: event queue full\n", prog);
378          closedir(dir);
379          return FALSE;
380        }
381      }
382      closedir(dir);
383      return TRUE;
384    }
385    
386    
387    /**
388     * Write data to the queue file
389     * @param file Filedescriptor to write to
390     * @param data Data to be written
391     * @param size Size of the data to be written
392     * @return TRUE if the passed otherwise FALSE
393     */
394    int File_writeQueue(FILE *file, void *data, int size) {
395    
396      ASSERT(file);
397    
398      /* write size */
399      if(fwrite(&size, 1, sizeof(int), file) != sizeof(int) || ferror(file))
400        goto error;
401    
402      /* write data if any */
403      if(size > 0)
404        if(fwrite(data, 1, size, file) != size || ferror(file))
405          goto error;
406    
407      return TRUE;
408    
409      error:
410      log("%s: unable to write data to the file\n", prog);
411      return FALSE;
412    }
413    
414    
415    /**
416     * Read the data from the queue file's actual position
417     * @param file Filedescriptor to read from
418     * @param size Size of the data read
419     * @return The data read if any or NULL. The size parameter is set
420     * appropriately.
421     */
422    void *File_readQueue(FILE *file, int *size) {
423    
424      void *data = NULL;
425    
426      ASSERT(file);
427    
428      /* read size */
429      if(fread(size, 1, sizeof(int), file) != sizeof(int) || ferror(file))
430        goto error;
431    
432      /* read data if any */
433      if(*size > 0) {
434        data = xcalloc(1, *size);
435        if(fread(data, 1, *size, file) != *size || ferror(file)) {
436          goto error;
437        }
438      }
439    
440      return data;
441    
442      error:
443      log("%s: unable to read data from the file\n", prog);
444      return FALSE;
445    }
446    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26