xref: /aosp_15_r20/test/dittosuite/src/resize_file.cpp (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
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/resize_file.h>
16 
17 #include <ditto/logger.h>
18 #include <ditto/shared_variables.h>
19 
20 #include <ditto/utils.h>
21 
22 namespace dittosuite {
23 
ResizeFile(const Params & params,int64_t size,int input_fd_key)24 ResizeFile::ResizeFile(const Params& params, int64_t size, int input_fd_key)
25     : Instruction(kName, params), size_(size), input_fd_key_(input_fd_key) {}
26 
RunSingle()27 void ResizeFile::RunSingle() {
28   int fd = std::get<int>(SharedVariables::Get(input_fd_key_));
29   int64_t file_size = GetFileSize(syscall_, fd);
30 
31   if (size_ > file_size) {
32     if (syscall_.FAllocate(fd, 0, 0, size_) != 0) {
33       PLOGF("Error while calling fallocate()");
34     }
35   } else if (size_ < file_size) {
36     if (syscall_.FTruncate(fd, size_) != 0) {
37       PLOGF("Error while calling ftruncate()");
38     }
39   }
40 }
41 
ResizeFileRandom(const Params & params,int64_t min,int64_t max,uint64_t seed,Reseeding reseeding,int input_fd_key)42 ResizeFileRandom::ResizeFileRandom(const Params& params, int64_t min, int64_t max, uint64_t seed,
43                                    Reseeding reseeding, int input_fd_key)
44     : ResizeFile(params, -1, input_fd_key),
45       min_(min),
46       max_(max),
47       gen_(seed),
48       seed_(seed),
49       reseeding_(reseeding) {}
50 
SetUp()51 void ResizeFileRandom::SetUp() {
52   if (reseeding_ == Reseeding::kEachRoundOfCycles) {
53     gen_.seed(seed_);
54   }
55   Instruction::SetUp();
56 }
57 
SetUpSingle()58 void ResizeFileRandom::SetUpSingle() {
59   if (reseeding_ == Reseeding::kEachCycle) {
60     gen_.seed(seed_);
61   }
62 
63   std::uniform_int_distribution<> uniform_distribution(min_, max_);
64   size_ = uniform_distribution(gen_);
65 
66   Instruction::SetUpSingle();
67 }
68 
69 }  // namespace dittosuite
70