github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/fuse_fd_util.cc (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #include "test/fuse/linux/fuse_fd_util.h"
    16  
    17  #include <fcntl.h>
    18  #include <linux/fuse.h>
    19  #include <sys/stat.h>
    20  #include <sys/types.h>
    21  #include <sys/uio.h>
    22  
    23  #include <string>
    24  #include <vector>
    25  
    26  #include "test/util/cleanup.h"
    27  #include "test/util/file_descriptor.h"
    28  #include "test/util/fuse_util.h"
    29  #include "test/util/posix_error.h"
    30  
    31  namespace gvisor {
    32  namespace testing {
    33  
    34  PosixErrorOr<FileDescriptor> FuseFdTest::OpenPath(const std::string &path,
    35                                                    uint32_t flags, uint64_t fh) {
    36    struct fuse_out_header out_header = {
    37        .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_open_out),
    38    };
    39    struct fuse_open_out out_payload = {
    40        .fh = fh,
    41        .open_flags = flags,
    42    };
    43    auto iov_out = FuseGenerateIovecs(out_header, out_payload);
    44    SetServerResponse(FUSE_OPEN, iov_out);
    45  
    46    auto res = Open(path.c_str(), flags);
    47    if (res.ok()) {
    48      SkipServerActualRequest();
    49    }
    50    return res;
    51  }
    52  
    53  Cleanup FuseFdTest::CloseFD(FileDescriptor &fd) {
    54    return Cleanup([&] {
    55      close(fd.release());
    56      SkipServerActualRequest();
    57    });
    58  }
    59  
    60  }  // namespace testing
    61  }  // namespace gvisor