Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

renderfarmfsclient.C

Go to the documentation of this file.
00001 // Disable 64 bit indirections so we can override both functions.
00002 #undef _LARGEFILE64_SOURCE
00003 #undef _LARGEFILE_SOURCE
00004 #undef _FILE_OFFSET_BITS
00005 
00006 #include "mutex.h"
00007 #include "renderfarm.h"
00008 #include "renderfarmclient.h"
00009 #include "renderfarmfsclient.h"
00010 #include "renderfarmfsserver.h"
00011 #include "units.h"
00012 
00013 #include <dlfcn.h>
00014 #include <fcntl.h>
00015 #include <stdarg.h>
00016 #include <stdio.h>
00017 #include <string.h>
00018 #include <sys/stat.h>
00019 #include <unistd.h>
00020 
00021 
00022 #define DEBUG 0
00023 
00024 // These are hacks to get all the file I/O libraries to transparently 
00025 // go over the network without rewriting them.
00026 
00027 
00028 extern "C"
00029 {
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 RenderFarmFSClient *renderfarm_fs_global = 0;
00038 
00039 
00040 // open doesn't seem overridable
00041 // int open (__const char *path, int flags, ...)
00042 // {
00043 //      static int (*func)(__const char *__file, int __oflag) = 0;
00044 //      int result = -1;
00045 // printf("open %s\n", path);
00046 // 
00047 //      if (!func)
00048 //      func = (int(*)(const char *path, int flags))dlsym(RTLD_NEXT, "open");
00049 //      
00050 //      result = (*func)(path, flags);
00051 //      return result;
00052 // }
00053 
00054 
00055 FILE* fopen(const char *path, const char *mode)
00056 {
00057         static FILE* (*func)(const char *path, const char *mode) = 0;
00058 // This pointer is meaningless except on the server.
00059         FILE *result = 0;
00060 
00061         if (!func)
00062         func = (FILE*(*)(const char *path, const char *mode))dlsym(RTLD_NEXT, "fopen");
00063 
00064 // VFS path
00065         if(!strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
00066         {
00067                 renderfarm_fs_global->lock();
00068                 result = renderfarm_fs_global->fopen(path, mode);
00069                 renderfarm_fs_global->unlock();
00070         }
00071         else
00072                 result = (*func)(path, mode);
00073 
00074     return result;
00075 }
00076 
00077 FILE* fopen64(const char *path, const char *mode)
00078 {
00079         static FILE* (*func)(const char *path, const char *mode) = 0;
00080 // This pointer is meaningless except on the server.
00081         FILE *result = 0;
00082 
00083         if (!func)
00084         func = (FILE*(*)(const char *path, const char *mode))dlsym(RTLD_NEXT, "fopen64");
00085 
00086 // VFS path
00087         if(!strncmp(path, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
00088         {
00089                 renderfarm_fs_global->lock();
00090                 result = renderfarm_fs_global->fopen(path, mode);
00091                 renderfarm_fs_global->unlock();
00092         }
00093         else
00094                 result = (*func)(path, mode);
00095 
00096     return result;
00097 }
00098 
00099 
00100 
00101 int fclose(FILE *file)
00102 {
00103         static int (*func)(FILE *) = 0;
00104         int result = 0, done = 0;
00105         if(!func)
00106         func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fclose");
00107 //printf("fclose\n");
00108 
00109         if(renderfarm_fs_global)
00110         {
00111                 renderfarm_fs_global->lock();
00112                 if(renderfarm_fs_global->is_open(file))
00113                 {
00114                         result = renderfarm_fs_global->fclose(file);
00115                         done = 1;
00116                 }
00117                 renderfarm_fs_global->unlock();
00118         }
00119 
00120         if(!done) result = (*func)(file);
00121         return result;
00122 }
00123 
00124 int fileno (FILE *stream)
00125 {
00126         static int (*func)(FILE *) = 0;
00127         int result = -1;
00128         if(!func)
00129         func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fileno");
00130         if(renderfarm_fs_global)
00131         {
00132                 renderfarm_fs_global->lock();
00133                 if(renderfarm_fs_global->is_open(stream))
00134                 {
00135                         result = renderfarm_fs_global->fileno(stream);
00136                 }
00137                 renderfarm_fs_global->unlock();
00138         }
00139         else
00140                 result = (*func)(stream);
00141         return result;
00142 }
00143 
00144 // int fflush(FILE *file)
00145 // {
00146 //      static int (*func)(FILE *) = 0;
00147 //      int result = 0, done = 0;
00148 //      if(!func)
00149 //      func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fflush");
00150 // //printf("fflush\n");
00151 // 
00152 //      renderfarm_fs_global->lock();
00153 //      if(renderfarm_fs_global->is_open(file))
00154 //      {
00155 //              result = renderfarm_fs_global->fflush(file);
00156 //              done = 1;
00157 //      }
00158 //      renderfarm_fs_global->unlock();
00159 //      
00160 //      if(!done) result = (*func)(file);
00161 //      return result;
00162 // }
00163 
00164 int remove (__const char *__filename)
00165 {
00166         static int (*func)(__const char *) = 0;
00167         int result = 0;
00168         if(!func)
00169         func = (int(*)(__const char *))dlsym(RTLD_NEXT, "remove");
00170 //printf("remove\n");
00171 
00172 // VFS path
00173         if(!strncmp(__filename, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
00174         {
00175                 renderfarm_fs_global->lock();
00176                 result = renderfarm_fs_global->remove(__filename);
00177                 renderfarm_fs_global->unlock();
00178         }
00179         else
00180                 result = (*func)(__filename);
00181 
00182         return result;
00183 }
00184 
00185 int rename (__const char *__old, __const char *__new)
00186 {
00187         static int (*func)(__const char *, __const char *) = 0;
00188         int result = 0;
00189         if(!func)
00190         func = (int(*)(__const char *, __const char *))dlsym(RTLD_NEXT, "rename");
00191 //printf("rename\n");
00192 
00193 // VFS path
00194         if(!strncmp(__old, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
00195         {
00196                 renderfarm_fs_global->lock();
00197                 result = renderfarm_fs_global->rename(__old, __new);
00198                 renderfarm_fs_global->unlock();
00199         }
00200         else
00201                 result = (*func)(__old, __new);
00202 
00203         return result;
00204 }
00205 
00206 int fgetc (FILE *__stream)
00207 {
00208         static int (*func)(FILE *) = 0;
00209         int result = 0, done = 0;
00210         if(!func)
00211         func = (int(*)(FILE *))dlsym(RTLD_NEXT, "fgetc");
00212 //printf("fgetc\n");
00213 
00214         if(renderfarm_fs_global)
00215         {
00216                 renderfarm_fs_global->lock();
00217                 if(renderfarm_fs_global->is_open(__stream))
00218                 {
00219                         result = renderfarm_fs_global->fgetc(__stream);
00220                         done = 1;
00221                 }
00222                 renderfarm_fs_global->unlock();
00223         }
00224 
00225         if(!done) result = (*func)(__stream);
00226         return result;
00227 }
00228 
00229 int getc (FILE *__stream)
00230 {
00231         return fgetc(__stream);
00232 }
00233 
00234 int fputc (int __c, FILE *__stream)
00235 {
00236         static int (*func)(int, FILE *) = 0;
00237         int result = 0, done = 0;
00238         if(!func)
00239         func = (int(*)(int, FILE *))dlsym(RTLD_NEXT, "fputc");
00240 //printf("fputc\n");
00241 
00242         if(renderfarm_fs_global)
00243         {
00244                 renderfarm_fs_global->lock();
00245                 if(renderfarm_fs_global->is_open(__stream))
00246                 {
00247                         result = renderfarm_fs_global->fputc(__c, __stream);
00248                         done = 1;
00249                 }
00250                 renderfarm_fs_global->unlock();
00251         }
00252 
00253         if(!done) result = (*func)(__c, __stream);
00254         return result;
00255 }
00256 
00257 int putc (int __c, FILE *__stream)
00258 {
00259         return fputc(__c, __stream);
00260 }
00261 
00262 size_t fread (void *__restrict __ptr, size_t __size,
00263                      size_t __n, FILE *__restrict __stream)
00264 {
00265         static int (*func)(void *, size_t, size_t, FILE *) = 0;
00266         size_t result = 0;
00267         int done = 0;
00268         if(!func)
00269         func = (int(*)(void *, size_t, size_t, FILE *))dlsym(RTLD_NEXT, "fread");
00270 //printf("fread\n");
00271 
00272         if(renderfarm_fs_global)
00273         {
00274                 renderfarm_fs_global->lock();
00275                 if(renderfarm_fs_global->is_open(__stream))
00276                 {
00277                         result = renderfarm_fs_global->fread(__ptr, __size, __n, __stream);
00278                         done = 1;
00279                 }
00280                 renderfarm_fs_global->unlock();
00281         }
00282 
00283         if(!done) result = (*func)(__ptr, __size, __n, __stream);
00284 
00285         return result;
00286 }
00287 
00288 size_t fwrite (__const void *__restrict __ptr, size_t __size,
00289                       size_t __n, FILE *__restrict __s)
00290 {
00291         static int (*func)(__const void *, size_t, size_t, FILE *) = 0;
00292         size_t result = 0;
00293         int done = 0;
00294         if(!func)
00295         func = (int(*)(__const void *, size_t, size_t, FILE *))dlsym(RTLD_NEXT, "fwrite");
00296 
00297         if(renderfarm_fs_global)
00298         {
00299                 renderfarm_fs_global->lock();
00300                 if(renderfarm_fs_global->is_open(__s))
00301                 {
00302                         result = renderfarm_fs_global->fwrite(__ptr, __size, __n, __s);
00303                         done = 1;
00304                 }
00305                 renderfarm_fs_global->unlock();
00306         }
00307 
00308         if(!done) result = (*func)(__ptr, __size, __n, __s);
00309 
00310         return result;
00311 }
00312 
00313 int fseek (FILE *__stream, long int __off, int __whence)
00314 {
00315         static int (*func)(FILE *, long int, int) = 0;
00316         int result = 0;
00317         int done = 0;
00318         if(!func)
00319         func = (int(*)(FILE *, long int, int))dlsym(RTLD_NEXT, "fseek");
00320 //printf("fseek\n");
00321 
00322         if(renderfarm_fs_global)
00323         {
00324                 renderfarm_fs_global->lock();
00325                 if(renderfarm_fs_global->is_open(__stream))
00326                 {
00327                         result = renderfarm_fs_global->fseek(__stream, __off, __whence);
00328                         done = 1;
00329                 }
00330                 renderfarm_fs_global->unlock();
00331         }
00332 
00333         if(!done) result = (*func)(__stream, __off, __whence);
00334 
00335         return result;
00336 }
00337 
00338 int fseeko64 (FILE *__stream, __off64_t __off, int __whence)
00339 {
00340         static int (*func)(FILE *, __off64_t, int) = 0;
00341         int result = 0;
00342         int done = 0;
00343         if(!func)
00344         func = (int(*)(FILE *, __off64_t, int))dlsym(RTLD_NEXT, "fseeko64");
00345 //printf("fseeko64\n");
00346 
00347         if(renderfarm_fs_global)
00348         {
00349                 renderfarm_fs_global->lock();
00350                 if(renderfarm_fs_global->is_open(__stream))
00351                 {
00352                         result = renderfarm_fs_global->fseek(__stream, __off, __whence);
00353                         done = 1;
00354                 }
00355                 renderfarm_fs_global->unlock();
00356         }
00357 
00358         if(!done) result = (*func)(__stream, __off, __whence);
00359 
00360         return result;
00361 }
00362 
00363 long int ftell (FILE *__stream)
00364 {
00365         static long int (*func)(FILE *) = 0;
00366         int result = 0;
00367         int done = 0;
00368         if(!func)
00369         func = (long int(*)(FILE *))dlsym(RTLD_NEXT, "ftell");
00370 //printf("ftell\n");
00371 
00372         if(renderfarm_fs_global)
00373         {
00374                 renderfarm_fs_global->lock();
00375                 if(renderfarm_fs_global->is_open(__stream))
00376                 {
00377                         result = renderfarm_fs_global->ftell(__stream);
00378                         done = 1;
00379                 }
00380                 renderfarm_fs_global->unlock();
00381         }
00382 
00383         if(!done) result = (*func)(__stream);
00384 
00385         return result;
00386 }
00387 
00388 __off64_t ftello64 (FILE *__stream)
00389 {
00390         static __off64_t (*func)(FILE *) = 0;
00391         __off64_t result = 0;
00392         int done = 0;
00393         if(!func)
00394         func = (__off64_t(*)(FILE *))dlsym(RTLD_NEXT, "ftello64");
00395 
00396         if(renderfarm_fs_global)
00397         {
00398                 renderfarm_fs_global->lock();
00399                 if(renderfarm_fs_global->is_open(__stream))
00400                 {
00401                         result = renderfarm_fs_global->ftell(__stream);
00402                         done = 1;
00403                 }
00404                 renderfarm_fs_global->unlock();
00405         }
00406 
00407         if(!done) result = (*func)(__stream);
00408 
00409         return result;
00410         return (*func)(__stream);
00411 }
00412 
00413 
00414 // Glibc inlines the stat functions and redirects them to __xstat functions
00415 int __xstat (int __ver, __const char *__filename,
00416                     struct stat *__stat_buf)
00417 {
00418         static int (*func)(int __ver, __const char *__filename,
00419                     struct stat *__stat_buf) = 0;
00420 
00421 // This pointer is meaningless except on the server.
00422         int result = 0;
00423 
00424         if (!func)
00425         func = (int(*)(int __ver, __const char *__filename,
00426                     struct stat *__stat_buf))dlsym(RTLD_NEXT, "__xstat");
00427 
00428 // VFS path
00429         if(!strncmp(__filename, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
00430         {
00431                 renderfarm_fs_global->lock();
00432                 result = renderfarm_fs_global->stat(__filename, __stat_buf);
00433                 renderfarm_fs_global->unlock();
00434         }
00435         else
00436         {
00437                 result = (*func)(__ver, __filename, __stat_buf);
00438         }
00439 
00440     return result;
00441 }
00442 
00443 int __xstat64 (int __ver, __const char *__filename,
00444                       struct stat64 *__stat_buf)
00445 {
00446         static int (*func)(int __ver, __const char *__restrict __file,
00447                  struct stat64 *__restrict __buf) = 0;
00448 // This pointer is meaningless except on the server.
00449         int result = 0;
00450 
00451         if (!func)
00452         func = (int(*)(int __ver, __const char *__restrict __file,
00453                                         struct stat64 *__restrict __buf))dlsym(RTLD_NEXT, "__xstat64");
00454 
00455 // VFS path
00456         if(!strncmp(__filename, RENDERFARM_FS_PREFIX, strlen(RENDERFARM_FS_PREFIX)))
00457         {
00458                 renderfarm_fs_global->lock();
00459                 result = renderfarm_fs_global->stat64(__filename, __stat_buf);
00460                 renderfarm_fs_global->unlock();
00461         }
00462         else
00463                 result = (*func)(__ver, __filename, __stat_buf);
00464 
00465     return result;
00466 }
00467 
00468 char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
00469 {
00470         static char* (*func)(char *__restrict __s, int __n, FILE *__restrict __stream) = 0;
00471         char *result = 0;
00472         int done = 0;
00473         if(!func)
00474         func = (char*(*)(char *__restrict __s, int __n, FILE *__restrict __stream))dlsym(RTLD_NEXT, "fgets");
00475 
00476         if(renderfarm_fs_global)
00477         {
00478                 renderfarm_fs_global->lock();
00479                 if(renderfarm_fs_global->is_open(__stream))
00480                 {
00481                         result = renderfarm_fs_global->fgets(__s, __n, __stream);
00482                         done = 1;
00483                 }
00484                 renderfarm_fs_global->unlock();
00485         }
00486 
00487         if(!done) result = (*func)(__s, __n, __stream);
00488 
00489         return result;
00490 }
00491 
00492 int fscanf (FILE *__restrict __stream,
00493                    __const char *__restrict __format, ...)
00494 {
00495         int result = 0;
00496         int done = 0;
00497         va_list ap;
00498         va_start(ap, __format);
00499 
00500         if(renderfarm_fs_global)
00501         {
00502                 renderfarm_fs_global->lock();
00503                 if(renderfarm_fs_global->is_open(__stream))
00504                 {
00505 // Since this is currently only used in one place in dcraw, leave it blank.
00506 // The future implementation may just read until the next \n and scan the string.
00507                         result = renderfarm_fs_global->fscanf(__stream, __format, ap);
00508                         done = 1;
00509                 }
00510                 renderfarm_fs_global->unlock();
00511         }
00512 
00513         if(!done) result = vfscanf(__stream, __format, ap);
00514         return result;
00515 }
00516 
00517 
00518 
00519 
00520 
00521 
00522 
00523 
00524 
00525 
00526 }
00527 
00528 
00529 
00530 
00531 
00532 
00533 
00534 
00535 
00536 
00537 
00538 
00539 RenderFarmFSClient::RenderFarmFSClient(RenderFarmClientThread *client)
00540 {
00541         mutex_lock = new Mutex("RenderFarmFSClient::mutex_lock");
00542         this->client = client;
00543 }
00544 
00545 RenderFarmFSClient::~RenderFarmFSClient()
00546 {
00547         delete mutex_lock;
00548 // Must not access filesystem until we get here
00549         renderfarm_fs_global = 0;
00550 }
00551 
00552 void RenderFarmFSClient::initialize()
00553 {
00554         renderfarm_fs_global = this;
00555 }
00556 
00557 void RenderFarmFSClient::lock()
00558 {
00559         mutex_lock->lock("RenderFarmFSClient::lock");
00560 }
00561 
00562 void RenderFarmFSClient::unlock()
00563 {
00564         mutex_lock->unlock();
00565 }
00566 
00567 int RenderFarmFSClient::is_open(FILE *ptr)
00568 {
00569         for(int i = 0; i < files.total; i++)
00570                 if(files.values[i] == ptr) return 1;
00571         return 0;
00572 }
00573 
00574 void RenderFarmFSClient::set_open(FILE *ptr, int64_t pointer)
00575 {
00576         files.append(ptr);
00577         if(sizeof(FILE*) == 4)
00578                 pointers.append(pointer);
00579 }
00580 
00581 void RenderFarmFSClient::unset_open(FILE *ptr, int64_t pointer)
00582 {
00583         files.remove(ptr);
00584         if(sizeof(FILE*) == 4)
00585                 pointers.remove(pointer);
00586 }
00587 
00588 int64_t RenderFarmFSClient::get_64(FILE *ptr)
00589 {
00590         if(sizeof(FILE*) == 4)
00591         {
00592                 for(int i = 0; i < files.total; i++)
00593                 {
00594                         if(files.values[i] == ptr)
00595                                 return pointers.values[i];
00596                 }
00597         }
00598         else
00599                 return Units::ptr_to_int64(ptr);
00600 
00601         printf("RenderFarmFSClient::get_64 file %p not found\n", ptr);
00602         return 0;
00603 }
00604 
00605 
00606 FILE* RenderFarmFSClient::fopen(const char *path, const char *mode)
00607 {
00608 if(DEBUG)
00609 printf("RenderFarmFSClient::fopen 1\n");
00610         int len = strlen(path) - strlen(RENDERFARM_FS_PREFIX) + strlen(mode) + 2;
00611         char *buffer = new char[len];
00612         FILE *file = 0;
00613         int64_t file_int64;
00614         strcpy(buffer, path + strlen(RENDERFARM_FS_PREFIX));
00615         strcpy(buffer + strlen(buffer) + 1, mode);
00616 
00617 
00618         client->lock("RenderFarmFSClient::fopen");
00619         if(!client->send_request_header(RENDERFARM_FOPEN, 
00620                 len))
00621         {
00622                 if(client->write_socket(buffer, len, RENDERFARM_TIMEOUT) == len)
00623                 {
00624                         unsigned char data[8];
00625                         if(client->read_socket((char*)data, 8, RENDERFARM_TIMEOUT) == 8)
00626                         {
00627                                 file_int64 = READ_INT64(data);
00628                                 file = (FILE*)Units::int64_to_ptr(file_int64);
00629                         }
00630                 }
00631         }
00632         client->unlock();
00633         if(file) set_open(file, file_int64);
00634         delete [] buffer;
00635 
00636 if(DEBUG)
00637 printf("RenderFarmFSClient::fopen path=%s mode=%s file=%p\n", path, mode, file);
00638 
00639         return file;
00640 }
00641 
00642 int RenderFarmFSClient::fclose(FILE *file)
00643 {
00644         int result = 0;
00645         unsigned char datagram[8];
00646         int i = 0;
00647         int64_t file_int64 = get_64(file);
00648         STORE_INT64(file_int64);
00649 
00650         client->lock("RenderFarmFSClient::fclose");
00651         if(!client->send_request_header(RENDERFARM_FCLOSE, 8))
00652         {
00653                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) == 8)
00654                         result = 0;
00655                 else
00656                         result = -1;
00657         }
00658         else
00659                 result = -1;
00660         client->unlock();
00661         unset_open(file, file_int64);
00662 if(DEBUG)
00663 printf("RenderFarmFSClient::fclose file=%p\n", file);
00664         return result;
00665 }
00666 
00667 int RenderFarmFSClient::fileno(FILE *file)
00668 {
00669 if(DEBUG)
00670 printf("RenderFarmFSClient::fileno file=%p\n", file);
00671         int result = 0;
00672         unsigned char datagram[8];
00673         int i = 0;
00674         int64_t file_int64 = get_64(file);
00675         STORE_INT64(file_int64);
00676 
00677         client->lock("RenderFarmFSClient::fileno");
00678         if(!client->send_request_header(RENDERFARM_FILENO, 8))
00679         {
00680                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) == 8)
00681                 {
00682                         unsigned char data[4];
00683                         if(client->read_socket((char*)data, 4, RENDERFARM_TIMEOUT) == 4)
00684                         {
00685                                 result = READ_INT32(data);
00686                         }
00687                 }
00688                 else
00689                         result = -1;
00690         }
00691         else
00692                 result = -1;
00693         client->unlock();
00694 if(DEBUG)
00695 printf("RenderFarmFSClient::fileno file=%p result=%d\n", file, result);
00696         return result;
00697 }
00698 
00699 int RenderFarmFSClient::remove (__const char *__filename)
00700 {
00701         int result = 0;
00702         int len = strlen(__filename) + 1;
00703         char *datagram = new char[len];
00704         strcpy(datagram, __filename);
00705         
00706         client->lock("RenderFarmFSClient::remove");
00707         if(!client->send_request_header(RENDERFARM_REMOVE, len))
00708         {
00709                 if(client->write_socket(datagram, len, RENDERFARM_TIMEOUT) != len)
00710                         result = -1;
00711                 else
00712                         result = 0;
00713         }
00714         else
00715                 result = -1;
00716         client->unlock();
00717 
00718         delete [] datagram;
00719 if(DEBUG)
00720 printf("RenderFarmFSClient::remove path=%s\n", __filename);
00721         return result;
00722 }
00723 
00724 int RenderFarmFSClient::rename (__const char *__old, __const char *__new)
00725 {
00726         int result = 0;
00727         int len = strlen(__old) + 1 + strlen(__new) + 1;
00728         char *datagram = new char[len];
00729         strcpy(datagram, __old);
00730         strcpy(datagram + strlen(__old) + 1, __new);
00731         
00732         client->lock("RenderFarmFSClient::rename");
00733         if(!client->send_request_header(RENDERFARM_RENAME, len))
00734         {
00735                 if(client->write_socket(datagram, len, RENDERFARM_TIMEOUT) != len)
00736                         result = -1;
00737                 else
00738                         result = 0;
00739         }
00740         else
00741                 result = -1;
00742         client->unlock();
00743 
00744         delete [] datagram;
00745 if(DEBUG)
00746 printf("RenderFarmFSClient::remove old=%s new=%s\n", __old, __new);
00747         return result;
00748 }
00749 
00750 int RenderFarmFSClient::fgetc (FILE *__stream)
00751 {
00752 if(DEBUG)
00753 printf("RenderFarmFSClient::fgetc 1\n");
00754         int result = 0;
00755         unsigned char datagram[8];
00756         int i = 0;
00757         int64_t file_int64 = get_64(__stream);
00758         STORE_INT64(file_int64);
00759         
00760         client->lock("RenderFarmFSClient::fgetc");
00761         if(!client->send_request_header(RENDERFARM_FGETC, 8))
00762         {
00763                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) != 8)
00764                         result = -1;
00765                 else
00766                 {
00767                         if(client->read_socket((char*)datagram, 1, RENDERFARM_TIMEOUT) != 1)
00768                                 result = -1;
00769                         else
00770                         {
00771                                 result = datagram[0];
00772                         }
00773                 }
00774         }
00775         else
00776                 result = -1;
00777         client->unlock();
00778 if(DEBUG)
00779 printf("RenderFarmFSClient::fgetc file=%p result=%02x\n", __stream, result);
00780 
00781         return result;
00782 }
00783 
00784 int RenderFarmFSClient::fputc (int __c, FILE *__stream)
00785 {
00786 if(DEBUG)
00787 printf("RenderFarmFSClient::fputc 1\n");
00788         int result = 0;
00789         unsigned char datagram[9];
00790         int i = 0;
00791         int64_t file_int64 = get_64(__stream);
00792         STORE_INT64(file_int64);
00793         datagram[i++] = __c;
00794         
00795         client->lock("RenderFarmFSClient::fputc");
00796         if(!client->send_request_header(RENDERFARM_FPUTC, 9))
00797         {
00798                 if(client->write_socket((char*)datagram, 9, RENDERFARM_TIMEOUT) != 9)
00799                         result = -1;
00800                 else
00801                         result = __c;
00802         }
00803         else
00804                 result = -1;
00805         client->unlock();
00806 if(DEBUG)
00807 printf("RenderFarmFSClient::fputc file=%p result=%02x\n", __stream, result);
00808 
00809         return result;
00810 }
00811 
00812 int RenderFarmFSClient::fscanf(FILE *__restrict stream, const char *__restrict format, va_list ap)
00813 {
00814         char string[BCTEXTLEN];
00815         fgets (string, BCTEXTLEN, stream);
00816         return 0;
00817 }
00818 
00819 char* RenderFarmFSClient::fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
00820 {
00821         char *result = 0;
00822         int bytes = 0;
00823         unsigned char datagram[12];
00824         int i = 0;
00825         int64_t file_int64 = get_64(__stream);
00826         STORE_INT64(file_int64);
00827         STORE_INT32(__n);
00828         
00829         client->lock("RenderFarmFSClient::fgets");
00830         if(!client->send_request_header(RENDERFARM_FGETS, 12))
00831         {
00832                 if(client->write_socket((char*)datagram, 12, RENDERFARM_TIMEOUT) == 12)
00833                 {
00834 // fgets bytes to follow
00835                         if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) == 4)
00836                         {
00837 // fgets data
00838                                 bytes = READ_INT32(datagram);
00839                                 if(bytes)
00840                                 {
00841                                         if(client->read_socket((char*)__s, bytes, RENDERFARM_TIMEOUT) == bytes)
00842                                         {
00843                                                 result = __s;
00844                                         }
00845                                 }
00846                         }
00847                 }
00848         }
00849         else
00850                 result = 0;
00851         client->unlock();
00852 if(DEBUG)
00853 printf("RenderFarmFSClient::fgets file=%p string=%p size=%d bytes=%p\n", 
00854 __stream, __s, bytes, bytes);
00855 
00856         return result;
00857 }
00858 
00859 
00860 size_t RenderFarmFSClient::fread (void *__restrict __ptr, size_t __size,
00861                      size_t __n, FILE *__restrict __stream)
00862 {
00863         size_t result = 0;
00864         unsigned char datagram[16];
00865         int i = 0;
00866         int64_t file_int64 = get_64(__stream);
00867         STORE_INT64(file_int64);
00868         STORE_INT32(__size);
00869         STORE_INT32(__n);
00870         
00871         client->lock("RenderFarmFSClient::fread");
00872         if(!client->send_request_header(RENDERFARM_FREAD, 16))
00873         {
00874                 if(client->write_socket((char*)datagram, 16, RENDERFARM_TIMEOUT) != 16)
00875                         result = 0;
00876                 else
00877                 {
00878 // fread result
00879                         if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) != 4)
00880                                 result = 0;
00881                         else
00882                         {
00883 // fread data
00884                                 result = READ_INT32(datagram);
00885                                 if(client->read_socket((char*)__ptr, __size * result, RENDERFARM_TIMEOUT) != 
00886                                         __size * result)
00887                                         result = 0;
00888                         }
00889                 }
00890         }
00891         else
00892                 result = 0;
00893         client->unlock();
00894 if(DEBUG)
00895 printf("RenderFarmFSClient::fread file=%p size=%d num=%d result=%d\n", 
00896 __stream, __size, __n, result);
00897 
00898         return result;
00899 }
00900 
00901 size_t RenderFarmFSClient::fwrite (__const void *__restrict __ptr, size_t __size,
00902                       size_t __n, FILE *__restrict __s)
00903 {
00904 if(DEBUG)
00905 printf("RenderFarmFSClient::fwrite 1\n");
00906         size_t result = 0;
00907         unsigned char datagram[16];
00908         int i = 0;
00909         int64_t file_int64 = get_64(__s);
00910         STORE_INT64(file_int64);
00911         STORE_INT32(__size);
00912         STORE_INT32(__n);
00913         
00914         client->lock("RenderFarmFSClient::fwrite");
00915         if(!client->send_request_header(RENDERFARM_FWRITE, 16))
00916         {
00917                 if(client->write_socket((char*)datagram, 16, RENDERFARM_TIMEOUT) != 16)
00918                         result = 0;
00919                 else
00920                 {
00921 // fwrite data
00922                         if(client->write_socket((char*)__ptr, __size * __n, RENDERFARM_TIMEOUT) != 
00923                                 __size * __n)
00924                         result = 0;
00925                         else
00926                         {
00927 // fwrite result
00928                                 if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) != 4)
00929                                         result = 0;
00930                                 else
00931                                 {
00932                                         result = READ_INT32(datagram);
00933                                 }
00934                         }
00935                 }
00936         }
00937         else
00938                 result = 0;
00939         client->unlock();
00940 if(DEBUG)
00941 printf("RenderFarmFSClient::fwrite file=%p size=%d num=%d result=%d\n", 
00942 __s, __size, __n, result);
00943 
00944         return result;
00945 }
00946 
00947 int RenderFarmFSClient::fseek (FILE *__stream, int64_t __off, int __whence)
00948 {
00949 if(DEBUG)
00950 printf("RenderFarmFSClient::fseek 1\n");
00951         int result = 0;
00952         unsigned char datagram[20];
00953         int i = 0;
00954         int64_t file_int64 = get_64(__stream);
00955         STORE_INT64(file_int64);
00956         STORE_INT64(__off);
00957         STORE_INT32(__whence);
00958 
00959 // printf("RenderFarmFSClient::fseek %p %llx datagram=%02x%02x%02x%02x%02x%02x%02x%02x\n",
00960 // __stream, file_int64, datagram[0], datagram[1], datagram[2], datagram[3], datagram[4], datagram[5], datagram[6], datagram[7]);
00961         client->lock("RenderFarmFSClient::fseek");
00962         if(!client->send_request_header(RENDERFARM_FSEEK, 20))
00963         {
00964                 if(client->write_socket((char*)datagram, 20, RENDERFARM_TIMEOUT) != 20)
00965                         result = -1;
00966                 else
00967                 {
00968                         if(client->read_socket((char*)datagram, 4, RENDERFARM_TIMEOUT) != 4)
00969                                 result = -1;
00970                         else
00971                                 result = READ_INT32(datagram);
00972                 }
00973         }
00974         else
00975                 result = -1;
00976         client->unlock();
00977 if(DEBUG)
00978 printf("RenderFarmFSClient::fseek stream=%p offset=%lld whence=%d result=%d\n", 
00979 __stream, __off, __whence, result);
00980         return result;
00981 }
00982 
00983 int64_t RenderFarmFSClient::ftell (FILE *__stream)
00984 {
00985         int64_t result = 0;
00986         unsigned char datagram[8];
00987         int i = 0;
00988         int64_t file_int64 = get_64(__stream);
00989         STORE_INT64(file_int64);
00990 
00991         client->lock("RenderFarmFSClient::ftell");
00992         if(!client->send_request_header(RENDERFARM_FTELL, 8))
00993         {
00994                 if(client->write_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) != 8)
00995                         result = -1;
00996                 else
00997                 {
00998                         if(client->read_socket((char*)datagram, 8, RENDERFARM_TIMEOUT) != 8)
00999                                 result = -1;
01000                         else
01001                                 result = READ_INT64(datagram);
01002                 }
01003         }
01004         else
01005                 result = -1;
01006         client->unlock();
01007 if(DEBUG)
01008 printf("RenderFarmFSClient::fseek stream=%p result=%lld\n", 
01009 __stream, result);
01010         return result;
01011 }
01012 
01013 int RenderFarmFSClient::stat (__const char *__restrict __file,
01014                  struct stat *__restrict __buf)
01015 {
01016         int len = strlen(__file) + 1;
01017         int result = 0;
01018 
01019         client->lock("RenderFarmFSClient::stat");
01020         if(!client->send_request_header(RENDERFARM_STAT, len))
01021         {
01022                 if(client->write_socket((char*)__file + strlen(RENDERFARM_FS_PREFIX), len, RENDERFARM_TIMEOUT) == len)
01023                 {
01024                         if(client->read_socket((char*)__buf, sizeof(struct stat), RENDERFARM_TIMEOUT) == sizeof(struct stat))
01025                         {
01026                                 ;
01027                         }
01028                         else
01029                                 result = 1;
01030                 }
01031                 else
01032                         result = 1;
01033         }
01034         else
01035                 result = 1;
01036         client->unlock();
01037 if(DEBUG)
01038 printf("RenderFarmFSClient::stat path=%s\n", __file);
01039 
01040         return result;
01041 }
01042 
01043 
01044 
01045 int RenderFarmFSClient::stat64 (__const char *__restrict __file,
01046                    struct stat64 *__restrict __buf)
01047 {
01048         int len = strlen(__file) + 1;
01049         int result = 0;
01050         bzero(__buf, sizeof(struct stat64));
01051 
01052         client->lock("RenderFarmFSClient::stat64");
01053         if(!client->send_request_header(RENDERFARM_STAT64, len))
01054         {
01055                 if(client->write_socket((char*)__file + strlen(RENDERFARM_FS_PREFIX), len, RENDERFARM_TIMEOUT) == len)
01056                 {
01057                         vfs_stat_t arg;
01058                         if(client->read_socket((char*)&arg, sizeof(arg), RENDERFARM_TIMEOUT) == sizeof(arg))
01059                         {
01060                                 __buf->st_dev = arg.dev;
01061 //                              __buf->__st_ino = arg.ino32;
01062                                 __buf->st_ino = arg.ino;
01063                                 __buf->st_mode = arg.mode;
01064                                 __buf->st_nlink = arg.nlink;
01065                                 __buf->st_uid = arg.uid;
01066                                 __buf->st_gid = arg.gid;
01067                                 __buf->st_rdev = arg.rdev;
01068                                 __buf->st_size = arg.size;
01069                                 __buf->st_blksize = arg.blksize;
01070                                 __buf->st_blocks = arg.blocks;
01071                                 __buf->st_atim.tv_sec = arg.atim;
01072                                 __buf->st_mtim.tv_sec = arg.mtim;
01073                                 __buf->st_ctim.tv_sec = arg.ctim;
01074                         }
01075                         else
01076                                 result = 1;
01077                 }
01078                 else
01079                         result = 1;
01080         }
01081         else
01082                 result = 1;
01083         client->unlock();
01084 if(DEBUG)
01085 printf("RenderFarmFSClient::stat64 path=%s\n", __file);
01086 
01087         return result;
01088 }
01089 
01090 
01091 

Generated on Sun Jan 8 13:39:00 2006 for Cinelerra-svn by  doxygen 1.4.4