Skip to content

Commit

Permalink
change create to open-create, create the file only if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkdev committed Feb 16, 2024
1 parent 3e71a26 commit 631fe60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/wfslib/file_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FileDevice : public Device {
uint32_t log2_sector_size = 9 /* 512 */,
uint32_t sectors_count = 0,
bool read_only = true,
bool create = false);
bool open_create = false);
void ReadSectors(const std::span<std::byte>& data, uint32_t sector_address, uint32_t sectors_count) override;
void WriteSectors(const std::span<std::byte>& data, uint32_t sector_address, uint32_t sectors_count) override;
uint32_t SectorsCount() const override { return sectors_count_; }
Expand Down
13 changes: 8 additions & 5 deletions src/file_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ FileDevice::FileDevice(const std::string& path,
uint32_t log2_sector_size,
uint32_t sectors_count,
bool read_only,
bool create)
bool open_create)
: log2_sector_size_(log2_sector_size), sectors_count_(sectors_count), read_only_(read_only) {
std::ios_base::openmode mode = std::ios::binary;
if (!create)
mode |= std::ios::in;
if (create || !read_only)
std::ios_base::openmode mode = std::ios::binary | std::ios::in;
if (!read_only)
mode |= std::ios::out;
file_.reset(new std::fstream(path, mode));
if (file_->fail() && open_create) {
// try to create the file
mode |= std::ios::trunc;
file_.reset(new std::fstream(path, mode));
}
if (file_->fail()) {
throw std::runtime_error("FileDevice: Failed to open file");
}
Expand Down

0 comments on commit 631fe60

Please sign in to comment.