Bug 958888 - Use OS.File in getAvailableFileName. r=myk

This commit is contained in:
Marco Castelluccio 2014-01-30 11:10:41 -05:00
parent ed85114ff4
commit 3681a9ae3f

View File

@ -340,7 +340,7 @@ WinNativeApp.prototype = {
*/
install: function(aZipPath) {
return Task.spawn(function() {
this._getInstallDir();
yield this._getInstallDir();
try {
yield this._createDirectoryStructure();
@ -400,9 +400,9 @@ WinNativeApp.prototype = {
// Check in both directories to see if a shortcut with the same name
// already exists.
this.shortcutName = getAvailableFileName([ PROGS_DIR, DESKTOP_DIR ],
this.appNameAsFilename,
".lnk");
this.shortcutName = yield getAvailableFileName([ PROGS_DIR, DESKTOP_DIR ],
this.appNameAsFilename,
".lnk");
}
},
@ -670,7 +670,7 @@ MacNativeApp.prototype = {
install: function(aZipPath) {
return Task.spawn(function() {
this._getInstallDir();
yield this._getInstallDir();
try {
yield this._createDirectoryStructure();
@ -712,13 +712,15 @@ MacNativeApp.prototype = {
throw("Updates for apps installed with the old naming scheme unsupported");
}
} else {
let destinationName = getAvailableFileName([ LOCAL_APP_DIR ],
this.appNameAsFilename,
".app");
if (!destinationName) {
throw("No available filename");
let localAppDir = getFile(LOCAL_APP_DIR);
if (!localAppDir.isWritable()) {
throw("Not enough privileges to install apps");
}
let destinationName = yield getAvailableFileName([ LOCAL_APP_DIR ],
this.appNameAsFilename,
".app");
this.installDir = OS.Path.join(LOCAL_APP_DIR, destinationName);
}
},
@ -1128,58 +1130,37 @@ function stripStringForFilename(aPossiblyBadFilenameString) {
* was not available
*/
function getAvailableFileName(aPathSet, aName, aExtension) {
let fileSet = [];
let name = aName + aExtension;
let isUnique = true;
return Task.spawn(function*() {
let name = aName + aExtension;
// Check if the plain name is a unique name in all the directories.
for (let path of aPathSet) {
let folder = getFile(path);
function checkUnique(aName) {
return Task.spawn(function*() {
for (let path of aPathSet) {
if (yield OS.File.exists(OS.Path.join(path, aName))) {
return false;
}
}
folder.followLinks = false;
if (!folder.isDirectory() || !folder.isWritable()) {
return null;
return true;
});
}
let file = folder.clone();
file.append(name);
// Avoid exists() call if we already know this file name is not unique in
// one of the directories.
if (isUnique && file.exists()) {
isUnique = false;
if (yield checkUnique(name)) {
return name;
}
fileSet.push(file);
}
// If we're here, the plain name wasn't enough. Let's try modifying the name
// by adding "(" + num + ")".
for (let i = 2; i < 100; i++) {
name = aName + " (" + i + ")" + aExtension;
if (isUnique) {
return name;
}
function checkUnique(aName) {
for (let file of fileSet) {
file.leafName = aName;
if (file.exists()) {
return false;
if (yield checkUnique(name)) {
return name;
}
}
return true;
}
// If we're here, the plain name wasn't enough. Let's try modifying the name
// by adding "(" + num + ")".
for (let i = 2; i < 100; i++) {
name = aName + " (" + i + ")" + aExtension;
if (checkUnique(name)) {
return name;
}
}
return null;
throw "No available filename";
});
}
/**