Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1885)

Unified Diff: base/file_util_win.cc

Issue 12212010: Truncate the download file name if it exceeds the filesystem limit. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add test. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/file_util_win.cc
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index 199a7cfcce11c496e10a34b05033ff66e7e32cd2..a5f7e4cc161c5bb61bd287c5a1ae457aed86d615 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -10,6 +10,7 @@
#include <shlobj.h>
#include <time.h>
+#include <algorithm>
#include <limits>
#include <string>
@@ -950,4 +951,28 @@ bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) {
return success;
}
+int GetMaximumPathComponentLength(const FilePath& path) {
+ base::ThreadRestrictions::AssertIOAllowed();
+
+ wchar_t volume_path[MAX_PATH];
+ if (!GetVolumePathNameW(path.NormalizePathSeparators().value().c_str(),
+ volume_path,
+ arraysize(volume_path))) {
+ return -1;
+ }
+
+ DWORD max_length = 0;
+ if (!GetVolumeInformationW(volume_path, NULL, 0, NULL, &max_length, NULL,
+ NULL, 0)) {
+ return -1;
+ }
+
+ // Length of |path| with path separator appended.
+ size_t prefix = path.StripTrailingSeparators().value().size() + 1;
+ // The whole path string must be shorter than MAX_PATH. That is, it must be
+ // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1).
+ int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix));
+ return std::min(whole_path_limit, static_cast<int>(max_length));
+}
+
} // namespace file_util

Powered by Google App Engine
This is Rietveld 408576698