github.com/3JoB/vfs@v1.0.0/example_readonly_test.go (about)

     1  package vfs_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/3JoB/vfs"
     8  )
     9  
    10  // Every vfs.Filesystem could be easily wrapped
    11  func ExampleRoFS() {
    12  	// Create a readonly vfs accessing the filesystem of the underlying OS
    13  	roFS := vfs.ReadOnly(vfs.OS())
    14  
    15  	// Mkdir is disabled on ReadOnly vfs, will return vfs.ErrReadOnly
    16  	// See vfs.ReadOnly for all disabled operations
    17  	err := roFS.Mkdir("/tmp/vfs_example", 0777)
    18  	if err != nil {
    19  		fmt.Printf("Error creating directory: %s\n", err)
    20  		return
    21  	}
    22  
    23  	// OpenFile is controlled to support read-only functionality. os.O_CREATE or os.O_APPEND will fail.
    24  	// Flags like os.O_RDWR are supported but the returned file is protected e.g. from Write(..).
    25  	f, err := roFS.OpenFile("/tmp/vfs_example/example.txt", os.O_RDWR, 0)
    26  	if err != nil {
    27  		fmt.Printf("Could not create file: %s\n", err)
    28  		return
    29  	}
    30  	defer f.Close()
    31  
    32  	// Will fail and return vfs.ErrReadOnly
    33  	_, err = f.Write([]byte("VFS working on your filesystem"))
    34  	if err != nil {
    35  		fmt.Printf("Could not write file on read only filesystem: %s", err)
    36  		return
    37  	}
    38  }