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

     1  //
     2  //  Copyright 2020 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 orefafs
    18  
    19  import (
    20  	"io/fs"
    21  	"time"
    22  
    23  	"github.com/avfs/avfs"
    24  )
    25  
    26  // New returns a new memory file system (OrefaFS) with the default Options.
    27  func New() *OrefaFS {
    28  	return NewWithOptions(nil)
    29  }
    30  
    31  // NewWithOptions returns a new memory file system (OrefaFS) with the selected Options.
    32  func NewWithOptions(opts *Options) *OrefaFS {
    33  	if opts == nil {
    34  		opts = &Options{OSType: avfs.OsUnknown}
    35  	}
    36  
    37  	features := avfs.FeatHardlink | avfs.BuildFeatures()
    38  	idm := avfs.NotImplementedIdm
    39  
    40  	user := opts.User
    41  	if opts.User == nil {
    42  		user = idm.AdminUser()
    43  	}
    44  
    45  	vfs := &OrefaFS{
    46  		dirMode:  fs.ModeDir,
    47  		fileMode: 0,
    48  		lastId:   new(uint64),
    49  		name:     opts.Name,
    50  	}
    51  
    52  	_ = vfs.SetFeatures(features)
    53  	_ = vfs.SetOSType(opts.OSType)
    54  	_ = vfs.SetIdm(idm)
    55  	_ = vfs.SetUser(user)
    56  
    57  	vfs.err.SetOSType(vfs.OSType())
    58  
    59  	volumeName := ""
    60  	curDir := "/"
    61  
    62  	if vfs.OSType() == avfs.OsWindows {
    63  		vfs.dirMode |= avfs.DefaultDirPerm
    64  		vfs.fileMode |= avfs.DefaultFilePerm
    65  		volumeName = avfs.DefaultVolume
    66  		curDir = volumeName + string(vfs.PathSeparator())
    67  	}
    68  
    69  	vfs.nodes = make(nodes)
    70  	vfs.nodes[volumeName] = &node{
    71  		mode:  fs.ModeDir | 0o755,
    72  		mtime: time.Now().UnixNano(),
    73  		uid:   0,
    74  		gid:   0,
    75  	}
    76  
    77  	_ = vfs.SetCurDir(curDir)
    78  
    79  	if len(opts.SystemDirs) == 0 {
    80  		opts.SystemDirs = avfs.SystemDirs(vfs, volumeName)
    81  	}
    82  
    83  	_ = avfs.MkSystemDirs(vfs, opts.SystemDirs)
    84  	_ = vfs.SetUMask(avfs.UMask())
    85  
    86  	return vfs
    87  }
    88  
    89  // Name returns the name of the fileSystem.
    90  func (vfs *OrefaFS) Name() string {
    91  	return vfs.name
    92  }
    93  
    94  // Type returns the type of the fileSystem or Identity manager.
    95  func (*OrefaFS) Type() string {
    96  	return "OrefaFS"
    97  }