- How to write to file in FUSE file system using php
- <?php
- $op = new FuseOperations();
- $op->getattr = "hello_getattr";
- $op->open = "hello_open";
- $op->read = "hello_read";
- $op->readdir = "hello_readdir";
- $op->write = "hello_write";
- fuse_main( $argv, $op );
- function hello_write($path, &$buf, $offset, $fh = None) {
- logit('I am in write function');
- logit($path);
- logit($fh);
- }
- function hello_getattr( $path, $stbuf ) {
- if( strcmp($path, "/") == 0 ) {
- $stbuf->st_mode = FUSE_S_IFDIR | 0755;
- $stbuf->st_nlink = 2;
- } else {
- $stbuf->st_uid = posix_getuid();
- $stbuf->st_gid = posix_getgid();
- $stbuf->st_mode = FUSE_S_IFREG | 0644;
- $stbuf->st_nlink = 1;
- $stbuf->st_size = 56;
- $stbuf->st_mtime = 1226379246;
- }
- return -FUSE_ENOERR;
- }
- function hello_open( $path, $fi ) {
- logit('I am in open function');
- if( ($fi->flags & FUSE_O_ACCMODE) != FUSE_O_RDONLY )
- return -FUSE_ENOACCES;
- return -FUSE_ENOERR;
- }
- function hello_read( $path, &$buf, $size, $offset, $fi ) {
- $data = file_get_contents('/home/poomalai/php_fuse/files'.$path);
- if( $offset > strlen($data) )
- return 0;
- $buf = substr( $data, $offset, $size );
- return strlen($data);
- }
- function hello_readdir( $path, &$buf ) {
- if( strcmp($path, "/") != 0 )
- return -FUSE_ENOENT;
- $buf[] = ".";
- $buf[] = "..";
- if ($handle = opendir('/home/poomalai/php_fuse/files')) {
- while (false !== ($entry = readdir($handle))) {
- if ($entry != "." && $entry != "..") {
- $buf[] = $entry;
- }
- }
- closedir($handle);
- }
- return -FUSE_ENOERR;
- }
- function logit($data) {
- file_put_contents('/home/poomalai/php_fuse/debug.txt', print_r($data, true), FILE_APPEND);
- file_put_contents('/home/poomalai/php_fuse/debug.txt', "n", FILE_APPEND);
- }