blob: 4173522a66af299436f9a9996f66e6b29aa84f1c [file] [log] [blame]
// Copyright (C) 2021 The Android Open Source Project
//
// 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 <ditto/resize_file.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstdint>
#include <ditto/logger.h>
#include <ditto/shared_variables.h>
#include <ditto/utils.h>
namespace dittosuite {
ResizeFile::ResizeFile(int repeat, int64_t size, int input_fd_key)
: Instruction(kName, repeat), size_(size), input_fd_key_(input_fd_key) {}
void ResizeFile::RunSingle() {
int fd = std::get<int>(SharedVariables::Get(input_fd_key_));
int64_t file_size = GetFileSize(fd);
if (size_ > file_size && fallocate(fd, 0, 0, size_) != 0) {
LOGF("Error while calling fallocate()");
} else if (ftruncate(fd, size_) != 0) {
LOGF("Error while calling ftruncate()");
}
}
ResizeFileRandom::ResizeFileRandom(int repeat, int64_t min, int64_t max, uint64_t seed,
Reseeding reseeding, int input_fd_key)
: ResizeFile(repeat, -1, input_fd_key),
min_(min),
max_(max),
gen_(seed),
seed_(seed),
reseeding_(reseeding) {}
void ResizeFileRandom::SetUp() {
if (reseeding_ == kEachRoundOfCycles) {
gen_.seed(seed_);
}
Instruction::SetUp();
}
void ResizeFileRandom::SetUpSingle() {
if (reseeding_ == kEachCycle) {
gen_.seed(seed_);
}
std::uniform_int_distribution<> uniform_distribution(min_, max_);
size_ = uniform_distribution(gen_);
Instruction::SetUpSingle();
}
} // namespace dittosuite