github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/memfs/memfs_iofs.go (about)

     1  //
     2  //  Copyright 2022 The AVFS 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 memfs
    18  
    19  import (
    20  	"io/fs"
    21  	"os"
    22  )
    23  
    24  // Open opens the named file for reading. If successful, methods on
    25  // the returned file can be used for reading; the associated file
    26  // descriptor has mode O_RDONLY.
    27  // If there is an error, it will be of type *PathError.
    28  func (vfs *MemIOFS) Open(name string) (fs.File, error) {
    29  	return vfs.OpenFile(name, os.O_RDONLY, 0)
    30  }
    31  
    32  // Sub returns an FS corresponding to the subtree rooted at dir.
    33  func (vfs *MemIOFS) Sub(dir string) (fs.FS, error) {
    34  	const op = "sub"
    35  
    36  	_, child, _, err := vfs.searchNode(dir, slmEval)
    37  	if err != vfs.err.FileExists || child == nil {
    38  		return nil, &fs.PathError{Op: op, Path: dir, Err: err}
    39  	}
    40  
    41  	c, ok := child.(*dirNode)
    42  	if !ok {
    43  		return nil, &fs.PathError{Op: op, Path: dir, Err: vfs.err.NotADirectory}
    44  	}
    45  
    46  	subFS := *vfs
    47  	subFS.rootNode = c
    48  
    49  	return &subFS, nil
    50  }