mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
@@ -494,3 +494,41 @@ func (r *FUSEEmptyIn) MarshalUnsafe(buf []byte) {}
|
||||
func (r *FUSEEmptyIn) SizeBytes() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// FUSEMkdirMeta contains all the static fields of FUSEMkdirIn,
|
||||
// which is used for FUSE_MKDIR.
|
||||
//
|
||||
// +marshal
|
||||
type FUSEMkdirMeta struct {
|
||||
// Mode of the directory of create.
|
||||
Mode uint32
|
||||
|
||||
// Umask is the user file creation mask.
|
||||
Umask uint32
|
||||
}
|
||||
|
||||
// FUSEMkdirIn contains all the arguments sent by the kernel
|
||||
// to the daemon, to create a new directory.
|
||||
//
|
||||
// Dynamically-sized objects cannot be marshalled.
|
||||
type FUSEMkdirIn struct {
|
||||
marshal.StubMarshallable
|
||||
|
||||
// MkdirMeta contains Mode and Umask of the directory to create.
|
||||
MkdirMeta FUSEMkdirMeta
|
||||
|
||||
// Name of the directory to create.
|
||||
Name string
|
||||
}
|
||||
|
||||
// MarshalUnsafe serializes r.MkdirMeta and r.Name to the dst buffer.
|
||||
func (r *FUSEMkdirIn) MarshalUnsafe(buf []byte) {
|
||||
r.MkdirMeta.MarshalUnsafe(buf[:r.MkdirMeta.SizeBytes()])
|
||||
copy(buf[r.MkdirMeta.SizeBytes():], r.Name)
|
||||
}
|
||||
|
||||
// SizeBytes is the size of the memory representation of FUSEMkdirIn.
|
||||
// 1 extra byte for null-terminated Name string.
|
||||
func (r *FUSEMkdirIn) SizeBytes() int {
|
||||
return r.MkdirMeta.SizeBytes() + len(r.Name) + 1
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ go_library(
|
||||
srcs = [
|
||||
"connection.go",
|
||||
"dev.go",
|
||||
"directory.go",
|
||||
"file.go",
|
||||
"fusefs.go",
|
||||
"init.go",
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2020 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package fuse
|
||||
|
||||
import (
|
||||
"gvisor.dev/gvisor/pkg/context"
|
||||
"gvisor.dev/gvisor/pkg/sentry/vfs"
|
||||
"gvisor.dev/gvisor/pkg/syserror"
|
||||
"gvisor.dev/gvisor/pkg/usermem"
|
||||
)
|
||||
|
||||
type directoryFD struct {
|
||||
fileDescription
|
||||
}
|
||||
|
||||
// Allocate implements directoryFD.Allocate.
|
||||
func (directoryFD) Allocate(ctx context.Context, mode, offset, length uint64) error {
|
||||
return syserror.EISDIR
|
||||
}
|
||||
|
||||
// PRead implements FileDescriptionImpl.PRead.
|
||||
func (directoryFD) PRead(ctx context.Context, dst usermem.IOSequence, offset int64, opts vfs.ReadOptions) (int64, error) {
|
||||
return 0, syserror.EISDIR
|
||||
}
|
||||
|
||||
// Read implements FileDescriptionImpl.Read.
|
||||
func (directoryFD) Read(ctx context.Context, dst usermem.IOSequence, opts vfs.ReadOptions) (int64, error) {
|
||||
return 0, syserror.EISDIR
|
||||
}
|
||||
|
||||
// PWrite implements FileDescriptionImpl.PWrite.
|
||||
func (directoryFD) PWrite(ctx context.Context, src usermem.IOSequence, offset int64, opts vfs.WriteOptions) (int64, error) {
|
||||
return 0, syserror.EISDIR
|
||||
}
|
||||
|
||||
// Write implements FileDescriptionImpl.Write.
|
||||
func (directoryFD) Write(ctx context.Context, src usermem.IOSequence, opts vfs.WriteOptions) (int64, error) {
|
||||
return 0, syserror.EISDIR
|
||||
}
|
||||
@@ -262,8 +262,17 @@ func (i *inode) Open(ctx context.Context, rp *vfs.ResolvingPath, vfsd *vfs.Dentr
|
||||
return nil, syserror.EOVERFLOW
|
||||
}
|
||||
|
||||
// FOPEN_KEEP_CACHE is the defualt flag for noOpen.
|
||||
fd := fileDescription{OpenFlag: linux.FOPEN_KEEP_CACHE}
|
||||
var fd *fileDescription
|
||||
var fdImpl vfs.FileDescriptionImpl
|
||||
if isDir {
|
||||
directoryFD := &directoryFD{}
|
||||
fd = &(directoryFD.fileDescription)
|
||||
fdImpl = directoryFD
|
||||
} else {
|
||||
// FOPEN_KEEP_CACHE is the defualt flag for noOpen.
|
||||
fd = &fileDescription{OpenFlag: linux.FOPEN_KEEP_CACHE}
|
||||
fdImpl = fd
|
||||
}
|
||||
// Only send open request when FUSE server support open or is opening a directory.
|
||||
if !i.fs.conn.noOpen || isDir {
|
||||
kernelTask := kernel.TaskFromContext(ctx)
|
||||
@@ -330,7 +339,7 @@ func (i *inode) Open(ctx context.Context, rp *vfs.ResolvingPath, vfsd *vfs.Dentr
|
||||
i.attributeTime = 0
|
||||
}
|
||||
|
||||
if err := fd.vfsfd.Init(&fd, opts.Flags, rp.Mount(), vfsd, fdOptions); err != nil {
|
||||
if err := fd.vfsfd.Init(fdImpl, opts.Flags, rp.Mount(), vfsd, fdOptions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fd.vfsfd, nil
|
||||
@@ -374,6 +383,18 @@ func (i *inode) NewSymlink(ctx context.Context, name, target string) (*vfs.Dentr
|
||||
return i.newEntry(ctx, name, linux.S_IFLNK, linux.FUSE_SYMLINK, &in)
|
||||
}
|
||||
|
||||
// NewDir implements kernfs.Inode.NewDir.
|
||||
func (i *inode) NewDir(ctx context.Context, name string, opts vfs.MkdirOptions) (*vfs.Dentry, error) {
|
||||
in := linux.FUSEMkdirIn{
|
||||
MkdirMeta: linux.FUSEMkdirMeta{
|
||||
Mode: uint32(opts.Mode),
|
||||
Umask: uint32(kernel.TaskFromContext(ctx).FSContext().Umask()),
|
||||
},
|
||||
Name: name,
|
||||
}
|
||||
return i.newEntry(ctx, name, linux.S_IFDIR, linux.FUSE_MKDIR, &in)
|
||||
}
|
||||
|
||||
// newEntry calls FUSE server for entry creation and allocates corresponding entry according to response.
|
||||
// Shared by FUSE_MKNOD, FUSE_MKDIR, FUSE_SYMLINK, FUSE_LINK and FUSE_LOOKUP.
|
||||
func (i *inode) newEntry(ctx context.Context, name string, fileType linux.FileMode, opcode linux.FUSEOpcode, payload marshal.Marshallable) (*vfs.Dentry, error) {
|
||||
|
||||
@@ -31,3 +31,8 @@ syscall_test(
|
||||
fuse = "True",
|
||||
test = "//test/fuse/linux:readlink_test",
|
||||
)
|
||||
|
||||
syscall_test(
|
||||
fuse = "True",
|
||||
test = "//test/fuse/linux:mkdir_test",
|
||||
)
|
||||
|
||||
@@ -84,6 +84,20 @@ cc_binary(
|
||||
],
|
||||
)
|
||||
|
||||
cc_binary(
|
||||
name = "mkdir_test",
|
||||
testonly = 1,
|
||||
srcs = ["mkdir_test.cc"],
|
||||
deps = [
|
||||
gtest,
|
||||
":fuse_base",
|
||||
"//test/util:fuse_util",
|
||||
"//test/util:temp_umask",
|
||||
"//test/util:test_main",
|
||||
"//test/util:test_util",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "fuse_base",
|
||||
testonly = 1,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
// Copyright 2020 The gVisor Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/fuse.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "test/fuse/linux/fuse_base.h"
|
||||
#include "test/util/fuse_util.h"
|
||||
#include "test/util/temp_umask.h"
|
||||
#include "test/util/test_util.h"
|
||||
|
||||
namespace gvisor {
|
||||
namespace testing {
|
||||
|
||||
namespace {
|
||||
|
||||
class MkdirTest : public FuseTest {
|
||||
protected:
|
||||
const std::string test_dir_ = "test_dir";
|
||||
const mode_t perms_ = S_IRWXU | S_IRWXG | S_IRWXO;
|
||||
};
|
||||
|
||||
TEST_F(MkdirTest, CreateDir) {
|
||||
const std::string test_dir_path_ =
|
||||
JoinPath(mount_point_.path().c_str(), test_dir_);
|
||||
const mode_t new_umask = 0077;
|
||||
|
||||
struct fuse_out_header out_header = {
|
||||
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out),
|
||||
};
|
||||
struct fuse_entry_out out_payload = DefaultEntryOut(S_IFDIR | perms_, 5);
|
||||
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
|
||||
SetServerResponse(FUSE_MKDIR, iov_out);
|
||||
TempUmask mask(new_umask);
|
||||
ASSERT_THAT(mkdir(test_dir_path_.c_str(), 0777), SyscallSucceeds());
|
||||
|
||||
struct fuse_in_header in_header;
|
||||
struct fuse_mkdir_in in_payload;
|
||||
std::vector<char> actual_dir(test_dir_.length() + 1);
|
||||
auto iov_in = FuseGenerateIovecs(in_header, in_payload, actual_dir);
|
||||
GetServerActualRequest(iov_in);
|
||||
|
||||
EXPECT_EQ(in_header.len,
|
||||
sizeof(in_header) + sizeof(in_payload) + test_dir_.length() + 1);
|
||||
EXPECT_EQ(in_header.opcode, FUSE_MKDIR);
|
||||
EXPECT_EQ(in_payload.mode & 0777, perms_ & ~new_umask);
|
||||
EXPECT_EQ(in_payload.umask, new_umask);
|
||||
EXPECT_EQ(std::string(actual_dir.data()), test_dir_);
|
||||
}
|
||||
|
||||
TEST_F(MkdirTest, FileTypeError) {
|
||||
const std::string test_dir_path_ =
|
||||
JoinPath(mount_point_.path().c_str(), test_dir_);
|
||||
|
||||
struct fuse_out_header out_header = {
|
||||
.len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out),
|
||||
};
|
||||
struct fuse_entry_out out_payload = DefaultEntryOut(S_IFREG | perms_, 5);
|
||||
auto iov_out = FuseGenerateIovecs(out_header, out_payload);
|
||||
SetServerResponse(FUSE_MKDIR, iov_out);
|
||||
ASSERT_THAT(mkdir(test_dir_path_.c_str(), 0777), SyscallFailsWithErrno(EIO));
|
||||
SkipServerActualRequest();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace testing
|
||||
} // namespace gvisor
|
||||
Reference in New Issue
Block a user