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

Support long path in file access on Windows #76739

Merged
merged 1 commit into from
May 5, 2023
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
2 changes: 1 addition & 1 deletion core/io/dir_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DirAccess : public RefCounted {
virtual String _get_root_string() const;

AccessType get_access_type() const;
String fix_path(String p_path) const;
virtual String fix_path(String p_path) const;

template <class T>
static Ref<DirAccess> _create_builtin() {
Expand Down
2 changes: 1 addition & 1 deletion core/io/file_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class FileAccess : public RefCounted {
static void _bind_methods();

AccessType get_access_type() const;
String fix_path(const String &p_path) const;
virtual String fix_path(const String &p_path) const;
virtual Error open_internal(const String &p_path, int p_mode_flags) = 0; ///< open a file
virtual uint64_t _get_modified_time(const String &p_file) = 0;
virtual void _set_access_type(AccessType p_access);
Expand Down
15 changes: 9 additions & 6 deletions drivers/windows/dir_access_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ struct DirAccessWindowsPrivate {
WIN32_FIND_DATAW fu; //unicode version
};

String DirAccessWindows::fix_path(String p_path) const {
String r_path = DirAccess::fix_path(p_path);
if (r_path.is_absolute_path() && !r_path.is_network_share_path() && r_path.length() > MAX_PATH) {
r_path = "\\\\?\\" + r_path.replace("/", "\\");
}
return r_path;
}

// CreateFolderAsync

Error DirAccessWindows::list_dir_begin() {
Expand Down Expand Up @@ -158,19 +166,14 @@ Error DirAccessWindows::make_dir(String p_dir) {
p_dir = fix_path(p_dir);
if (p_dir.is_relative_path()) {
p_dir = current_dir.path_join(p_dir);
p_dir = fix_path(p_dir);
}

p_dir = p_dir.simplify_path().replace("/", "\\");

bool success;
int err;

if (!p_dir.is_network_share_path()) {
p_dir = "\\\\?\\" + p_dir;
// Add "\\?\" to the path to extend max. path length past 248, if it's not a network share UNC path.
// See https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx
}

success = CreateDirectoryW((LPCWSTR)(p_dir.utf16().get_data()), nullptr);
err = GetLastError();

Expand Down
3 changes: 3 additions & 0 deletions drivers/windows/dir_access_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class DirAccessWindows : public DirAccess {
bool _cisdir = false;
bool _cishidden = false;

protected:
virtual String fix_path(String p_path) const override;

public:
virtual Error list_dir_begin() override; ///< This starts dir listing
virtual String get_next() override;
Expand Down
8 changes: 8 additions & 0 deletions drivers/windows/file_access_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ bool FileAccessWindows::is_path_invalid(const String &p_path) {
return invalid_files.has(fname);
}

String FileAccessWindows::fix_path(const String &p_path) const {
String r_path = FileAccess::fix_path(p_path);
if (r_path.is_absolute_path() && !r_path.is_network_share_path() && r_path.length() > MAX_PATH) {
r_path = "\\\\?\\" + r_path.replace("/", "\\");
}
return r_path;
}

Error FileAccessWindows::open_internal(const String &p_path, int p_mode_flags) {
if (is_path_invalid(p_path)) {
#ifdef DEBUG_ENABLED
Expand Down
1 change: 1 addition & 0 deletions drivers/windows/file_access_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class FileAccessWindows : public FileAccess {
static HashSet<String> invalid_files;

public:
virtual String fix_path(const String &p_path) const override;
virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file
virtual bool is_open() const override; ///< true when file is open

Expand Down