Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ファイルの書き込み時に更新日時を更新する #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions kernel/fat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <cctype>
#include <utility>

#include "uefi.hpp"

namespace {

std::pair<const char*, bool>
Expand Down Expand Up @@ -203,9 +205,24 @@ void SetFileName(DirectoryEntry& entry, const char* name) {
}
}

void SetWriteTime(DirectoryEntry& entry, const EFI_TIME& write_time) {
uint16_t time_encoded =
((write_time.Hour & 0x1f) << 11) |
((write_time.Minute & 0x3f) << 5) |
((write_time.Second / 2) & 0x1f);
uint16_t date_encoded =
(((write_time.Year - 1980) & 0x7f) << 9) |
((write_time.Month & 0xf) << 5) |
(write_time.Day & 0x1f);

entry.write_time = time_encoded;
entry.write_date = date_encoded;
}

WithError<DirectoryEntry*> CreateFile(const char* path) {
auto parent_dir_cluster = fat::boot_volume_image->root_cluster;
const char* filename = path;
DirectoryEntry* parent_dir_entry = nullptr;

if (const char* slash_pos = strrchr(path, '/')) {
filename = &slash_pos[1];
Expand All @@ -222,6 +239,7 @@ WithError<DirectoryEntry*> CreateFile(const char* path) {
if (parent_dir == nullptr) {
return { nullptr, MAKE_ERROR(Error::kNoSuchEntry) };
}
parent_dir_entry = parent_dir;
parent_dir_cluster = parent_dir->FirstCluster();
}
}
Expand All @@ -230,8 +248,14 @@ WithError<DirectoryEntry*> CreateFile(const char* path) {
if (dir == nullptr) {
return { nullptr, MAKE_ERROR(Error::kNoEnoughMemory) };
}
EFI_TIME current_time;
uefi_rt->GetTime(&current_time, nullptr);
fat::SetFileName(*dir, filename);
fat::SetWriteTime(*dir, current_time);
dir->file_size = 0;
if (parent_dir_entry != nullptr) {
fat::SetWriteTime(*parent_dir_entry, current_time);
}
return { dir, MAKE_ERROR(Error::kSuccess) };
}

Expand Down Expand Up @@ -319,6 +343,10 @@ size_t FileDescriptor::Write(const void* buf, size_t len) {

wr_off_ += total;
fat_entry_.file_size = wr_off_;

EFI_TIME current_time;
uefi_rt->GetTime(&current_time, nullptr);
fat::SetWriteTime(fat_entry_, current_time);
return total;
}

Expand Down
8 changes: 8 additions & 0 deletions kernel/fat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "error.hpp"
#include "file.hpp"
#include "uefi.hpp"

namespace fat {

Expand Down Expand Up @@ -171,6 +172,13 @@ DirectoryEntry* AllocateEntry(unsigned long dir_cluster);
*/
void SetFileName(DirectoryEntry& entry, const char* name);

/** @brief ディレクトリエントリの更新日時をセットする。
*
* @param entry 更新日時を設定する対象のディレクトリエントリ
* @param write_time 更新日時として設定する日時
*/
void SetWriteTime(DirectoryEntry& entry, const EFI_TIME& write_time);

/** @brief 指定されたパスにファイルエントリを作成する。
*
* @param path ファイルパス
Expand Down