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

Fix Windows Timestamps for Alternate Data Streams #837

Merged
merged 5 commits into from
Jan 8, 2025
Merged
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
17 changes: 7 additions & 10 deletions mz_os_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ static void mz_os_unix_to_file_time(time_t unix_time, FILETIME *file_time) {
}

int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date) {
WIN32_FIND_DATAW ff32;
HANDLE handle = NULL;
WIN32_FILE_ATTRIBUTE_DATA wfad;
wchar_t *path_wide = NULL;
int32_t err = MZ_INTERNAL_ERROR;

Expand All @@ -245,21 +244,19 @@ int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *acc
if (!path_wide)
return MZ_PARAM_ERROR;

handle = FindFirstFileW(path_wide, &ff32);
free(path_wide);

if (handle != INVALID_HANDLE_VALUE) {
if (GetFileAttributesExW(path_wide, GetFileExInfoStandard, &wfad)) {
if (modified_date)
mz_os_file_to_unix_time(ff32.ftLastWriteTime, modified_date);
mz_os_file_to_unix_time(wfad.ftLastWriteTime, modified_date);
if (accessed_date)
mz_os_file_to_unix_time(ff32.ftLastAccessTime, accessed_date);
mz_os_file_to_unix_time(wfad.ftLastAccessTime, accessed_date);
if (creation_date)
mz_os_file_to_unix_time(ff32.ftCreationTime, creation_date);
mz_os_file_to_unix_time(wfad.ftCreationTime, creation_date);

FindClose(handle);
err = MZ_OK;
}

free(path_wide);

return err;
}

Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ if(NOT MZ_COMPRESS_ONLY AND NOT MZ_DECOMPRESS_ONLY)
list(APPEND TEST_SRCS test_stream_compress.cc)
endif()

if(MSVC)
list(APPEND TEST_SRCS test_file.cc)
endif()

add_executable(gtest_minizip test_main.cc ${TEST_SRCS})
target_compile_definitions(gtest_minizip PRIVATE ${STDLIB_DEF} ${MINIZIP_DEF})
target_include_directories(gtest_minizip PRIVATE
Expand Down
44 changes: 44 additions & 0 deletions test/test_file.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* test_file.cc - Test file functionality
part of the minizip-ng project

Copyright (C) Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng

This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/

#include "mz.h"
#include "mz_os.h"

#include <gtest/gtest.h>
#include <fstream>
#include <cstdio>

TEST(os, get_file_date_ads) {
const std::string main_stream_name = "minizip_ads_test";
const std::string ads_name = main_stream_name + ":ads";
const std::string ads_contents = "Alternate Data Stream";

// Create main stream
std::ofstream main_stream(main_stream_name);
main_stream.close();

// Attach ADS
std::ofstream ads(ads_name);
ads << ads_contents;
ads.close();

// Get file date
time_t modified_date = 0;
time_t accessed_date = 0;
time_t creation_date = 0;

EXPECT_EQ(MZ_OK, mz_os_get_file_date(ads_name.c_str(), &modified_date, &accessed_date, &creation_date));

std::remove(main_stream_name.c_str());

ASSERT_GT(modified_date, 0);
ASSERT_GT(accessed_date, 0);
ASSERT_GT(creation_date, 0);
}
Loading