The vbs tools - vbs_ls, vbs_rm, vbs_fs - for listing, removing and mounting vbs and Mark6 format scattered VLBI recordings on FlexBuff and Mark6 systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

237 lines
6.7 KiB

  1. // FUSE file system for vlbi_streamer style recordings
  2. #define FUSE_USE_VERSION 26
  3. #include <fuse.h>
  4. #include <evlbidebug.h>
  5. #include <iostream>
  6. #include <cstddef>
  7. using namespace std;
  8. ///////////////////////////////////////////////
  9. // Keep state of the FUSE file system in
  10. // a struct of this type. It's a global
  11. // variable.
  12. ///////////////////////////////////////////////
  13. const string defaultRootDir = "/mnt";
  14. struct vbs_state_type {
  15. string rootDir;
  16. vbs_state_type() {}
  17. };
  18. vbs_state_type* vbs_state = 0;
  19. ///////////////////////////////////////////////
  20. // Here follow the implementations
  21. // of the file system functions
  22. // that we support
  23. ///////////////////////////////////////////////
  24. void* vbs_init(struct fuse_conn_info* /*conn*/) {
  25. DEBUG(-1, "vbs_init" << endl);
  26. return (void*)vbs_state;
  27. }
  28. void vbs_destroy(void* ptr) {
  29. DEBUG(-1, "vbs_destroy" << endl);
  30. if( ptr==(void*)vbs_state )
  31. DEBUG(-1, "Yes, it was our global pointer" << endl);
  32. delete (vbs_state_type*)ptr;
  33. vbs_state = 0;
  34. }
  35. ///////////////////////////////////////////////
  36. // A filesystem must define a set
  37. // of operations with callback functions
  38. // on the file system objects it
  39. // represents. All callbacks are
  40. // registered in the fuse_operations struct
  41. ///////////////////////////////////////////////
  42. struct vbs_fuse_operations :
  43. public fuse_operations {
  44. vbs_fuse_operations() {
  45. init = &vbs_init;
  46. destroy = &vbs_destroy;
  47. }
  48. };
  49. vbs_fuse_operations vbs_oper;
  50. #if 0
  51. struct fuse_operations vbs_oper = {
  52. //.getattr = vbs_getattr,
  53. //.readdir = vbs_readdir,
  54. //.open = vbs_open,
  55. //.read = vbs_read,
  56. //.readlink = vbs_readlink,
  57. // no .getdir -- that's deprecated
  58. //.getdir = NULL,
  59. //.mknod = vbs_mknod,
  60. //.mkdir = vbs_mkdir,
  61. //.unlink = vbs_unlink,
  62. //.rmdir = vbs_rmdir,
  63. //.symlink = vbs_symlink,
  64. //.rename = vbs_rename,
  65. //.link = vbs_link,
  66. //.chmod = vbs_chmod,
  67. //.chown = vbs_chown,
  68. //.truncate = vbs_truncate,
  69. //.utime = vbs_utime,
  70. //.write = vbs_write,
  71. /** Just a placeholder, don't set */ // huh???
  72. //.statfs = vbs_statfs,
  73. //.flush = vbs_flush,
  74. //.release = vbs_release,
  75. //.fsync = vbs_fsync,
  76. //.setxattr = vbs_setxattr,
  77. //.getxattr = vbs_getxattr,
  78. //.listxattr = vbs_listxattr,
  79. //.removexattr = vbs_removexattr,
  80. //.opendir = vbs_opendir,
  81. //.releasedir = vbs_releasedir,
  82. //.fsyncdir = vbs_fsyncdir,
  83. .init = vbs_init,
  84. .destroy = vbs_destroy,
  85. //.access = vbs_access,
  86. //.create = vbs_create,
  87. //.ftruncate = vbs_ftruncate,
  88. };
  89. #endif
  90. ///////////////////////////////////////////////
  91. // Command line handling
  92. // There's only a few options
  93. // that we support
  94. // -m <int> setting debug level
  95. // -r <path> set root dir for disks
  96. // -f FUSE 'foreground' flag
  97. ///////////////////////////////////////////////
  98. struct vbs_settings {
  99. int debug_level;
  100. bool help;
  101. bool foreground;
  102. char const* root_dir;
  103. // Initialize with defaults
  104. vbs_settings():
  105. debug_level( 0 ), help( false ), foreground( false ), root_dir( 0 )
  106. {}
  107. };
  108. ostream& operator<<(ostream& os, const vbs_settings& vbo) {
  109. os << "-m " << vbo.debug_level << " ";
  110. if( vbo.root_dir )
  111. os << "-r " << vbo.root_dir << " ";
  112. if( vbo.help )
  113. os << "-h" << " ";
  114. if( vbo.foreground )
  115. os << "-f" << " ";
  116. return os;
  117. }
  118. #define KEY_FOREGROUND 42
  119. #define KEY_HELP 666
  120. #define VBSFS_OPT(t, p, v) { t, offsetof(struct vbs_settings, p), v }
  121. struct fuse_opt vbs_options[] = {
  122. VBSFS_OPT("-m %i", debug_level, 0),
  123. VBSFS_OPT("-r %s", root_dir, 0),
  124. // The FUSE_OPT_KEY() ones are handled
  125. // by vbs_option_proc()
  126. //FUSE_OPT_KEY("-f", KEY_FOREGROUND),
  127. //FUSE_OPT_KEY("-h", KEY_HELP),
  128. FUSE_OPT_END
  129. };
  130. int vbs_option_proc(void* data, const char* arg, int key, struct fuse_args* /*outargs*/) {
  131. int rv = 1;
  132. const string arg_s( arg );
  133. vbs_settings* settingsptr = (vbs_settings*)data;
  134. if( key==KEY_FOREGROUND ) {
  135. rv = 0;
  136. settingsptr->foreground = true;
  137. } else if( key==KEY_HELP ) {
  138. rv = 0;
  139. settingsptr->help = true;
  140. } else {
  141. DEBUG(-1, "vbs_option_proc/Unrecognized arg=" << arg << " (key=" << key << ")" << endl);
  142. }
  143. if( arg_s=="-h" )
  144. settingsptr->help = true;
  145. return rv;
  146. }
  147. void Usage( void ) {
  148. cout << "VBS specific command line arguments:" << endl
  149. #if 0
  150. << " -h print this message" << endl
  151. << " -f run FUSE in the foreground, not as daemon" << endl
  152. #endif
  153. << " -m <int> set VBS_FS debug print level to <n>" << endl
  154. << " higher <n> => more output, default 0 (no output)" << endl
  155. << " -r <path> path to vbs_fs 'root' directory; assume all disks" << endl
  156. << " are mounted under <path>/disk<n> with <n> being" << endl
  157. << " a non-negative integer" << endl;
  158. return;
  159. }
  160. ///////////////////////////////////////////////
  161. //
  162. // The main function
  163. //
  164. ///////////////////////////////////////////////
  165. int main(int argc, char** argv) {
  166. // Before doing _anything_ check if we're not root/suid root
  167. if( ::getuid()==0 || ::geteuid()==0 ) {
  168. DEBUG(-1, "Running vbs_fuse as root opens unnacceptable security holes" << endl);
  169. return -11;
  170. }
  171. // Now we can safely start parsing
  172. int fuse_stat;
  173. vbs_settings settings;
  174. struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
  175. vbs_state = new vbs_state_type();
  176. // Do command line parsing
  177. ::fuse_opt_parse(&args, (void*)&settings, &vbs_options[0], vbs_option_proc);
  178. DEBUG(1, "fuse_opt_arg done [" << settings << "]" << endl);
  179. #if 0
  180. // Append to FUSE 'command line'
  181. if( settings.foreground && ::fuse_opt_add_arg(&args, "-f")!=0 ) {
  182. DEBUG(-1, "fuse_opt_add_arg(.., \"-f\") fails?!" << endl);
  183. return -12;
  184. }
  185. if( settings.help && ::fuse_opt_add_arg(&args, "-h")!=0 ) {
  186. DEBUG(-1, "fuse_opt_add_arg(.., \"-h\") fails?!" << endl);
  187. return -13;
  188. }
  189. #endif
  190. if( settings.help )
  191. Usage();
  192. // If no root dir given, default to "/mnt"
  193. if( settings.root_dir==0 )
  194. DEBUG(-1, "WARN: No root dir specified for disks, defaulting to " << defaultRootDir << endl);
  195. vbs_state->rootDir = (settings.root_dir==0?defaultRootDir:string(settings.root_dir));
  196. // Now it's about time to drop into FUSE's main loop
  197. DEBUG(3, "dropping into fuse_main" << endl);
  198. fuse_stat = fuse_main(args.argc, args.argv, &vbs_oper, vbs_state);
  199. DEBUG(3, "fuse_stat returned " << fuse_stat << endl);
  200. return fuse_stat;
  201. }