depot/go/nix/nar/inmemoryfs.go

60 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-08-23 23:00:44 +00:00
// SPDX-FileCopyrightText: 2023 Luke Granger-Brown <depot@lukegb.com>
//
// SPDX-License-Identifier: Apache-2.0
package nar
import "os"
type InmemoryDirent struct {
SubFS *InmemoryFS
Content []byte
Executable bool
Target string
}
func (dent *InmemoryDirent) Close() error { return nil }
func (dent *InmemoryDirent) Write(bs []byte) (int, error) {
dent.Content = append(dent.Content, bs...)
return len(bs), nil
}
func (dent *InmemoryDirent) MakeExecutable() error {
dent.Executable = true
return nil
}
type InmemoryFS struct {
Dirent map[string]*InmemoryDirent
}
func (fs *InmemoryFS) Create(name string) (WriteFile, error) {
if _, ok := fs.Dirent[name]; ok {
return nil, os.ErrExist
}
dent := &InmemoryDirent{}
fs.Dirent[name] = dent
return dent, nil
}
func (fs *InmemoryFS) Symlink(name, target string) error {
if _, ok := fs.Dirent[name]; ok {
return os.ErrExist
}
dent := &InmemoryDirent{Target: target}
fs.Dirent[name] = dent
return nil
}
func (fs *InmemoryFS) Mkdir(name string) (WriteFS, error) {
if _, ok := fs.Dirent[name]; ok {
return nil, os.ErrExist
}
dent := &InmemoryDirent{SubFS: NewInmemoryFS()}
fs.Dirent[name] = dent
return dent.SubFS, nil
}
func NewInmemoryFS() *InmemoryFS {
return &InmemoryFS{Dirent: make(map[string]*InmemoryDirent)}
}