Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 1.85 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to write to file in FUSE file system using php
  2. <?php
  3.  
  4. $op = new FuseOperations();
  5. $op->getattr = "hello_getattr";
  6. $op->open = "hello_open";
  7. $op->read = "hello_read";
  8. $op->readdir = "hello_readdir";
  9. $op->write = "hello_write";
  10.  
  11. fuse_main( $argv, $op );
  12.  
  13. function hello_write($path, &$buf, $offset, $fh = None) {
  14.     logit('I am in write function');
  15.     logit($path);
  16.     logit($fh);
  17. }
  18.  
  19. function hello_getattr( $path, $stbuf ) {
  20.     if( strcmp($path, "/") == 0 ) {
  21.         $stbuf->st_mode = FUSE_S_IFDIR | 0755;
  22.         $stbuf->st_nlink = 2;
  23.     } else {
  24.         $stbuf->st_uid = posix_getuid();
  25.         $stbuf->st_gid = posix_getgid();
  26.         $stbuf->st_mode = FUSE_S_IFREG | 0644;
  27.         $stbuf->st_nlink = 1;
  28.         $stbuf->st_size = 56;
  29.         $stbuf->st_mtime = 1226379246;
  30.     }
  31.  
  32.     return -FUSE_ENOERR;
  33. }
  34.  
  35. function hello_open( $path, $fi ) {
  36.     logit('I am in open function');
  37.     if( ($fi->flags & FUSE_O_ACCMODE) != FUSE_O_RDONLY )
  38.     return -FUSE_ENOACCES;
  39.  
  40.     return -FUSE_ENOERR;
  41. }
  42.  
  43. function hello_read( $path, &$buf, $size, $offset, $fi ) {
  44.     $data = file_get_contents('/home/poomalai/php_fuse/files'.$path);
  45.  
  46.     if( $offset > strlen($data) )
  47.     return 0;
  48.  
  49.     $buf = substr( $data, $offset, $size );
  50.  
  51.     return strlen($data);
  52. }
  53.  
  54. function hello_readdir( $path, &$buf ) {
  55.     if( strcmp($path, "/") != 0 )
  56.         return -FUSE_ENOENT;
  57.  
  58.     $buf[] = ".";
  59.     $buf[] = "..";
  60.  
  61.     if ($handle = opendir('/home/poomalai/php_fuse/files')) {
  62.         while (false !== ($entry = readdir($handle))) {
  63.             if ($entry != "." && $entry != "..") {
  64.                 $buf[] =  $entry;
  65.             }
  66.         }
  67.         closedir($handle);
  68.     }
  69.     return -FUSE_ENOERR;
  70. }
  71.  
  72.  
  73. function logit($data) {
  74.     file_put_contents('/home/poomalai/php_fuse/debug.txt', print_r($data, true), FILE_APPEND);
  75.     file_put_contents('/home/poomalai/php_fuse/debug.txt', "n", FILE_APPEND);
  76. }