1 // Copyright (C) 2021 The Android Open Source Project
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 <ditto/open_file.h>
16
17 #include <ditto/logger.h>
18 #include <ditto/shared_variables.h>
19 #include <ditto/utils.h>
20
21 namespace dittosuite {
22
OpenFile(const Params & params,const std::string & path_name,bool create,bool direct_io,int output_fd_key,AccessMode access_mode)23 OpenFile::OpenFile(const Params& params, const std::string& path_name, bool create, bool direct_io,
24 int output_fd_key, AccessMode access_mode)
25 : Instruction(kName, params),
26 random_name_(false),
27 path_name_(GetAbsolutePath() + path_name),
28 create_(create),
29 direct_io_(direct_io),
30 input_key_(-1),
31 output_fd_key_(output_fd_key),
32 access_mode_(access_mode) {}
33
OpenFile(const Params & params,int input_key,bool create,bool direct_io,int output_fd_key,AccessMode access_mode)34 OpenFile::OpenFile(const Params& params, int input_key, bool create, bool direct_io,
35 int output_fd_key, AccessMode access_mode)
36 : Instruction(kName, params),
37 random_name_(false),
38 create_(create),
39 direct_io_(direct_io),
40 input_key_(input_key),
41 output_fd_key_(output_fd_key),
42 access_mode_(access_mode) {}
43
OpenFile(const Params & params,bool create,bool direct_io,int output_fd_key,AccessMode access_mode)44 OpenFile::OpenFile(const Params& params, bool create, bool direct_io, int output_fd_key,
45 AccessMode access_mode)
46 : Instruction(kName, params),
47 random_name_(true),
48 create_(create),
49 direct_io_(direct_io),
50 input_key_(-1),
51 output_fd_key_(output_fd_key),
52 gen_(time(nullptr)),
53 access_mode_(access_mode) {}
54
SetUpSingle()55 void OpenFile::SetUpSingle() {
56 if (input_key_ != -1) {
57 path_name_ = std::get<std::string>(SharedVariables::Get(input_key_));
58 } else if (random_name_) {
59 std::uniform_int_distribution<> uniform_distribution(1e8, 9e8); // 9 digit number
60 do {
61 path_name_ = GetAbsolutePath() + std::to_string(uniform_distribution(gen_));
62 } while (FileExists(syscall_, path_name_));
63 }
64 Instruction::SetUpSingle();
65 }
66
RunSingle()67 void OpenFile::RunSingle() {
68 int open_mode = S_IRUSR | S_IWUSR;
69
70 int open_flags = 0;
71 switch (access_mode_) {
72 case AccessMode::kReadOnly:
73 open_flags |= O_RDONLY;
74 break;
75 case AccessMode::kWriteOnly:
76 open_flags |= O_WRONLY;
77 break;
78 case AccessMode::kReadWrite:
79 open_flags |= O_RDWR;
80 break;
81 }
82 open_flags |= O_CLOEXEC;
83 if (create_) open_flags |= O_CREAT;
84 if (direct_io_) open_flags |= O_DIRECT;
85
86 int fd = syscall_.Open(path_name_, open_flags, open_mode);
87
88 if (fd == -1) {
89 PLOGF("Cannot open \"" + path_name_ + "\"");
90 }
91
92 if (output_fd_key_ != -1) {
93 SharedVariables::Set(output_fd_key_, fd);
94 }
95 }
96
97 } // namespace dittosuite
98