github.com/go4org/go4@v0.0.0-20200104003542-c7e774b10ea0/wkfs/wkfs.go (about)

     1  /*
     2  Copyright 2014 The Perkeep Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package wkfs implements the pluggable "well-known filesystem" abstraction layer.
    18  //
    19  // Instead of accessing files directly through the operating system
    20  // using os.Open or os.Stat, code should use wkfs.Open or wkfs.Stat,
    21  // which first try to intercept paths at well-known top-level
    22  // directories representing previously-registered mount types,
    23  // otherwise fall through to the operating system paths.
    24  //
    25  // Example of top-level well-known directories that might be
    26  // registered include /gcs/bucket/object for Google Cloud Storage or
    27  // /s3/bucket/object for AWS S3.
    28  package wkfs // import "go4.org/wkfs"
    29  
    30  import (
    31  	"io"
    32  	"io/ioutil"
    33  	"os"
    34  	"strings"
    35  )
    36  
    37  type File interface {
    38  	io.Reader
    39  	io.ReaderAt
    40  	io.Closer
    41  	io.Seeker
    42  	Name() string
    43  	Stat() (os.FileInfo, error)
    44  }
    45  
    46  type FileWriter interface {
    47  	io.Writer
    48  	io.Closer
    49  }
    50  
    51  func Open(name string) (File, error)               { return fs(name).Open(name) }
    52  func Stat(name string) (os.FileInfo, error)        { return fs(name).Stat(name) }
    53  func Lstat(name string) (os.FileInfo, error)       { return fs(name).Lstat(name) }
    54  func MkdirAll(path string, perm os.FileMode) error { return fs(path).MkdirAll(path, perm) }
    55  func OpenFile(name string, flag int, perm os.FileMode) (FileWriter, error) {
    56  	return fs(name).OpenFile(name, flag, perm)
    57  }
    58  func Remove(name string) error { return fs(name).Remove(name) }
    59  func Create(name string) (FileWriter, error) {
    60  	// like os.Create but WRONLY instead of RDWR because we don't
    61  	// expose a Reader here.
    62  	return OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
    63  }
    64  
    65  func fs(name string) FileSystem {
    66  	for pfx, fs := range wkFS {
    67  		if strings.HasPrefix(name, pfx) {
    68  			return fs
    69  		}
    70  	}
    71  	return osFS{}
    72  }
    73  
    74  type osFS struct{}
    75  
    76  func (osFS) Open(name string) (File, error)               { return os.Open(name) }
    77  func (osFS) Stat(name string) (os.FileInfo, error)        { return os.Stat(name) }
    78  func (osFS) Lstat(name string) (os.FileInfo, error)       { return os.Lstat(name) }
    79  func (osFS) MkdirAll(path string, perm os.FileMode) error { return os.MkdirAll(path, perm) }
    80  func (osFS) OpenFile(name string, flag int, perm os.FileMode) (FileWriter, error) {
    81  	return os.OpenFile(name, flag, perm)
    82  }
    83  func (osFS) Remove(name string) error { return os.Remove(name) }
    84  
    85  type FileSystem interface {
    86  	Open(name string) (File, error)
    87  	OpenFile(name string, flag int, perm os.FileMode) (FileWriter, error)
    88  	Stat(name string) (os.FileInfo, error)
    89  	Lstat(name string) (os.FileInfo, error)
    90  	MkdirAll(path string, perm os.FileMode) error
    91  	Remove(name string) error
    92  }
    93  
    94  // well-known filesystems
    95  var wkFS = map[string]FileSystem{}
    96  
    97  // RegisterFS registers a well-known filesystem. It intercepts
    98  // anything beginning with prefix (which must start and end with a
    99  // forward slash) and forwards it to fs.
   100  func RegisterFS(prefix string, fs FileSystem) {
   101  	if !strings.HasPrefix(prefix, "/") || !strings.HasSuffix(prefix, "/") {
   102  		panic("bogus prefix: " + prefix)
   103  	}
   104  	if _, dup := wkFS[prefix]; dup {
   105  		panic("duplication registration of " + prefix)
   106  	}
   107  	wkFS[prefix] = fs
   108  }
   109  
   110  // WriteFile writes data to a file named by filename.
   111  // If the file does not exist, WriteFile creates it with permissions perm;
   112  // otherwise WriteFile truncates it before writing.
   113  func WriteFile(filename string, data []byte, perm os.FileMode) error {
   114  	f, err := OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
   115  	if err != nil {
   116  		return err
   117  	}
   118  	n, err := f.Write(data)
   119  	if err == nil && n < len(data) {
   120  		err = io.ErrShortWrite
   121  	}
   122  	if err1 := f.Close(); err == nil {
   123  		err = err1
   124  	}
   125  	return err
   126  }
   127  
   128  func ReadFile(filename string) ([]byte, error) {
   129  	f, err := Open(filename)
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  	defer f.Close()
   134  	return ioutil.ReadAll(f)
   135  }