r1014: Enable horizontal scrolling with the mouse wheel by pressing Ctrl.
[cinelerra_cv/ct.git] / cinelerra / renderfarmfsclient.C
blobc1185bbb3b0e91ef8843d695c4f8a9fa1807e11f
1 // Disable 64 bit indirections so we can override both functions.
2 #undef _LARGEFILE64_SOURCE
3 #undef _LARGEFILE_SOURCE
4 #undef _FILE_OFFSET_BITS
6 #include "mutex.h"
7 #include "renderfarm.h"
8 #include "renderfarmclient.h"
9 #include "renderfarmfsclient.h"
10 #include "renderfarmfsserver.h"
11 #include "units.h"
13 #include <dlfcn.h>
14 #include <fcntl.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
22 #define DEBUG 0
24 // These are hacks to get all the file I/O libraries to transparently 
25 // go over the network without rewriting them.
28 extern "C"
37 RenderFarmFSClient *renderfarm_fs_global = 0;
40 // open doesn't seem overridable
41 // int open (__const char *path, int flags, ...)
42 // {
43 //      static int (*func)(__const char *__file, int __oflag) = 0;
44 //      int result = -1;
45 // printf("open %s\n", path);
46 // 
47 //      if (!func)
48 //      func = (int(*)(const char *path, int flags))dlsym(RTLD_NEXT, "open");
49 //      
50 //      result = (*func)(path, flags);
51 //      return result;
52 // }
55 FILE* fopen(const char *path, const char *mode)
57         static FILE* (*func)(const char *path, const char *mode) = 0;
58 // This pointer is meaningless except on the server.
59         FILE *result = 0;
61         if (!func)
62         func = (FILE*(*)(const char *path, const char *mode))dlsym(RTLD_NEXT, "fopen");
64 // VFS path
65         if(!strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
66         {
67                 renderfarm_fs_global->lock();
68                 result = renderfarm_fs_global->fopen(path, mode);
69                 renderfarm_fs_global->unlock();
70         }
71         else
72                 result = (*func)(path, mode);
74     return result;
77 FILE* fopen64(const char *path, const char *mode)
79         static FILE* (*func)(const char *path, const char *mode) = 0;
80 // This pointer is meaningless except on the server.
81         FILE *result = 0;
83         if (!func)
84         func = (FILE*(*)(const char *path, const char *mode))dlsym(RTLD_NEXT, "fopen64");
86 // VFS path
87         if(!strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
88         {
89                 renderfarm_fs_global->lock();
90                 result = renderfarm_fs_global->fopen(path, mode);
91                 renderfarm_fs_global->unlock();
92         }
93         else
94                 result = (*func)(path, mode);
96     return result;
101 int fclose(FILE *file)
103         static int (*func)(FILE *) = 0;
104         int result = 0, done = 0;
105         if(!func)
106         func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fclose");
107 //printf("fclose\n");
109         if(renderfarm_fs_global)
110         {
111                 renderfarm_fs_global->lock();
112                 if(renderfarm_fs_global->is_open(file))
113                 {
114                         result = renderfarm_fs_global->fclose(file);
115                         done = 1;
116                 }
117                 renderfarm_fs_global->unlock();
118         }
120         if(!done) result = (*func)(file);
121         return result;
124 int fileno (FILE *stream)
126         static int (*func)(FILE *) = 0;
127         int result = -1;
128         if(!func)
129         func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fileno");
130         if(renderfarm_fs_global)
131         {
132                 renderfarm_fs_global->lock();
133                 if(renderfarm_fs_global->is_open(stream))
134                 {
135                         result = renderfarm_fs_global->fileno(stream);
136                 }
137                 renderfarm_fs_global->unlock();
138         }
139         else
140                 result = (*func)(stream);
141         return result;
144 // int fflush(FILE *file)
145 // {
146 //      static int (*func)(FILE *) = 0;
147 //      int result = 0, done = 0;
148 //      if(!func)
149 //      func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fflush");
150 // //printf("fflush\n");
151 // 
152 //      renderfarm_fs_global->lock();
153 //      if(renderfarm_fs_global->is_open(file))
154 //      {
155 //              result = renderfarm_fs_global->fflush(file);
156 //              done = 1;
157 //      }
158 //      renderfarm_fs_global->unlock();
159 //      
160 //      if(!done) result = (*func)(file);
161 //      return result;
162 // }
164 int remove (__const char *__filename)
166         static int (*func)(__const char *) = 0;
167         int result = 0;
168         if(!func)
169         func = (int(*)(__const char *))dlsym(RTLD_NEXT, "remove");
170 //printf("remove\n");
172 // VFS path
173         if(!strncmp(__filename, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
174         {
175                 renderfarm_fs_global->lock();
176                 result = renderfarm_fs_global->remove(__filename);
177                 renderfarm_fs_global->unlock();
178         }
179         else
180                 result = (*func)(__filename);
182         return result;
185 int rename (__const char *__old, __const char *__new)
187         static int (*func)(__const char *, __const char *) = 0;
188         int result = 0;
189         if(!func)
190         func = (int(*)(__const char *, __const char *))dlsym(RTLD_NEXT, "rename");
191 //printf("rename\n");
193 // VFS path
194         if(!strncmp(__old, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
195         {
196                 renderfarm_fs_global->lock();
197                 result = renderfarm_fs_global->rename(__old, __new);
198                 renderfarm_fs_global->unlock();
199         }
200         else
201                 result = (*func)(__old, __new);
203         return result;
206 int fgetc (FILE *__stream)
208         static int (*func)(FILE *) = 0;
209         int result = 0, done = 0;
210         if(!func)
211         func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fgetc");
212 //printf("fgetc\n");
214         if(renderfarm_fs_global)
215         {
216                 renderfarm_fs_global->lock();
217                 if(renderfarm_fs_global->is_open(__stream))
218                 {
219                         result = renderfarm_fs_global->fgetc(__stream);
220                         done = 1;
221                 }
222                 renderfarm_fs_global->unlock();
223         }
225         if(!done) result = (*func)(__stream);
226         return result;
229 int getc (FILE *__stream)
231         return fgetc(__stream);
234 int fputc (int __c, FILE *__stream)
236         static int (*func)(int, FILE *) = 0;
237         int result = 0, done = 0;
238         if(!func)
239         func = (int(*)(int, FILE *))dlsym(RTLD_NEXT, "fputc");
240 //printf("fputc\n");
242         if(renderfarm_fs_global)
243         {
244                 renderfarm_fs_global->lock();
245                 if(renderfarm_fs_global->is_open(__stream))
246                 {
247                         result = renderfarm_fs_global->fputc(__c, __stream);
248                         done = 1;
249                 }
250                 renderfarm_fs_global->unlock();
251         }
253         if(!done) result = (*func)(__c, __stream);
254         return result;
257 int putc (int __c, FILE *__stream)
259         return fputc(__c, __stream);
262 size_t fread (void *__restrict __ptr, size_t __size,
263                      size_t __n, FILE *__restrict __stream)
265         static int (*func)(void *, size_t, size_t, FILE *) = 0;
266         size_t result = 0;
267         int done = 0;
268         if(!func)
269         func = (int(*)(void *, size_t, size_t, FILE *))dlsym(RTLD_NEXT, "fread");
270 //printf("fread\n");
272         if(renderfarm_fs_global)
273         {
274                 renderfarm_fs_global->lock();
275                 if(renderfarm_fs_global->is_open(__stream))
276                 {
277                         result = renderfarm_fs_global->fread(__ptr, __size, __n, __stream);
278                         done = 1;
279                 }
280                 renderfarm_fs_global->unlock();
281         }
283         if(!done) result = (*func)(__ptr, __size, __n, __stream);
285         return result;
288 size_t fwrite (__const void *__restrict __ptr, size_t __size,
289                       size_t __n, FILE *__restrict __s)
291         static int (*func)(__const void *, size_t, size_t, FILE *) = 0;
292         size_t result = 0;
293         int done = 0;
294         if(!func)
295         func = (int(*)(__const void *, size_t, size_t, FILE *))dlsym(RTLD_NEXT, "fwrite");
297         if(renderfarm_fs_global)
298         {
299                 renderfarm_fs_global->lock();
300                 if(renderfarm_fs_global->is_open(__s))
301                 {
302                         result = renderfarm_fs_global->fwrite(__ptr, __size, __n, __s);
303                         done = 1;
304                 }
305                 renderfarm_fs_global->unlock();
306         }
308         if(!done) result = (*func)(__ptr, __size, __n, __s);
310         return result;
313 int fseek (FILE *__stream, long int __off, int __whence)
315         static int (*func)(FILE *, long int, int) = 0;
316         int result = 0;
317         int done = 0;
318         if(!func)
319         func = (int(*)(FILE *, long int, int))dlsym(RTLD_NEXT, "fseek");
320 //printf("fseek\n");
322         if(renderfarm_fs_global)
323         {
324                 renderfarm_fs_global->lock();
325                 if(renderfarm_fs_global->is_open(__stream))
326                 {
327                         result = renderfarm_fs_global->fseek(__stream, __off, __whence);
328                         done = 1;
329                 }
330                 renderfarm_fs_global->unlock();
331         }
333         if(!done) result = (*func)(__stream, __off, __whence);
335         return result;
338 int fseeko64 (FILE *__stream, __off64_t __off, int __whence)
340         static int (*func)(FILE *, __off64_t, int) = 0;
341         int result = 0;
342         int done = 0;
343         if(!func)
344         func = (int(*)(FILE *, __off64_t, int))dlsym(RTLD_NEXT, "fseeko64");
345 //printf("fseeko64\n");
347         if(renderfarm_fs_global)
348         {
349                 renderfarm_fs_global->lock();
350                 if(renderfarm_fs_global->is_open(__stream))
351                 {
352                         result = renderfarm_fs_global->fseek(__stream, __off, __whence);
353                         done = 1;
354                 }
355                 renderfarm_fs_global->unlock();
356         }
358         if(!done) result = (*func)(__stream, __off, __whence);
360         return result;
363 long int ftell (FILE *__stream)
365         static long int (*func)(FILE *) = 0;
366         int result = 0;
367         int done = 0;
368         if(!func)
369         func = (long int(*)(FILE *))dlsym(RTLD_NEXT, "ftell");
370 //printf("ftell\n");
372         if(renderfarm_fs_global)
373         {
374                 renderfarm_fs_global->lock();
375                 if(renderfarm_fs_global->is_open(__stream))
376                 {
377                         result = renderfarm_fs_global->ftell(__stream);
378                         done = 1;
379                 }
380                 renderfarm_fs_global->unlock();
381         }
383         if(!done) result = (*func)(__stream);
385         return result;
388 __off64_t ftello64 (FILE *__stream)
390         static __off64_t (*func)(FILE *) = 0;
391         __off64_t result = 0;
392         int done = 0;
393         if(!func)
394         func = (__off64_t(*)(FILE *))dlsym(RTLD_NEXT, "ftello64");
396         if(renderfarm_fs_global)
397         {
398                 renderfarm_fs_global->lock();
399                 if(renderfarm_fs_global->is_open(__stream))
400                 {
401                         result = renderfarm_fs_global->ftell(__stream);
402                         done = 1;
403                 }
404                 renderfarm_fs_global->unlock();
405         }
407         if(!done) result = (*func)(__stream);
409         return result;
410         return (*func)(__stream);
414 // Glibc inlines the stat functions and redirects them to __xstat functions
415 int __xstat (int __ver, __const char *__filename,
416                     struct stat *__stat_buf)
418         static int (*func)(int __ver, __const char *__filename,
419                     struct stat *__stat_buf) = 0;
421 // This pointer is meaningless except on the server.
422         int result = 0;
424         if (!func)
425         func = (int(*)(int __ver, __const char *__filename,
426                     struct stat *__stat_buf))dlsym(RTLD_NEXT, "__xstat");
428 // VFS path
429         if(!strncmp(__filename, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
430         {
431                 renderfarm_fs_global->lock();
432                 result = renderfarm_fs_global->stat(__filename, __stat_buf);
433                 renderfarm_fs_global->unlock();
434         }
435         else
436         {
437                 result = (*func)(__ver, __filename, __stat_buf);
438         }
440     return result;
443 int __xstat64 (int __ver, __const char *__filename,
444                       struct stat64 *__stat_buf)
446         static int (*func)(int __ver, __const char *__restrict __file,
447                  struct stat64 *__restrict __buf) = 0;
448 // This pointer is meaningless except on the server.
449         int result = 0;
451         if (!func)
452         func = (int(*)(int __ver, __const char *__restrict __file,
453                                         struct stat64 *__restrict __buf))dlsym(RTLD_NEXT, "__xstat64");
455 // VFS path
456         if(!strncmp(__filename, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
457         {
458                 renderfarm_fs_global->lock();
459                 result = renderfarm_fs_global->stat64(__filename, __stat_buf);
460                 renderfarm_fs_global->unlock();
461         }
462         else
463                 result = (*func)(__ver, __filename, __stat_buf);
465     return result;
468 char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
470         static char* (*func)(char *__restrict __s, int __n, FILE *__restrict __stream) = 0;
471         char *result = 0;
472         int done = 0;
473         if(!func)
474         func = (char*(*)(char *__restrict __s, int __n, FILE *__restrict __stream))dlsym(RTLD_NEXT, "fgets");
476         if(renderfarm_fs_global)
477         {
478                 renderfarm_fs_global->lock();
479                 if(renderfarm_fs_global->is_open(__stream))
480                 {
481                         result = renderfarm_fs_global->fgets(__s, __n, __stream);
482                         done = 1;
483                 }
484                 renderfarm_fs_global->unlock();
485         }
487         if(!done) result = (*func)(__s, __n, __stream);
489         return result;
492 int fscanf (FILE *__restrict __stream,
493                    __const char *__restrict __format, ...)
495         int result = 0;
496         int done = 0;
497         va_list ap;
498         va_start(ap, __format);
500         if(renderfarm_fs_global)
501         {
502                 renderfarm_fs_global->lock();
503                 if(renderfarm_fs_global->is_open(__stream))
504                 {
505 // Since this is currently only used in one place in dcraw, leave it blank.
506 // The future implementation may just read until the next \n and scan the string.
507                         result = renderfarm_fs_global->fscanf(__stream, __format, ap);
508                         done = 1;
509                 }
510                 renderfarm_fs_global->unlock();
511         }
513         if(!done) result = vfscanf(__stream, __format, ap);
514         return result;
539 RenderFarmFSClient::RenderFarmFSClient(RenderFarmClientThread *client)
541         mutex_lock = new Mutex("RenderFarmFSClient::mutex_lock");
542         this->client = client;
545 RenderFarmFSClient::~RenderFarmFSClient()
547         delete mutex_lock;
548 // Must not access filesystem until we get here
549         renderfarm_fs_global = 0;
552 void RenderFarmFSClient::initialize()
554         renderfarm_fs_global = this;
557 void RenderFarmFSClient::lock()
559         mutex_lock->lock("RenderFarmFSClient::lock");
562 void RenderFarmFSClient::unlock()
564         mutex_lock->unlock();
567 int RenderFarmFSClient::is_open(FILE *ptr)
569         for(int i = 0; i < files.total; i++)
570                 if(files.values[i] == ptr) return 1;
571         return 0;
574 void RenderFarmFSClient::set_open(FILE *ptr, int64_t pointer)
576         files.append(ptr);
577         if(sizeof(FILE*) == 4)
578                 pointers.append(pointer);
581 void RenderFarmFSClient::unset_open(FILE *ptr, int64_t pointer)
583         files.remove(ptr);
584         if(sizeof(FILE*) == 4)
585                 pointers.remove(pointer);
588 int64_t RenderFarmFSClient::get_64(FILE *ptr)
590         if(sizeof(FILE*) == 4)
591         {
592                 for(int i = 0; i < files.total; i++)
593                 {
594                         if(files.values[i] == ptr)
595                                 return pointers.values[i];
596                 }
597         }
598         else
599                 return Units::ptr_to_int64(ptr);
601         printf("RenderFarmFSClient::get_64 file %p not found\n", ptr);
602         return 0;
606 FILE* RenderFarmFSClient::fopen(const char *path, const char *mode)
608 if(DEBUG)
609 printf("RenderFarmFSClient::fopen 1\n");
610         int len = strlen(path) - strlen(RENDERFARM_FS_PREFIX) + strlen(mode) + 2;
611         char *buffer = new char[len];
612         FILE *file = 0;
613         int64_t file_int64;
614         strcpy(buffer, path + strlen(RENDERFARM_FS_PREFIX));
615         strcpy(buffer + strlen(buffer) + 1, mode);
618         client->lock("RenderFarmFSClient::fopen");
619         if(!client->send_request_header(RENDERFARM_FOPEN, 
620                 len))
621         {
622                 if(client->write_socket(buffer, len, RENDERFARM_TIMEOUT) == len)
623                 {
624                         unsigned char data[8];
625                         if(client->read_socket((char*)data, 8, RENDERFARM_TIMEOUT) == 8)
626                         {
627                                 file_int64 = READ_INT64(data);
628                                 file = (FILE*)Units::int64_to_ptr(file_int64);
629                         }
630                 }
631         }
632         client->unlock();
633         if(file) set_open(file, file_int64);
634         delete [] buffer;
636 if(DEBUG)
637 printf("RenderFarmFSClient::fopen path=%s mode=%s file=%p\n", path, mode, file);
639         return file;
642 int RenderFarmFSClient::fclose(FILE *file)
644         int result = 0;
645         unsigned char datagram[8];
646         int i = 0;
647         int64_t file_int64 = get_64(file);
648         STORE_INT64(file_int64);
650         client->lock("RenderFarmFSClient::fclose");
651         if(!client->send_request_header(RENDERFARM_FCLOSE, 8))
652         {
653                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) == 8)
654                         result = 0;
655                 else
656                         result = -1;
657         }
658         else
659                 result = -1;
660         client->unlock();
661         unset_open(file, file_int64);
662 if(DEBUG)
663 printf("RenderFarmFSClient::fclose file=%p\n", file);
664         return result;
667 int RenderFarmFSClient::fileno(FILE *file)
669 if(DEBUG)
670 printf("RenderFarmFSClient::fileno file=%p\n", file);
671         int result = 0;
672         unsigned char datagram[8];
673         int i = 0;
674         int64_t file_int64 = get_64(file);
675         STORE_INT64(file_int64);
677         client->lock("RenderFarmFSClient::fileno");
678         if(!client->send_request_header(RENDERFARM_FILENO, 8))
679         {
680                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) == 8)
681                 {
682                         unsigned char data[4];
683                         if(client->read_socket((char*)data, 4, RENDERFARM_TIMEOUT) == 4)
684                         {
685                                 result = READ_INT32(data);
686                         }
687                 }
688                 else
689                         result = -1;
690         }
691         else
692                 result = -1;
693         client->unlock();
694 if(DEBUG)
695 printf("RenderFarmFSClient::fileno file=%p result=%d\n", file, result);
696         return result;
699 int RenderFarmFSClient::remove (__const char *__filename)
701         int result = 0;
702         int len = strlen(__filename) + 1;
703         char *datagram = new char[len];
704         strcpy(datagram, __filename);
705         
706         client->lock("RenderFarmFSClient::remove");
707         if(!client->send_request_header(RENDERFARM_REMOVE, len))
708         {
709                 if(client->write_socket(datagram, len, RENDERFARM_TIMEOUT) != len)
710                         result = -1;
711                 else
712                         result = 0;
713         }
714         else
715                 result = -1;
716         client->unlock();
718         delete [] datagram;
719 if(DEBUG)
720 printf("RenderFarmFSClient::remove path=%s\n", __filename);
721         return result;
724 int RenderFarmFSClient::rename (__const char *__old, __const char *__new)
726         int result = 0;
727         int len = strlen(__old) + 1 + strlen(__new) + 1;
728         char *datagram = new char[len];
729         strcpy(datagram, __old);
730         strcpy(datagram + strlen(__old) + 1, __new);
731         
732         client->lock("RenderFarmFSClient::rename");
733         if(!client->send_request_header(RENDERFARM_RENAME, len))
734         {
735                 if(client->write_socket(datagram, len, RENDERFARM_TIMEOUT) != len)
736                         result = -1;
737                 else
738                         result = 0;
739         }
740         else
741                 result = -1;
742         client->unlock();
744         delete [] datagram;
745 if(DEBUG)
746 printf("RenderFarmFSClient::remove old=%s new=%s\n", __old, __new);
747         return result;
750 int RenderFarmFSClient::fgetc (FILE *__stream)
752 if(DEBUG)
753 printf("RenderFarmFSClient::fgetc 1\n");
754         int result = 0;
755         unsigned char datagram[8];
756         int i = 0;
757         int64_t file_int64 = get_64(__stream);
758         STORE_INT64(file_int64);
759         
760         client->lock("RenderFarmFSClient::fgetc");
761         if(!client->send_request_header(RENDERFARM_FGETC, 8))
762         {
763                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) != 8)
764                         result = -1;
765                 else
766                 {
767                         if(client->read_socket((char*)datagram, 1, RENDERFARM_TIMEOUT) != 1)
768                                 result = -1;
769                         else
770                         {
771                                 result = datagram[0];
772                         }
773                 }
774         }
775         else
776                 result = -1;
777         client->unlock();
778 if(DEBUG)
779 printf("RenderFarmFSClient::fgetc file=%p result=%02x\n", __stream, result);
781         return result;
784 int RenderFarmFSClient::fputc (int __c, FILE *__stream)
786 if(DEBUG)
787 printf("RenderFarmFSClient::fputc 1\n");
788         int result = 0;
789         unsigned char datagram[9];
790         int i = 0;
791         int64_t file_int64 = get_64(__stream);
792         STORE_INT64(file_int64);
793         datagram[i++] = __c;
794         
795         client->lock("RenderFarmFSClient::fputc");
796         if(!client->send_request_header(RENDERFARM_FPUTC, 9))
797         {
798                 if(client->write_socket((char*)datagram, 9, RENDERFARM_TIMEOUT) != 9)
799                         result = -1;
800                 else
801                         result = __c;
802         }
803         else
804                 result = -1;
805         client->unlock();
806 if(DEBUG)
807 printf("RenderFarmFSClient::fputc file=%p result=%02x\n", __stream, result);
809         return result;
812 int RenderFarmFSClient::fscanf(FILE *__restrict stream, const char *__restrict format, va_list ap)
814         char string[BCTEXTLEN];
815         fgets (string, BCTEXTLEN, stream);
816         return 0;
819 char* RenderFarmFSClient::fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
821         char *result = 0;
822         int bytes = 0;
823         unsigned char datagram[12];
824         int i = 0;
825         int64_t file_int64 = get_64(__stream);
826         STORE_INT64(file_int64);
827         STORE_INT32(__n);
828         
829         client->lock("RenderFarmFSClient::fgets");
830         if(!client->send_request_header(RENDERFARM_FGETS, 12))
831         {
832                 if(client->write_socket((char*)datagram, 12, RENDERFARM_TIMEOUT) == 12)
833                 {
834 // fgets bytes to follow
835                         if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) == 4)
836                         {
837 // fgets data
838                                 bytes = READ_INT32(datagram);
839                                 if(bytes)
840                                 {
841                                         if(client->read_socket((char*)__s, bytes, RENDERFARM_TIMEOUT) == bytes)
842                                         {
843                                                 result = __s;
844                                         }
845                                 }
846                         }
847                 }
848         }
849         else
850                 result = 0;
851         client->unlock();
852 if(DEBUG)
853 printf("RenderFarmFSClient::fgets file=%p string=%p size=%d bytes=%p\n", 
854 __stream, __s, bytes, bytes);
856         return result;
860 size_t RenderFarmFSClient::fread (void *__restrict __ptr, size_t __size,
861                      size_t __n, FILE *__restrict __stream)
863         size_t result = 0;
864         unsigned char datagram[16];
865         int i = 0;
866         int64_t file_int64 = get_64(__stream);
867         STORE_INT64(file_int64);
868         STORE_INT32(__size);
869         STORE_INT32(__n);
870         
871         client->lock("RenderFarmFSClient::fread");
872         if(!client->send_request_header(RENDERFARM_FREAD, 16))
873         {
874                 if(client->write_socket((char*)datagram, 16, RENDERFARM_TIMEOUT) != 16)
875                         result = 0;
876                 else
877                 {
878 // fread result
879                         if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) != 4)
880                                 result = 0;
881                         else
882                         {
883 // fread data
884                                 result = READ_INT32(datagram);
885                                 if(client->read_socket((char*)__ptr, __size * result, RENDERFARM_TIMEOUT) != 
886                                         __size * result)
887                                         result = 0;
888                         }
889                 }
890         }
891         else
892                 result = 0;
893         client->unlock();
894 if(DEBUG)
895 printf("RenderFarmFSClient::fread file=%p size=%d num=%d result=%d\n", 
896 __stream, __size, __n, result);
898         return result;
901 size_t RenderFarmFSClient::fwrite (__const void *__restrict __ptr, size_t __size,
902                       size_t __n, FILE *__restrict __s)
904 if(DEBUG)
905 printf("RenderFarmFSClient::fwrite 1\n");
906         size_t result = 0;
907         unsigned char datagram[16];
908         int i = 0;
909         int64_t file_int64 = get_64(__s);
910         STORE_INT64(file_int64);
911         STORE_INT32(__size);
912         STORE_INT32(__n);
913         
914         client->lock("RenderFarmFSClient::fwrite");
915         if(!client->send_request_header(RENDERFARM_FWRITE, 16))
916         {
917                 if(client->write_socket((char*)datagram, 16, RENDERFARM_TIMEOUT) != 16)
918                         result = 0;
919                 else
920                 {
921 // fwrite data
922                         if(client->write_socket((char*)__ptr, __size * __n, RENDERFARM_TIMEOUT) != 
923                                 __size * __n)
924                         result = 0;
925                         else
926                         {
927 // fwrite result
928                                 if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) != 4)
929                                         result = 0;
930                                 else
931                                 {
932                                         result = READ_INT32(datagram);
933                                 }
934                         }
935                 }
936         }
937         else
938                 result = 0;
939         client->unlock();
940 if(DEBUG)
941 printf("RenderFarmFSClient::fwrite file=%p size=%d num=%d result=%d\n", 
942 __s, __size, __n, result);
944         return result;
947 int RenderFarmFSClient::fseek (FILE *__stream, int64_t __off, int __whence)
949 if(DEBUG)
950 printf("RenderFarmFSClient::fseek 1\n");
951         int result = 0;
952         unsigned char datagram[20];
953         int i = 0;
954         int64_t file_int64 = get_64(__stream);
955         STORE_INT64(file_int64);
956         STORE_INT64(__off);
957         STORE_INT32(__whence);
959 // printf("RenderFarmFSClient::fseek %p %llx datagram=%02x%02x%02x%02x%02x%02x%02x%02x\n",
960 // __stream, file_int64, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4], datagram[5], datagram[6], datagram[7]);
961         client->lock("RenderFarmFSClient::fseek");
962         if(!client->send_request_header(RENDERFARM_FSEEK, 20))
963         {
964                 if(client->write_socket((char*)datagram, 20, RENDERFARM_TIMEOUT) != 20)
965                         result = -1;
966                 else
967                 {
968                         if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) != 4)
969                                 result = -1;
970                         else
971                                 result = READ_INT32(datagram);
972                 }
973         }
974         else
975                 result = -1;
976         client->unlock();
977 if(DEBUG)
978 printf("RenderFarmFSClient::fseek stream=%p offset=%lld whence=%d result=%d\n", 
979 __stream, __off, __whence, result);
980         return result;
983 int64_t RenderFarmFSClient::ftell (FILE *__stream)
985         int64_t result = 0;
986         unsigned char datagram[8];
987         int i = 0;
988         int64_t file_int64 = get_64(__stream);
989         STORE_INT64(file_int64);
991         client->lock("RenderFarmFSClient::ftell");
992         if(!client->send_request_header(RENDERFARM_FTELL, 8))
993         {
994                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) != 8)
995                         result = -1;
996                 else
997                 {
998                         if(client->read_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) != 8)
999                                 result = -1;
1000                         else
1001                                 result = READ_INT64(datagram);
1002                 }
1003         }
1004         else
1005                 result = -1;
1006         client->unlock();
1007 if(DEBUG)
1008 printf("RenderFarmFSClient::fseek stream=%p result=%lld\n", 
1009 __stream, result);
1010         return result;
1013 int RenderFarmFSClient::stat (__const char *__restrict __file,
1014                  struct stat *__restrict __buf)
1016         int len = strlen(__file) + 1;
1017         int result = 0;
1019         client->lock("RenderFarmFSClient::stat");
1020         if(!client->send_request_header(RENDERFARM_STAT, len))
1021         {
1022                 if(client->write_socket((char*)__file + strlen(RENDERFARM_FS_PREFIX), len, RENDERFARM_TIMEOUT) == len)
1023                 {
1024                         if(client->read_socket((char*)__buf, sizeof(struct stat), RENDERFARM_TIMEOUT) == sizeof(struct stat))
1025                         {
1026                                 ;
1027                         }
1028                         else
1029                                 result = 1;
1030                 }
1031                 else
1032                         result = 1;
1033         }
1034         else
1035                 result = 1;
1036         client->unlock();
1037 if(DEBUG)
1038 printf("RenderFarmFSClient::stat path=%s\n", __file);
1040         return result;
1045 int RenderFarmFSClient::stat64 (__const char *__restrict __file,
1046                    struct stat64 *__restrict __buf)
1048         int len = strlen(__file) + 1;
1049         int result = 0;
1050         bzero(__buf, sizeof(struct stat64));
1052         client->lock("RenderFarmFSClient::stat64");
1053         if(!client->send_request_header(RENDERFARM_STAT64, len))
1054         {
1055                 if(client->write_socket((char*)__file + strlen(RENDERFARM_FS_PREFIX), len, RENDERFARM_TIMEOUT) == len)
1056                 {
1057                         vfs_stat_t arg;
1058                         if(client->read_socket((char*)&arg, sizeof(arg), RENDERFARM_TIMEOUT) == sizeof(arg))
1059                         {
1060                                 __buf->st_dev = arg.dev;
1061 //                              __buf->__st_ino = arg.ino32;
1062                                 __buf->st_ino = arg.ino;
1063                                 __buf->st_mode = arg.mode;
1064                                 __buf->st_nlink = arg.nlink;
1065                                 __buf->st_uid = arg.uid;
1066                                 __buf->st_gid = arg.gid;
1067                                 __buf->st_rdev = arg.rdev;
1068                                 __buf->st_size = arg.size;
1069                                 __buf->st_blksize = arg.blksize;
1070                                 __buf->st_blocks = arg.blocks;
1071                                 __buf->st_atim.tv_sec = arg.atim;
1072                                 __buf->st_mtim.tv_sec = arg.mtim;
1073                                 __buf->st_ctim.tv_sec = arg.ctim;
1074                         }
1075                         else
1076                                 result = 1;
1077                 }
1078                 else
1079                         result = 1;
1080         }
1081         else
1082                 result = 1;
1083         client->unlock();
1084 if(DEBUG)
1085 printf("RenderFarmFSClient::stat64 path=%s\n", __file);
1087         return result;