rfs.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef RFS_H
  14. #define RFS_H
  15. #include <stdio.h>
  16. #ifndef byte
  17. #define byte unsigned char
  18. #endif
  19. struct RFS_context;
  20. #define RFS_OPEN_MODE_CREATE 0x00
  21. #define RFS_OPEN_MODE_READ 0x01
  22. #define RFS_OPEN_MODE_WRITE 0x02
  23. #define RFS_OPEN_MODE_READWRITE 0x03
  24. #define RFS_OPEN_MODE_CREATERW 0x04
  25. #define RFS_OPEN_MODE_MASK 0x07
  26. #define RFS_SHARING_NONE 0x00
  27. #define RFS_SHARING_READ 0x01
  28. #define RFS_SHARING_WRITE 0x02
  29. #define RFS_SHARING_EXEC 0x03
  30. #define RFS_SHARING_ALL 0x04
  31. #ifdef _WIN32
  32. typedef unsigned __int64 rfs_fpos_t;
  33. #else
  34. typedef unsigned long long rfs_fpos_t;
  35. #endif
  36. class RFS_ConnectionBase
  37. {
  38. public:
  39. RFS_ConnectionBase() {}
  40. virtual ~RFS_ConnectionBase() {}
  41. virtual void read(rfs_fpos_t pos, size_t len, size_t &outlen, void *out) = 0;
  42. virtual rfs_fpos_t size() = 0;
  43. virtual void write(rfs_fpos_t pos, size_t len, const void *in) = 0;
  44. virtual void close() = 0;
  45. };
  46. class RFS_ServerBase
  47. {
  48. public:
  49. virtual RFS_ConnectionBase *open(const char *name, byte mode, byte share) = 0; // return NULL if not found
  50. virtual void existFile(const char *filename, bool &existsout) = 0;
  51. virtual void removeFile(const char *filename) = 0;
  52. virtual void renameFile(const char *fromname,const char *toname) = 0;
  53. virtual void getFileTime(const char *filename, time_t &outaccessedtime, time_t &outcreatedtime, time_t &outmodifiedtime) = 0;
  54. virtual void setFileTime(const char *filename, time_t *inaccessedtime, time_t *increatedtime, time_t *inmodifiedtime) = 0; // params NULL if not to be set
  55. virtual void isFile(const char *filename, bool &outisfile) = 0;
  56. virtual void isDir(const char *filename, bool &outisdir) = 0;
  57. virtual void isReadOnly(const char *filename, bool &outisreadonly) = 0;
  58. virtual void setReadOnly(const char *filename, bool readonly) = 0;
  59. virtual void createDir(const char *dirname,bool &createdout) = 0;
  60. virtual void openDir(const char *dirname, const char *mask, bool recursive, bool includedir, void * &outhandle) = 0;
  61. virtual void nextDirEntry(void *handle, size_t maxfilenamesize, char *outfilename, bool &isdir, rfs_fpos_t &filesizeout, time_t &outmodifiedtime) = 0; // empty return for filename marks end
  62. virtual void closeDir(void *handle) = 0;
  63. virtual void getVersion(size_t programnamemax, char *programname, short &version) = 0;
  64. virtual unsigned short getDefaultPort(); // use if --port not specified (if overridden)
  65. virtual void poll() {} // called approx every second when idle
  66. virtual void getServiceName(size_t maxname, char *outname, // only called for windows services
  67. size_t maxdisplayname, char *outdisplayname);
  68. virtual int serviceInit(int argc, const char **argv,
  69. bool &outmulti); // only called for windows services (return 0 if OK)
  70. RFS_ServerBase() { context = NULL; }
  71. virtual ~RFS_ServerBase();
  72. bool init(int &argc, const char **argv); // must be called from main, will remove parameters it uses.
  73. // If returns false must exit returning 1
  74. int run(bool multi=false, // called to enter server run loop (multi not yet supported)
  75. const char *logname=""); // NULL is no log, "" is default
  76. void stop(); // can be called async to stop server (e.g. from poll)
  77. void setLogFilename(const char *filename); // set to NULL for no logfile (default is <exename>_<datetime>.log in cur dir)
  78. virtual void log(const char *format, ...) __attribute__((format(printf, 2, 3)));
  79. void throwError(int err, const char *errstr, bool fatal=false); // does not return, if fatal will stop process
  80. const char *logFilename();
  81. unsigned myNode(); // not yet supported
  82. unsigned clusterSize(); // not yet supported
  83. RFS_context *queryContext(); // internal
  84. int debugLevel(); // value of --debug
  85. private:
  86. RFS_context *context;
  87. };
  88. class RFS_SimpleString
  89. {
  90. size_t max;
  91. size_t inc;
  92. char *base;
  93. char *end;
  94. public:
  95. RFS_SimpleString(const char *inits=NULL,size_t initsz=0x1000);
  96. ~RFS_SimpleString()
  97. {
  98. free(base);
  99. }
  100. void appendc(char c);
  101. void appends(const char *s);
  102. inline const char *str() { *end = 0; return base; }
  103. inline char *data() { return base; }
  104. inline char *dup() { return _strdup(str()); }
  105. inline void clear() { end = base; }
  106. inline size_t length() { return end-base; }
  107. inline void setLength(size_t sz) { end = base+sz; }
  108. inline char lastChar() { if (end==base) return 0; return *(end-1); }
  109. inline void decLength(size_t sz=1) { if ((size_t)(end-base)<=sz) end = base; else end-=sz; }
  110. void trim();
  111. };
  112. class RFS_CSVwriter // for serializing rows (handles escaping etc)
  113. {
  114. RFS_SimpleString out;
  115. public:
  116. RFS_CSVwriter();
  117. void rewrite();
  118. void putField(size_t fldsize,void *data);
  119. void putRow();
  120. void consume(size_t sz);
  121. inline const void *base() { return out.str(); }
  122. inline size_t length() { return out.length(); }
  123. };
  124. class RFS_CSVreader // for serializing rows (handles escaping etc)
  125. {
  126. RFS_SimpleString fld;
  127. char * str;
  128. char * end;
  129. public:
  130. RFS_CSVreader();
  131. void reset(size_t sz,const void *data);
  132. bool nextRow();
  133. const char * getField(size_t &fldsize);
  134. };
  135. #endif