"Fossies" - the Fresh Open Source Software Archive

Member "netbiff-0.9.18/netbiffd-file.c" (21 Sep 2003, 3148 Bytes) of package /linux/privat/old/netbiff-0.9.18.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 #include <stdarg.h>
    5 #include <errno.h>
    6 #include <sys/stat.h>
    7 #include <unistd.h>
    8 
    9 #include "nbproto.h"
   10 
   11 static int use_atime = 1;
   12 
   13 typedef struct _folder Folder;
   14 struct _folder {
   15   char *name;
   16   unsigned long last_size;
   17   int last_seen;
   18   struct _folder *next;
   19 };
   20 
   21 Folder *folders = NULL;
   22 
   23 int update_folder(Folder *f);
   24  
   25 void folder_destroy(Folder *f) {
   26   Folder *next;
   27   while(f) {
   28     if(f->name)
   29       free(f->name);
   30     next = f->next;
   31     free(f);
   32     f = next;
   33   }
   34 }
   35 
   36 int check_folder(Folder *f) {
   37   unsigned long size = f->last_size;
   38 
   39   if(update_folder(f) < 0)
   40     return -1;
   41 
   42   if(size < f->last_size && !f->last_seen)
   43     netbiff_send(UPDATE, "%s", f->name);
   44   else if(size > f->last_size || f->last_seen)
   45     netbiff_send(RESET, "%s", f->name);
   46 
   47   return 0;
   48 }
   49 
   50 int update_folder(Folder *f) {
   51   struct stat statbuf;
   52 
   53   if(lstat(f->name, &statbuf) < 0) {
   54     if(errno == ENOENT) {
   55       f->last_size = 0;
   56       f->last_seen = 1;
   57       return 0;
   58     }
   59     else {
   60       return -1;
   61     }
   62   }
   63 
   64   if(use_atime && (!statbuf.st_atime || !statbuf.st_mtime || 
   65       statbuf.st_mtime < statbuf.st_atime))
   66     f->last_seen = 1;
   67   else
   68     f->last_seen = 0;
   69 
   70   f->last_size = statbuf.st_size;
   71 
   72   return 0;
   73 }
   74 
   75 int main() {
   76   {
   77     const char* x = getenv("NETBIFFD_IGNORE_ATIME");
   78     if(x && atoi(x))
   79       use_atime = 0;
   80   }
   81 
   82   netbiff_send(OK, "Howdy.");
   83 
   84   while(1) {
   85     int cmd;
   86     char *arg[NETBIFF_PROTO_MAXARGS];
   87 
   88     cmd = netbiff_proto_next(arg);
   89 
   90     switch(cmd) {
   91       case NETBIFF_CMD_POLL:
   92         /* Poll for recent folder updates */
   93         if(!arg[0]) {
   94           /* Check all if none given. */
   95           Folder *p;
   96 
   97           for(p = folders; p; p = p->next)
   98             check_folder(p);
   99           netbiff_send(OK, "");
  100         }
  101         else {
  102           Folder *p;
  103           
  104           for(p = folders; p; p = p->next) {
  105             if(!strcasecmp(p->name, arg[0])) {
  106               if(check_folder(p) < 0) 
  107                 netbiff_send(NO, "Error accessing %s: %s", p->name, 
  108                     strerror(errno));
  109               else 
  110                 netbiff_send(OK, "");
  111             break;
  112             }
  113           }
  114           if(!p) 
  115             netbiff_send(NO, "Error accessing %s: no such folder", arg[0]);
  116         }
  117         break;
  118 
  119       case NETBIFF_CMD_FOLDER:
  120         /* Add a new folder to the list */
  121         {
  122           Folder *f;
  123           f = malloc(sizeof(Folder));
  124           if(!f) {
  125             netbiff_send(NO, "Out of memory.");
  126             continue;
  127           }
  128           f->name = strdup(arg[0]);
  129           if(!f->name) {
  130             netbiff_send(NO, "Out of memory.");
  131             free(f);
  132           }
  133           if(update_folder(f) < 0) {
  134             netbiff_send(NO, "Unable to access %s: %s", f->name,
  135                 strerror(errno));
  136             free(f->name);
  137             free(f);
  138           }
  139           else {
  140             f->next = folders;
  141             folders = f;
  142             netbiff_send(OK, "");
  143           }
  144         }
  145         break;
  146 
  147       case NETBIFF_CMD_QUIT:
  148         return 0;
  149 
  150       case NETBIFF_CMD_EOF:
  151         return 1;
  152     }
  153   }
  154 }