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

Unified Diff: chrome/browser/download/download_path_reservation_tracker.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: Review fix (#14), rebase. 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
« no previous file with comments | « base/file_util_win.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/download/download_path_reservation_tracker.cc
diff --git a/chrome/browser/download/download_path_reservation_tracker.cc b/chrome/browser/download/download_path_reservation_tracker.cc
index bb0deb2dc431aef5b62a16557344b87b58456cee..44a58793e47e1def30a5cea221ab3f7ffff66569 100644
--- a/chrome/browser/download/download_path_reservation_tracker.cc
+++ b/chrome/browser/download/download_path_reservation_tracker.cc
@@ -12,6 +12,8 @@
#include "base/logging.h"
#include "base/path_service.h"
#include "base/stl_util.h"
+#include "base/string_util.h"
+#include "base/third_party/icu/icu_utf.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_thread.h"
@@ -26,6 +28,16 @@ namespace {
typedef std::map<content::DownloadId, base::FilePath> ReservationMap;
+// The lower bound for file name truncation. If the truncation results in a name
+// shorter than this limit, we give up automatic truncation and prompt the user.
+static const size_t kTruncatedNameLengthLowerbound = 5;
+
+// The length of the suffix string we append for an intermediate file name.
+// In the file name truncation, we keep the margin to append the suffix.
+// TODO(kinaba): remove the margin. The user should be able to set maximum
+// possible filename.
+static const size_t kIntermediateNameSuffixLength = sizeof(".crdownload") - 1;
+
// Map of download path reservations. Each reserved path is associated with a
// DownloadId. This object is destroyed in |Revoke()| when there are no more
// reservations.
@@ -90,10 +102,51 @@ bool IsPathInUse(const base::FilePath& path) {
return false;
}
+// Truncates path->BaseName() to satisfy the condition .value().size() <= limit.
asanka 2013/02/19 17:59:15 Nit: path->BaseName().value().size() <= limit. |l
kinaba 2013/02/20 13:57:41 Done.
+// - It keeps the extension as is. Only truncates the body part.
asanka 2013/02/19 17:59:15 Nit: "base filename" would, I think, be clearer th
kinaba 2013/02/20 13:57:41 Done.
+// - It secures the body part length to be >= kTruncatedNameLengthLowerbound.
+// If it was unable to shorten the name, returns false.
+bool TruncateFileName(base::FilePath* path, size_t limit) {
+ base::FilePath basename(path->BaseName());
+ // It is already short enough.
+ if (basename.value().size() <= limit)
+ return true;
+
+ base::FilePath dir(path->DirName());
+ base::FilePath::StringType ext(basename.Extension());
+ base::FilePath::StringType name(basename.RemoveExtension().value());
+
+ // Impossible to satisfy the limit.
+ if (limit < kTruncatedNameLengthLowerbound + ext.size())
+ return false;
+ limit -= ext.size();
+
+ // Encoding specific truncation logic.
+ base::FilePath::StringType truncated;
+#if defined(OS_CHROMEOS) || defined(OS_MACOSX)
+ // UTF-8.
+ TruncateUTF8ToByteSize(name, limit, &truncated);
+#elif defined(OS_WIN)
+ // UTF-16.
+ DCHECK(name.size() > limit);
+ truncated = name.substr(
+ 0, limit > 0 && CBU16_IS_TRAIL(name[limit]) ? limit - 1 : limit);
asanka 2013/02/19 17:59:15 Nit: limit > 0 is always true at this point.
kinaba 2013/02/20 13:57:41 Done.
+#else
+ // We cannot generally assume that the file name encoding is in UTF-8 (see
+ // the comment for FilePath::AsUTF8Unsafe), hence no safe way to truncate.
+#endif
+
+ if (truncated.size() < kTruncatedNameLengthLowerbound)
+ return false;
+ *path = dir.Append(truncated + ext);
+ return true;
+}
+
// Called on the FILE thread to reserve a download path. This method:
// - Creates directory |default_download_path| if it doesn't exist.
// - Verifies that the parent directory of |suggested_path| exists and is
// writeable.
+// - Truncates the suggested name if it exceeds the filesystem's limit.
// - Uniquifies |suggested_path| if |should_uniquify_path| is true.
// - Schedules |callback| on the UI thread with the reserved path and a flag
// indicating whether the returned path has been successfully verified.
@@ -118,6 +171,7 @@ void CreateReservation(
base::FilePath target_path(suggested_path.NormalizePathSeparators());
bool is_path_writeable = true;
bool has_conflicts = false;
+ bool name_too_long = false;
// Create the default download path if it doesn't already exist and is where
// we are going to create the downloaded file. |target_path| might point
@@ -139,24 +193,43 @@ void CreateReservation(
target_path = dir.Append(filename);
}
- if (is_path_writeable && should_uniquify && IsPathInUse(target_path)) {
- has_conflicts = true;
- for (int uniquifier = 1;
- uniquifier <= DownloadPathReservationTracker::kMaxUniqueFiles;
- ++uniquifier) {
- base::FilePath path_to_check(target_path.InsertBeforeExtensionASCII(
- StringPrintf(" (%d)", uniquifier)));
- if (!IsPathInUse(path_to_check)) {
- target_path = path_to_check;
- has_conflicts = false;
- break;
+ if (is_path_writeable) {
+ // Check the limit of file name length if it could be obtained. When the
+ // suggested name exceeds the limit, truncate or prompt the user.
+ int max_length = file_util::GetMaximumPathComponentLength(dir);
+ if (max_length != -1) {
+ int limit = max_length - kIntermediateNameSuffixLength;
+ if (limit <= 0 || !TruncateFileName(&target_path, limit))
+ name_too_long = true;
+ }
+
+ // Uniquify the name, if it already exists.
+ if (!name_too_long && should_uniquify && IsPathInUse(target_path)) {
+ has_conflicts = true;
+ for (int uniquifier = 1;
+ uniquifier <= DownloadPathReservationTracker::kMaxUniqueFiles;
+ ++uniquifier) {
+ // Append uniquifier. If necessary, truncate the name.
+ std::string suffix(StringPrintf(" (%d)", uniquifier));
asanka 2013/02/19 17:59:15 Nit: Use base::StringPrintf() and include "base/st
kinaba 2013/02/20 13:57:41 Done.
+ base::FilePath path_to_check(target_path);
+ int limit = max_length - kIntermediateNameSuffixLength - suffix.size();
+ if (limit <= 0 || !TruncateFileName(&path_to_check, limit))
+ break;
asanka 2013/02/19 17:59:15 Nit: We could get here if |max_length| is -1 and t
kinaba 2013/02/20 13:57:41 Good catch. Thanks. Explicitly dispatched the |max
+ path_to_check = path_to_check.InsertBeforeExtensionASCII(suffix);
+
+ if (!IsPathInUse(path_to_check)) {
+ target_path = path_to_check;
+ has_conflicts = false;
+ break;
+ }
}
}
}
+
reservations[download_id] = target_path;
- BrowserThread::PostTask(
- BrowserThread::UI, FROM_HERE,
- base::Bind(callback, target_path, (is_path_writeable && !has_conflicts)));
+ bool verified = (is_path_writeable && !has_conflicts && !name_too_long);
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(callback, target_path, verified));
}
// Called on the FILE thread to update the path of the reservation associated
« no previous file with comments | « base/file_util_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698