Skip to content

Commit 3486ca4

Browse files
laramielcopybara-github
authored andcommitted
Revert to mode 0666 for "file" kvstore.
Fixes: #220 PiperOrigin-RevId: 735593374 Change-Id: I0fc07212f5b676d590062377d0d5171956deee9a
1 parent f43a7f5 commit 3486ca4

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

tensorstore/internal/os/file_util.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,42 @@ inline absl::Time GetMTime(const FileInfo& info) {
293293
#endif
294294
}
295295

296+
/// Returns the creation time.
297+
inline absl::Time GetCTime(const FileInfo& info) {
298+
#ifdef _WIN32
299+
// Windows FILETIME is the number of 100-nanosecond intervals since the
300+
// Windows epoch (1601-01-01) which is 11644473600 seconds before the unix
301+
// epoch (1970-01-01).
302+
uint64_t windowsTicks =
303+
(static_cast<uint64_t>(info.ftCreationTime.dwHighDateTime) << 32) |
304+
static_cast<uint64_t>(info.ftCreationTime.dwLowDateTime);
305+
306+
return absl::UnixEpoch() +
307+
absl::Seconds((windowsTicks / 10000000) - 11644473600ULL) +
308+
absl::Nanoseconds(windowsTicks % 10000000);
309+
#else
310+
#if defined(__APPLE__)
311+
const struct ::timespec t = info.st_ctimespec;
312+
#else
313+
const struct ::timespec t = info.st_ctim;
314+
#endif
315+
return absl::FromTimeT(t.tv_sec) + absl::Nanoseconds(t.tv_nsec);
316+
#endif
317+
}
318+
319+
/// Returns the mode bits.
320+
inline uint32_t GetMode(const FileInfo& info) {
321+
#ifdef _WIN32
322+
if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
323+
return 0444; // read-only
324+
} else {
325+
return 0666; // read/write
326+
}
327+
#else
328+
return info.st_mode;
329+
#endif
330+
}
331+
296332
/// --------------------------------------------------------------------------
297333

298334
/// Opens a directory.

tensorstore/internal/os/file_util_posix.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Result<UniqueFileDescriptor> OpenFileWrapper(const std::string& path,
228228
FileDescriptor fd = FileDescriptorTraits::Invalid();
229229
const auto attempt_open = [&] {
230230
PotentiallyBlockingRegion region;
231-
fd = ::open(path.c_str(), static_cast<int>(flags) | O_CLOEXEC, 0660);
231+
fd = ::open(path.c_str(), static_cast<int>(flags) | O_CLOEXEC, 0666);
232232
};
233233
#ifndef __APPLE__
234234
attempt_open();

tensorstore/internal/os/file_util_test.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ using ::tensorstore::internal_os::DeleteFile;
4040
using ::tensorstore::internal_os::DeleteOpenFile;
4141
using ::tensorstore::internal_os::FileInfo;
4242
using ::tensorstore::internal_os::FsyncFile;
43+
using ::tensorstore::internal_os::GetCTime;
4344
using ::tensorstore::internal_os::GetDefaultPageSize;
4445
using ::tensorstore::internal_os::GetDeviceId;
4546
using ::tensorstore::internal_os::GetFileId;
4647
using ::tensorstore::internal_os::GetFileInfo;
48+
using ::tensorstore::internal_os::GetMode;
4749
using ::tensorstore::internal_os::GetMTime;
4850
using ::tensorstore::internal_os::GetSize;
4951
using ::tensorstore::internal_os::IsDirSeparator;
@@ -86,7 +88,6 @@ TEST(FileUtilTest, Basics) {
8688

8789
EXPECT_THAT(WriteCordToFile(f->get(), absl::Cord("foo")), IsOkAndHolds(3));
8890
EXPECT_THAT(WriteToFile(f->get(), "bar", 3), IsOkAndHolds(3));
89-
9091
EXPECT_THAT(FsyncFile(f->get()), IsOk());
9192
}
9293

@@ -95,6 +96,7 @@ TEST(FileUtilTest, Basics) {
9596
char buf[16];
9697
auto f = OpenExistingFileForReading(foo_txt);
9798
EXPECT_THAT(f, IsOk());
99+
98100
EXPECT_THAT(ReadFromFile(f->get(), buf, 3, 0), IsOkAndHolds(3));
99101

100102
// Check the file info
@@ -106,6 +108,8 @@ TEST(FileUtilTest, Basics) {
106108
EXPECT_THAT(GetFileId(info), ::testing::Ne(0));
107109
EXPECT_THAT(GetDeviceId(info), ::testing::Ne(0));
108110
EXPECT_THAT(GetMTime(info), ::testing::Ge(now));
111+
EXPECT_THAT(GetCTime(info), ::testing::Ge(now));
112+
EXPECT_THAT(GetMode(info) & 0600, ::testing::Eq(0600));
109113

110114
EXPECT_THAT(RenameOpenFile(f->get(), foo_txt, renamed_txt), IsOk());
111115
}

tensorstore/internal/os/file_util_win.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,6 @@ std::string_view GetDirName(std::string_view path) {
177177
return path.substr(0, i);
178178
}
179179

180-
#if 0
181-
Result<DWORD> GetFileAttributes(const std::wstring& filename) {
182-
if (const DWORD attrs = ::GetFileAttributesW(filename.c_str());
183-
attrs != INVALID_FILE_ATTRIBUTES) {
184-
return attrs;
185-
}
186-
return StatusFromOsError(::GetLastError(), "GetFileAttributesW failed");
187-
}
188-
#endif
189-
190180
void UnlockWin32Lock(FileDescriptor fd) {
191181
LoggedTraceSpan tspan(__func__, detail_logging.Level(1), {{"handle", fd}});
192182

0 commit comments

Comments
 (0)