github.com/hanwen/go-fuse@v1.0.0/benchmark/statfs.cc (about) 1 // +build !cgo 2 // g++ -Wall `pkg-config fuse --cflags --libs` statfs.cc -o statfs 3 4 #include <unordered_map> 5 #include <string> 6 7 using std::string; 8 using std::unordered_map; 9 10 #define FUSE_USE_VERSION 26 11 12 extern "C" { 13 #include <fuse.h> 14 } 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <errno.h> 20 #include <fcntl.h> 21 #include <unistd.h> 22 23 useconds_t delay_usec; 24 25 class StatFs { 26 public: 27 void readFrom(const string& fn); 28 unordered_map<string, bool> is_dir_; 29 30 int GetAttr(const char *name, struct stat *statbuf) { 31 if (strcmp(name, "/") == 0) { 32 statbuf->st_mode = S_IFDIR | 0777; 33 return 0; 34 } 35 unordered_map<string, bool>::const_iterator it(is_dir_.find(name)); 36 if (it == is_dir_.end()) { 37 return -ENOENT; 38 } 39 40 if (it->second) { 41 statbuf->st_mode = S_IFDIR | 0777; 42 } else { 43 statbuf->st_nlink = 1; 44 statbuf->st_mode = S_IFREG | 0666; 45 } 46 47 if (delay_usec > 0) { 48 usleep(delay_usec); 49 } 50 51 return 0; 52 } 53 54 }; 55 56 StatFs *global; 57 int global_getattr(const char *name, struct stat *statbuf) { 58 return global->GetAttr(name, statbuf); 59 } 60 61 void StatFs::readFrom(const string& fn) { 62 FILE *f = fopen(fn.c_str(), "r"); 63 64 char line[1024]; 65 while (char *s = fgets(line, sizeof(line), f)) { 66 int l = strlen(s); 67 if (line[l-1] == '\n') { 68 line[l-1] = '\0'; 69 l--; 70 } 71 bool is_dir = line[l-1] == '/'; 72 if (is_dir) { 73 line[l-1] = '\0'; 74 } 75 is_dir_[line] = is_dir; 76 } 77 fclose(f); 78 } 79 80 int main(int argc, char *argv[]) 81 { 82 global = new StatFs; 83 // don't want to know about fuselib's option handling 84 char *in = getenv("STATFS_INPUT"); 85 if (!in || !*in) { 86 fprintf(stderr, "pass file in $STATFS_INPUT\n"); 87 exit(2); 88 } 89 90 global->readFrom(in); 91 in = getenv("STATFS_DELAY_USEC"); 92 if (in != NULL) { 93 delay_usec = atoi(in); 94 } 95 96 struct fuse_operations statfs_oper = {0}; 97 statfs_oper.getattr = &global_getattr; 98 return fuse_main(argc, argv, &statfs_oper, NULL); 99 }