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

Last change on this file since 39 was 39, checked in by Sam Hocevar, 15 years ago
  • Fix almost 2,000 warnings by using proper "const" keywords where needed.
File size: 4.9 KB
Line 
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 const *filename, char const *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 const *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 const *filename, char const *mode)
43{
44  return new nfs_file(filename,mode);
45}
46
47
48nfs_file::nfs_file(char const *filename, char const *mode)
49{
50  local=NULL;
51  nfs_fd=-1;
52
53  int local_only=0;
54  char const *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;
60  char const *f = filename;
61  c = name;
62  while (*f) { *c=*(f++); *c=toupper(*c); c++; } *c=0;
63  if (strstr(name,"REGISTER"))
64    local_only=1;
65
66  if (net_crcs && !local_only)
67  {
68    int fail1,fail2,fail3=0;
69    char const *local_filename = filename;
70    if (filename[0]=='/' && filename[1]=='/')
71    { local_filename+=2;
72      while (*local_filename && *local_filename!='/') local_filename++;
73      local_filename++;
74    }
75
76    int remote_file_num=net_crcs->get_filenumber(local_filename);
77    uint32_t remote_crc=net_crcs->get_crc(remote_file_num,fail2);
78    if (!fail2)
79    {   
80      int local_file_num=crc_man.get_filenumber(local_filename);
81      uint32_t local_crc=crc_man.get_crc(local_file_num,fail1);
82      if (fail1)
83      {
84        bFILE *fp=new jFILE(local_filename,"rb");     
85        if (!fp->open_failure())
86        {
87          local_crc=crc_file(fp);
88          crc_man.set_crc(local_file_num,local_crc);
89        } else fail3=1;
90        delete fp;     
91      }
92
93      if (!fail3)
94      {
95        if (local_crc==remote_crc)
96          local_only=1;
97      }
98    }
99  }
100
101
102  if (local_only)
103  {
104    local=new jFILE(filename,mode);
105    if (local->open_failure()) { delete local; local=NULL; }
106  }
107  else
108  {
109
110
111    char nm[256];
112    strcpy(nm,filename);
113    nfs_fd=NF_open_file(nm,mode);
114    if (nfs_fd==-2)
115    {
116      local=new jFILE(nm,mode);     
117      if (local->open_failure()) { delete local; local=NULL; }
118      nfs_fd=-1;
119    }
120  }
121}
122
123
124int nfs_file::open_failure()
125{
126  if (local==NULL && nfs_fd<0) return 1;
127  else return 0;
128}
129
130
131int nfs_file::unbuffered_read(void *buf, size_t count)      // returns number of bytes read
132{
133  if (local)
134    return local->read(buf,count);
135  else if (nfs_fd>=0)
136  {
137    long a=NF_read(nfs_fd,buf,count);
138    if (a>(long)count)
139    {
140      fprintf(stderr,"ooch read too much\n");
141    }
142    return a;
143  }
144  else return 0;
145}
146
147int nfs_file::unbuffered_write(void const *buf, size_t count)      // returns number of bytes written
148{
149  if (local)
150    return local->write(buf,count);
151  else
152  {
153    fprintf(stderr,"write to nfs file not allowed for now!\n");
154    exit(0);
155  } 
156  return 0;
157}
158
159
160int nfs_file::unbuffered_seek(long off, int whence) // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
161{
162  if (local)
163    return local->seek(off,whence);
164  else if (nfs_fd>=0)
165  {
166    if (whence!=SEEK_SET)
167      fprintf(stderr,"JC's a fork\n");
168    else
169      return NF_seek(nfs_fd,off);
170  }
171  return 0;
172}
173
174int nfs_file::unbuffered_tell()
175{
176  if (local)          return local->tell();
177  else if (nfs_fd>=0) return NF_tell(nfs_fd);
178  else                return 0;
179}
180
181
182int nfs_file::file_size()
183{
184  if (local)          return local->file_size();
185  else if (nfs_fd>=0) return NF_filelength(nfs_fd);
186  else                return 0;
187}
188
189nfs_file::~nfs_file()
190{
191  flush_writes();
192  if (local)          delete local;
193  else if (nfs_fd>=0) NF_close(nfs_fd);
194}
195
196int set_file_server(net_address *addr)
197{
198  if (NF_set_file_server(addr))
199  {
200    if (net_crcs)
201    {
202      net_crcs->clean_up();
203      delete net_crcs;
204    }
205
206    net_crcs=new crc_manager();
207    if (!net_crcs->load_crc_file(NET_CRC_FILENAME))
208    {
209      delete net_crcs;
210      net_crcs=NULL;
211      return 0;
212    }
213    return 1;
214  }
215  return 0;
216}
217
218
219int set_file_server(char const *name)
220{
221  if (prot)
222  {
223    net_address *addr=prot->get_node_address(name,DEFAULT_COMM_PORT,0);
224    if (!addr) { dprintf("\nUnable to locate server\n"); return 0; }
225    if (!set_file_server(addr))
226    {
227      delete addr;
228      return 0;
229    } else return 1;
230  } else return 0;
231}
232
Note: See TracBrowser for help on using the repository browser.