Shorten filenames >=254 characters on storage sync error, since ext3/ext4/HFS+ all have ~254-character filename length limits

I don't have an OS/filesystem on which I can actually test this, but it might work.
This commit is contained in:
Dan Stillman 2009-11-01 19:45:41 +00:00
parent 8fad310894
commit a4f8e45b73

View file

@ -788,12 +788,24 @@ Zotero.Sync.Storage = new function () {
var destFile = parentDir.clone();
destFile.append(newName);
var windowsLength = false;
var nameLength = false;
// Windows API only allows paths of 260 characters
if (e.name == "NS_ERROR_FILE_NOT_FOUND" && destFile.path.length > 255) {
windowsLength = true;
}
// ext3/ext4/HFS+ have a filename length limit of ~254 characters
else if (e.name == "NS_ERROR_FAILURE" && destFile.leafName.length >= 254) {
nameLength = true;
}
if (windowsLength || nameLength) {
// Preserve extension
var matches = destFile.leafName.match(/\.[a-z0-9]{0,8}$/);
var ext = matches ? matches[0] : "";
if (windowsLength) {
var pathLength = destFile.path.length - destFile.leafName.length;
var newLength = 255 - pathLength;
// Require 40 available characters in path -- this is arbitrary,
@ -804,6 +816,10 @@ Zotero.Sync.Storage = new function () {
+ "Please relocate your Zotero data to a higher directory.";
throw (msg);
}
}
else {
var newLength = 254;
}
// Shorten file if it's too long -- we don't relink it, but this should
// be pretty rare and probably only occurs on extraneous files with
@ -910,8 +926,19 @@ Zotero.Sync.Storage = new function () {
destFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644);
}
catch (e) {
var windowsLength = false;
var nameLength = false;
// Windows API only allows paths of 260 characters
if (e.name == "NS_ERROR_FILE_NOT_FOUND" && destFile.path.length > 255) {
windowsLength = true;
}
// ext3/ext4/HFS+ have a filename length limit of ~254 characters
else if (e.name == "NS_ERROR_FAILURE" && destFile.leafName.length >= 254) {
nameLength = true;
}
if (windowsLength || nameLength) {
// Is this the main attachment file?
var primaryFile = item.getFile(null, true).leafName == destFile.leafName;
@ -919,6 +946,7 @@ Zotero.Sync.Storage = new function () {
var matches = destFile.leafName.match(/\.[a-z0-9]{0,8}$/);
var ext = matches ? matches[0] : "";
if (windowsLength) {
var pathLength = destFile.path.length - destFile.leafName.length;
var newLength = 255 - pathLength;
// Require 40 available characters in path -- this is arbitrary,
@ -930,6 +958,10 @@ Zotero.Sync.Storage = new function () {
+ "Please relocate your Zotero data to a higher directory.";
throw (msg);
}
}
else {
var newLength = 254;
}
// Shorten file if it's too long -- we don't relink it, but this should
// be pretty rare and probably only occurs on extraneous files with
@ -941,10 +973,10 @@ Zotero.Sync.Storage = new function () {
var step = 0;
do {
if (step == 0) {
var newName = destFile.leafName.substr(0, newLength - (ext.length + 1)) + ext;
var newName = destFile.leafName.substr(0, newLength - ext.length) + ext;
}
else {
var newName = destFile.leafName.substr(0, newLength - (ext.length + 1)) + "-" + step + ext;
var newName = destFile.leafName.substr(0, newLength - ext.length) + "-" + step + ext;
}
destFile.leafName = newName;
step++;