Skip to content

Commit

Permalink
improve copy file for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Feb 6, 2024
1 parent cb91a14 commit c8b7118
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/tbox/platform/windows/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ tb_bool_t tb_file_info(tb_char_t const* path, tb_file_info_t* info)
if (st.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) info->type = TB_FILE_TYPE_DIRECTORY;
else if (st.dwFileAttributes != 0xffffffff) info->type = TB_FILE_TYPE_FILE;

// TODO does not support symlink now
// is symlink?
info->flags = TB_FILE_FLAG_NONE;
if (st.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
info->flags |= TB_FILE_FLAG_LINK;

// file size
info->size = ((tb_hize_t)st.nFileSizeHigh << 32) | (tb_hize_t)st.nFileSizeLow;
Expand Down Expand Up @@ -436,17 +438,22 @@ tb_bool_t tb_file_copy(tb_char_t const* path, tb_char_t const* dest, tb_size_t f
tb_wchar_t full1[TB_PATH_MAXN];
if (!tb_path_absolute_w(dest, full1, TB_PATH_MAXN)) return tb_false;

// copy link
tb_file_info_t info = {0};
if (flags & TB_FILE_COPY_LINK && tb_file_info(path, &info) && info.flags & TB_FILE_FLAG_LINK)
{
if (tb_kernel32()->CopyFileW)
return (tb_bool_t)tb_kernel32()->CopyFileW(full0, full1, tb_null, tb_null, FALSE, COPY_FILE_COPY_SYMLINK);
// TODO we should read file content to copy it
// ...
}

// copy it
if (!CopyFileW(full0, full1, FALSE))
{
// make directory
tb_file_mkdir(full1);

// copy it again
return (tb_bool_t)CopyFileW(full0, full1, FALSE);
}

// ok
return tb_true;
}
tb_bool_t tb_file_create(tb_char_t const* path)
Expand Down
1 change: 1 addition & 0 deletions src/tbox/platform/windows/interface/kernel32.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ static tb_bool_t tb_kernel32_instance_init(tb_kernel32_ref_t kernel32)
TB_INTERFACE_LOAD(kernel32, InitializeProcThreadAttributeList);
TB_INTERFACE_LOAD(kernel32, UpdateProcThreadAttribute);
TB_INTERFACE_LOAD(kernel32, DeleteProcThreadAttributeList);
TB_INTERFACE_LOAD(kernel32, CopyFileExW);
#if defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BT(16, 0)
TB_INTERFACE_LOAD(kernel32, GetLogicalProcessorInformationEx);
#endif
Expand Down
12 changes: 12 additions & 0 deletions src/tbox/platform/windows/interface/kernel32.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ typedef BOOL (WINAPI* tb_kernel32_GetLogicalProcessorInformationEx_t)(
PDWORD ReturnedLength);
#endif

// the CopyFileW func type
typedef BOOL (WINAPI* tb_kernel32_CopyFileExW_t)(
LPCWSTR lpExistingFileName,
LPCWSTR lpNewFileName,
LPPROGRESS_ROUTINE lpProgressRoutine,
LPVOID lpData,
LPBOOL pbCancel,
DWORD dwCopyFlags);

// the kernel32 interfaces type
typedef struct __tb_kernel32_t
{
Expand Down Expand Up @@ -266,6 +275,9 @@ typedef struct __tb_kernel32_t
// DeleteProcThreadAttributeList
tb_kernel32_DeleteProcThreadAttributeList_t DeleteProcThreadAttributeList;

// CopyFileExW
tb_kernel32_CopyFileExW_t CopyFileExW;

// GetLogicalProcessorInformationEx
#if defined(TB_COMPILER_IS_MSVC) && TB_COMPILER_VERSION_BT(16, 0)
tb_kernel32_GetLogicalProcessorInformationEx_t GetLogicalProcessorInformationEx;
Expand Down

0 comments on commit c8b7118

Please sign in to comment.