home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / macabuse / src / nfclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  5.0 KB  |  232 lines

  1. #include "system.h"
  2. #include "netface.hpp"
  3.  
  4. #include "specs.hpp"
  5. #include "nfserver.hpp"
  6. #include "dprint.hpp"
  7. #include "crc.hpp"
  8. #include "cache.hpp"
  9.  
  10. #include "gserver.hpp"
  11.  
  12. #ifndef __MAC__
  13. #include <sys/types.h>
  14. #endif
  15.  
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <ctype.h>
  19.  
  20. void remove_client(int client_number) { ; }
  21.  
  22. crc_manager *net_crcs=NULL;
  23. extern net_protocol *prot;
  24.  
  25. class nfs_file : public bFILE 
  26. {
  27.   jFILE *local;
  28.   int nfs_fd;
  29.   int offset;
  30.   public :
  31.   nfs_file(char *filename, char *mode);
  32.   virtual int open_failure();
  33.   virtual int unbuffered_read(void *buf, size_t count);       // returns number of bytes read
  34.   int new_read(void *buf, size_t count);       // returns number of bytes read
  35.   virtual int unbuffered_write(void *buf, size_t count);      // returns number of bytes written
  36.   virtual int unbuffered_seek(long offset, int whence);  // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
  37.   virtual int unbuffered_tell();
  38.   virtual int file_size();
  39.   virtual ~nfs_file();
  40. } ;
  41.  
  42. bFILE *open_nfs_file(char *filename,char *mode)
  43. {
  44.   return new nfs_file(filename,mode);
  45. }
  46.  
  47.  
  48. nfs_file::nfs_file(char *filename, char *mode)
  49. {
  50.   local=NULL;
  51.   nfs_fd=-1; 
  52.  
  53.   int local_only=0;
  54.   char *s=mode;
  55.   for (;*s;s++)    // check to see if writeable file, if so don't go through nfs
  56.     if (*s=='w' || *s=='W' || *s=='a' || *s=='A') 
  57.       local_only=1;
  58.  
  59.   char name[256],*c,*f=filename;
  60.   c=name;
  61.   while (*f) { *c=*(f++); *c=toupper(*c); c++; } *c=0;
  62.   if (strstr(name,"REGISTER"))
  63.     local_only=1;
  64.  
  65.   if (net_crcs && !local_only)
  66.   {
  67.     int fail1,fail2,fail3=0;
  68.     char *local_filename=filename;
  69.     if (filename[0]=='/' && filename[1]=='/')
  70.     { local_filename+=2;
  71.       while (*local_filename && *local_filename!='/') local_filename++;
  72.       local_filename++;
  73.     }
  74.  
  75.     int remote_file_num=net_crcs->get_filenumber(local_filename);
  76.     ulong remote_crc=net_crcs->get_crc(remote_file_num,fail2);
  77.     if (!fail2)
  78.     {   
  79.       int local_file_num=crc_man.get_filenumber(local_filename);
  80.       ulong local_crc=crc_man.get_crc(local_file_num,fail1);
  81.       if (fail1)
  82.       {
  83.                 bFILE *fp=new jFILE(local_filename,"rb");      
  84.                 if (!fp->open_failure())
  85.                 {
  86.                   local_crc=crc_file(fp);
  87.                   crc_man.set_crc(local_file_num,local_crc);
  88.                 } else fail3=1;
  89.                 delete fp;    
  90.       }
  91.  
  92.       if (!fail3)
  93.       {
  94.                 if (local_crc==remote_crc)
  95.           local_only=1;    
  96.       }
  97.     }
  98.   }
  99.  
  100.  
  101.   if (local_only)
  102.   {
  103.     local=new jFILE(filename,mode);
  104.     if (local->open_failure()) { delete local; local=NULL; }
  105.   }
  106.   else
  107.   {
  108.  
  109.  
  110.     char nm[256];
  111.     strcpy(nm,filename);
  112.     nfs_fd=NF_open_file(nm,mode);
  113.     if (nfs_fd==-2)
  114.     {
  115.       local=new jFILE(nm,mode);      
  116.       if (local->open_failure()) { delete local; local=NULL; }
  117.       nfs_fd=-1;
  118.     }
  119.   }
  120. }
  121.  
  122.  
  123. int nfs_file::open_failure() 
  124.   if (local==NULL && nfs_fd<0) return 1;
  125.   else return 0;
  126. }
  127.  
  128.  
  129. int nfs_file::unbuffered_read(void *buf, size_t count)      // returns number of bytes read
  130. {
  131.   if (local)
  132.     return local->read(buf,count);
  133.   else if (nfs_fd>=0)
  134.   {
  135.     long a=NF_read(nfs_fd,buf,count);
  136.     if (a>count)
  137.     { 
  138.       dprintf("ooch read too much\n");
  139.     }
  140.     return a;
  141.   }
  142.   else return 0;
  143. }
  144.  
  145. int nfs_file::unbuffered_write(void *buf, size_t count)      // returns number of bytes written
  146. {
  147.   if (local)
  148.     return local->write(buf,count);
  149.   else
  150.   { 
  151.     dprintf("write to nfs file not allowed for now!\n");
  152.     exit(0);
  153.   }  
  154.   return 0;
  155. }
  156.  
  157.  
  158. int nfs_file::unbuffered_seek(long off, int whence) // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
  159. {
  160.   if (local)
  161.     return local->seek(off,whence);
  162.   else if (nfs_fd>=0)
  163.   {
  164.     if (whence!=SEEK_SET)
  165.       dprintf("JC's a fork\n");
  166.     else
  167.       return NF_seek(nfs_fd,off);
  168.   } 
  169.   return 0; 
  170. }
  171.  
  172. int nfs_file::unbuffered_tell()
  173. {
  174.   if (local)          return local->tell();
  175.   else if (nfs_fd>=0) return NF_tell(nfs_fd);
  176.   else                return 0;
  177. }
  178.  
  179.  
  180. int nfs_file::file_size()
  181. {
  182.   if (local)          return local->file_size();
  183.   else if (nfs_fd>=0) return NF_filelength(nfs_fd);
  184.   else                return 0;
  185. }
  186.  
  187. nfs_file::~nfs_file()
  188. {
  189.   flush_writes();
  190.   if (local)          delete local;
  191.   else if (nfs_fd>=0) NF_close(nfs_fd);
  192. }
  193.  
  194. int set_file_server(net_address *addr)
  195. {
  196.   if (NF_set_file_server(addr))
  197.   {
  198.     if (net_crcs)
  199.     {
  200.       net_crcs->clean_up();
  201.       delete net_crcs;
  202.     }
  203.  
  204.     net_crcs=new crc_manager();
  205.     if (!net_crcs->load_crc_file(NET_CRC_FILENAME))
  206.     { 
  207.       delete net_crcs; 
  208.       net_crcs=NULL;
  209.       return 0;
  210.     }
  211.     return 1;
  212.   }
  213.   return 0;
  214. }
  215.  
  216.  
  217. int set_file_server(char *name)
  218. {
  219.   if (prot)
  220.   {
  221.     net_address *addr=prot->get_node_address(name,DEFAULT_COMM_PORT,0);
  222.     if (!addr) { dprintf("\nUnable to locate server\n"); return 0; }
  223.     if (!set_file_server(addr))
  224.     {
  225.       delete addr;
  226.       return 0;
  227.     } else return 1;
  228.   } else return 0;
  229. }
  230.  
  231.