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 1 commit
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
25 changes: 25 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,26 @@ void SetFileName(DirectoryEntry& entry, const char* name) {
}
}

void SetWriteTimeToCurrent(DirectoryEntry& entry) {
EFI_TIME t;
uefi_rt->GetTime(&t, nullptr);
uint16_t current_time =
((t.Hour & 0x1f) << 11) |
((t.Minute & 0x3f) << 5) |
((t.Second / 2) & 0x1f);
uint16_t current_date =
(((t.Year - 1980) & 0x7f) << 9) |
((t.Month & 0xf) << 5) |
(t.Day & 0x1f);

entry.write_time = current_time;
entry.write_date = current_date;
}

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 +241,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 @@ -231,7 +251,11 @@ WithError<DirectoryEntry*> CreateFile(const char* path) {
return { nullptr, MAKE_ERROR(Error::kNoEnoughMemory) };
}
fat::SetFileName(*dir, filename);
fat::SetWriteTimeToCurrent(*dir);
dir->file_size = 0;
if (parent_dir_entry != nullptr) {
fat::SetWriteTimeToCurrent(*parent_dir_entry);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このやり方ではファイルと親ディレクトリの時刻がずれてしまいます。どうせやるなら同時刻を書きたいです。
SetWriteTimeToCurrentEFI_TIME* を引数に取るようにして、時刻取得は 1 回で済ませるようにしてください。

Linux ではぴったり同じ時刻になるという証拠:

$ mkdir foo_dir
$ ls -la --full-time foo_dir
total 8
drwxr-xr-x 2 uchan uchan 4096 2022-08-26 14:25:51.561915460 +0900 .
drwxr-xr-x 3 uchan uchan 4096 2022-08-26 14:25:51.561915460 +0900 ..
$ touch foo_dir/bar_file
$ ls -la --full-time foo_dir
total 8
drwxr-xr-x 2 uchan uchan 4096 2022-08-26 14:26:04.541915575 +0900 .
drwxr-xr-x 3 uchan uchan 4096 2022-08-26 14:25:51.561915460 +0900 ..
-rw-r--r-- 1 uchan uchan    0 2022-08-26 14:26:04.541915575 +0900 bar_file
$ touch foo_dir/bar_file
$ ls -la --full-time foo_dir
total 8
drwxr-xr-x 2 uchan uchan 4096 2022-08-26 14:26:04.541915575 +0900 .
drwxr-xr-x 3 uchan uchan 4096 2022-08-26 14:25:51.561915460 +0900 ..
-rw-r--r-- 1 uchan uchan    0 2022-08-26 14:26:11.201915635 +0900 bar_file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました。

}
return { dir, MAKE_ERROR(Error::kSuccess) };
}

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

wr_off_ += total;
fat_entry_.file_size = wr_off_;
fat::SetWriteTimeToCurrent(fat_entry_);
return total;
}

Expand Down
6 changes: 6 additions & 0 deletions kernel/fat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ DirectoryEntry* AllocateEntry(unsigned long dir_cluster);
*/
void SetFileName(DirectoryEntry& entry, const char* name);

/** @brief ディレクトリエントリの更新日時を現在時刻にセットする。
*
* @param entry 更新日時を設定する対象のディレクトリエントリ
*/
void SetWriteTimeToCurrent(DirectoryEntry& entry);

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