Allow passing root file type to tmpfs.

PiperOrigin-RevId: 304053357
This commit is contained in:
Nicolas Lacasse
2020-03-31 15:02:57 -07:00
committed by gVisor bot
parent 32a133537e
commit 9de982ea79
2 changed files with 33 additions and 2 deletions
+3
View File
@@ -162,6 +162,9 @@ func (s *System) ListDirents(pop *vfs.PathOperation) *DirentCollector {
// exactly the specified set of expected entries. AssertAllDirentTypes respects
// collector.skipDots, and implicitly checks for "." and ".." accordingly.
func (s *System) AssertAllDirentTypes(collector *DirentCollector, expected map[string]DirentType) {
if expected == nil {
expected = make(map[string]DirentType)
}
// Also implicitly check for "." and "..", if enabled.
if !collector.skipDots {
expected["."] = linux.DT_DIR
+30 -2
View File
@@ -68,6 +68,17 @@ func (FilesystemType) Name() string {
return Name
}
// FilesystemOpts is used to pass configuration data to tmpfs.
type FilesystemOpts struct {
// RootFileType is the FileType of the filesystem root. Valid values
// are: S_IFDIR, S_IFREG, and S_IFLNK. Defaults to S_IFDIR.
RootFileType uint16
// RootSymlinkTarget is the target of the root symlink. Only valid if
// RootFileType == S_IFLNK.
RootSymlinkTarget string
}
// GetFilesystem implements vfs.FilesystemType.GetFilesystem.
func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.VirtualFilesystem, creds *auth.Credentials, source string, opts vfs.GetFilesystemOptions) (*vfs.Filesystem, *vfs.Dentry, error) {
memFileProvider := pgalloc.MemoryFileProviderFromContext(ctx)
@@ -79,9 +90,26 @@ func (fstype FilesystemType) GetFilesystem(ctx context.Context, vfsObj *vfs.Virt
memFile: memFileProvider.MemoryFile(),
clock: clock,
}
fs.vfsfs.Init(vfsObj, &fstype, &fs)
root := fs.newDentry(fs.newDirectory(creds, 01777))
return &fs.vfsfs, &root.vfsd, nil
typ := uint16(linux.S_IFDIR)
tmpfsOpts, ok := opts.InternalData.(FilesystemOpts)
if ok && tmpfsOpts.RootFileType != 0 {
typ = tmpfsOpts.RootFileType
}
var root *inode
switch typ {
case linux.S_IFREG:
root = fs.newRegularFile(creds, 0777)
case linux.S_IFLNK:
root = fs.newSymlink(creds, tmpfsOpts.RootSymlinkTarget)
case linux.S_IFDIR:
root = fs.newDirectory(creds, 01777)
default:
return nil, nil, fmt.Errorf("invalid tmpfs root file type: %#o", typ)
}
return &fs.vfsfs, &fs.newDentry(root).vfsd, nil
}
// Release implements vfs.FilesystemImpl.Release.