source: abuse/trunk/src/nfclient.cpp @ 17

Last change on this file since 17 was 17, checked in by Sam Hocevar, 17 years ago
  • absolute shitloads of 64 bit fixes.
File size: 4.8 KB
RevLine 
[2]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#if (defined(__MACH__) || !defined(__APPLE__))
13#include <sys/types.h>
14#endif
15
16#include <fcntl.h>
17#include <unistd.h>
18#include <ctype.h>
19
20void remove_client(int client_number) { ; }
21
22crc_manager *net_crcs=NULL;
23extern net_protocol *prot;
24
25class 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
42bFILE *open_nfs_file(char *filename,char *mode)
43{
44  return new nfs_file(filename,mode);
45}
46
47
48nfs_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);
[17]76    uint32_t remote_crc=net_crcs->get_crc(remote_file_num,fail2);
[2]77    if (!fail2)
78    {   
79      int local_file_num=crc_man.get_filenumber(local_filename);
[17]80      uint32_t local_crc=crc_man.get_crc(local_file_num,fail1);
[2]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
123int nfs_file::open_failure()
124{
125  if (local==NULL && nfs_fd<0) return 1;
126  else return 0;
127}
128
129
130int nfs_file::unbuffered_read(void *buf, size_t count)      // returns number of bytes read
131{
132  if (local)
133    return local->read(buf,count);
134  else if (nfs_fd>=0)
135  {
136    long a=NF_read(nfs_fd,buf,count);
[4]137    if (a>(long)count)
[2]138    {
139      fprintf(stderr,"ooch read too much\n");
140    }
141    return a;
142  }
143  else return 0;
144}
145
146int nfs_file::unbuffered_write(void *buf, size_t count)      // returns number of bytes written
147{
148  if (local)
149    return local->write(buf,count);
150  else
151  {
152    fprintf(stderr,"write to nfs file not allowed for now!\n");
153    exit(0);
154  } 
155  return 0;
156}
157
158
159int nfs_file::unbuffered_seek(long off, int whence) // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
160{
161  if (local)
162    return local->seek(off,whence);
163  else if (nfs_fd>=0)
164  {
165    if (whence!=SEEK_SET)
166      fprintf(stderr,"JC's a fork\n");
167    else
168      return NF_seek(nfs_fd,off);
169  }
170  return 0;
171}
172
173int nfs_file::unbuffered_tell()
174{
175  if (local)          return local->tell();
176  else if (nfs_fd>=0) return NF_tell(nfs_fd);
177  else                return 0;
178}
179
180
181int nfs_file::file_size()
182{
183  if (local)          return local->file_size();
184  else if (nfs_fd>=0) return NF_filelength(nfs_fd);
185  else                return 0;
186}
187
188nfs_file::~nfs_file()
189{
190  flush_writes();
191  if (local)          delete local;
192  else if (nfs_fd>=0) NF_close(nfs_fd);
193}
194
195int set_file_server(net_address *addr)
196{
197  if (NF_set_file_server(addr))
198  {
199    if (net_crcs)
200    {
201      net_crcs->clean_up();
202      delete net_crcs;
203    }
204
205    net_crcs=new crc_manager();
206    if (!net_crcs->load_crc_file(NET_CRC_FILENAME))
207    {
208      delete net_crcs;
209      net_crcs=NULL;
210      return 0;
211    }
212    return 1;
213  }
214  return 0;
215}
216
217
218int set_file_server(char *name)
219{
220  if (prot)
221  {
222    net_address *addr=prot->get_node_address(name,DEFAULT_COMM_PORT,0);
223    if (!addr) { dprintf("\nUnable to locate server\n"); return 0; }
224    if (!set_file_server(addr))
225    {
226      delete addr;
227      return 0;
228    } else return 1;
229  } else return 0;
230}
231
Note: See TracBrowser for help on using the repository browser.