Bug 1009465 - Set the read-only attribute for temporary downloads on Windows. r=paolo

This commit is contained in:
Ganesh Sahukari 2015-04-01 23:43:28 +05:30
parent 2baf6f0589
commit fb6c1ab12f
3 changed files with 25 additions and 30 deletions

View File

@ -645,10 +645,15 @@ this.DownloadIntegration = {
Services.prefs.getBoolPref("browser.helperApps.deleteTempFileOnExit"));
// Permanently downloaded files are made accessible by other users on
// this system, while temporary downloads are marked as read-only.
let unixMode = isTemporaryDownload ? 0o400 : 0o666;
// On Unix, the umask of the process is respected. This call has no
// effect on Windows.
yield OS.File.setPermissions(aDownload.target.path, { unixMode });
let options = {};
if (isTemporaryDownload) {
options.unixMode = 0o400;
options.winAttributes = {readOnly: true};
} else {
options.unixMode = 0o666;
}
// On Unix, the umask of the process is respected.
yield OS.File.setPermissions(aDownload.target.path, options);
} catch (ex) {
// We should report errors with making the permissions less restrictive
// or marking the file as read-only on Unix and Mac, but this should not

View File

@ -144,25 +144,6 @@ nsresult DownloadPlatform::DownloadDone(nsIURI* aSource, nsIFile* aTarget,
}
}
#ifdef XP_WIN
// Adjust file attributes so that by default, new files are indexed by
// desktop search services. Skip off those that land in the temp folder.
nsCOMPtr<nsIFile> tempDir, fileDir;
nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tempDir));
NS_ENSURE_SUCCESS(rv, rv);
aTarget->GetParent(getter_AddRefs(fileDir));
bool isTemp = false;
if (fileDir) {
fileDir->Equals(tempDir, &isTemp);
}
nsCOMPtr<nsILocalFileWin> localFileWin(do_QueryInterface(aTarget));
if (!isTemp && localFileWin) {
localFileWin->SetFileAttributesWin(nsILocalFileWin::WFA_SEARCH_INDEXED);
}
#endif
#endif
return NS_OK;

View File

@ -193,8 +193,9 @@ add_task(function test_basic_tryToKeepPartialData()
*/
add_task(function test_unix_permissions()
{
// This test is only executed on Linux and Mac.
if (Services.appinfo.OS != "Darwin" && Services.appinfo.OS != "Linux") {
// This test is only executed on some Desktop systems.
if (Services.appinfo.OS != "Darwin" && Services.appinfo.OS != "Linux" &&
Services.appinfo.OS != "WINNT") {
do_print("Skipping test.");
return;
}
@ -228,12 +229,20 @@ add_task(function test_unix_permissions()
yield promiseDownloadStopped(download);
}
// Temporary downloads should be read-only and not accessible to other
// users, while permanently downloaded files should be readable and
// writable as specified by the system umask.
let isTemporary = launchWhenSucceeded && (autoDelete || isPrivate);
do_check_eq((yield OS.File.stat(download.target.path)).unixMode,
isTemporary ? 0o400 : (0o666 & ~OS.Constants.Sys.umask));
let stat = yield OS.File.stat(download.target.path);
if (Services.appinfo.OS == "WINNT") {
// On Windows
// Temporary downloads should be read-only
do_check_eq(stat.winAttributes.readOnly, isTemporary ? true : false);
} else {
// On Linux, Mac
// Temporary downloads should be read-only and not accessible to other
// users, while permanently downloaded files should be readable and
// writable as specified by the system umask.
do_check_eq(stat.unixMode,
isTemporary ? 0o400 : (0o666 & ~OS.Constants.Sys.umask));
}
}
}
}