Skip to content

Commit

Permalink
Backed out changeset c89276392499 (bug 1649611) for causing build bus…
Browse files Browse the repository at this point in the history
…tages. CLOSED TREE
  • Loading branch information
Butkovits Atila committed Jan 27, 2021
1 parent df8358b commit 09e5216
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 146 deletions.
6 changes: 1 addition & 5 deletions dom/chrome-webidl/IOUtils.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,11 @@ namespace IOUtils {
*
* @param path An absolute file path
* @param permissions The UNIX file mode representing the permissions.
* @param honorUmask If omitted or true, any UNIX file mode value is
* modified by the process umask. If false, the exact value
* of UNIX file mode will be applied. This value has no effect
* on Windows.
*
* @return Resolves if the permissions were set successfully, otherwise
* rejects with a DOMException.
*/
Promise<void> setPermissions(DOMString path, unsigned long permissions, optional boolean honorUmask = true);
Promise<void> setPermissions(DOMString path, unsigned long permissions);
/**
* Return whether or not the file exists at the given path.
*
Expand Down
9 changes: 0 additions & 9 deletions dom/chrome-webidl/PathUtils.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ namespace PathUtils {
[Throws]
DOMString createUniquePath(DOMString path);

/**
* Creates an adjusted path using a path whose length is already close
* to MAX_PATH. For windows only.
*
* @param path An absolute path.
*/
[Throws]
DOMString toExtendedWindowsPath(DOMString path);

/**
* Normalize a path by removing multiple separators and `..` and `.`
* directories.
Expand Down
13 changes: 1 addition & 12 deletions dom/system/IOUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
#include "prtime.h"
#include "prtypes.h"

#ifndef ANDROID
# include "nsSystemInfo.h"
#endif

#define REJECT_IF_SHUTTING_DOWN(aJSPromise) \
do { \
if (sShutdownStarted) { \
Expand Down Expand Up @@ -592,8 +588,7 @@ already_AddRefed<Promise> IOUtils::GetChildren(GlobalObject& aGlobal,
/* static */
already_AddRefed<Promise> IOUtils::SetPermissions(GlobalObject& aGlobal,
const nsAString& aPath,
uint32_t aPermissions,
const bool aHonorUmask) {
const uint32_t aPermissions) {
MOZ_ASSERT(XRE_IsParentProcess());
RefPtr<Promise> promise = CreateJSPromise(aGlobal);
if (!promise) {
Expand All @@ -603,12 +598,6 @@ already_AddRefed<Promise> IOUtils::SetPermissions(GlobalObject& aGlobal,
nsCOMPtr<nsIFile> file = new nsLocalFile();
REJECT_IF_INIT_PATH_FAILED(file, aPath, promise);

#if defined(XP_UNIX) && !defined(ANDROID)
if (aHonorUmask) {
aPermissions &= ~nsSystemInfo::gUserUmask;
}
#endif

RunOnBackgroundThreadAndResolve<Ok>(
promise, [file = std::move(file), permissions = aPermissions]() {
return SetPermissionsSync(file, permissions);
Expand Down
3 changes: 1 addition & 2 deletions dom/system/IOUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ class IOUtils final {

static already_AddRefed<Promise> SetPermissions(GlobalObject& aGlobal,
const nsAString& aPath,
uint32_t aPermissions,
const bool aHonorUmask);
const uint32_t aPermissions);

static already_AddRefed<Promise> Exists(GlobalObject& aGlobal,
const nsAString& aPath);
Expand Down
33 changes: 0 additions & 33 deletions dom/system/PathUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,39 +217,6 @@ void PathUtils::CreateUniquePath(const GlobalObject&, const nsAString& aPath,
MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
}

void PathUtils::ToExtendedWindowsPath(const GlobalObject&,
const nsAString& aPath, nsString& aResult,
ErrorResult& aErr) {
#ifndef XP_WIN
aErr.ThrowNotAllowedError("Operation is windows specific"_ns);
#endif

if (aPath.IsEmpty()) {
aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH);
return;
}

const nsAString& str1 = Substring(aPath, 1, 1);
const nsAString& str2 = Substring(aPath, 2, aPath.Length() - 2);

bool isUNC = aPath.Length() >= 2 &&
(aPath.First() == '\\' || aPath.First() == '/') &&
(str1.EqualsLiteral("\\") || str1.EqualsLiteral("/"));

constexpr auto pathPrefix = u"\\\\?\\"_ns;
const nsAString& uncPath = pathPrefix + u"UNC\\"_ns + str2;
const nsAString& normalPath = pathPrefix + aPath;

nsCOMPtr<nsIFile> path = new nsLocalFile();
if (nsresult rv = path->InitWithPath(isUNC ? uncPath : normalPath);
NS_FAILED(rv)) {
ThrowError(aErr, rv, ERROR_INITIALIZE_PATH);
return;
}

MOZ_ALWAYS_SUCCEEDS(path->GetPath(aResult));
}

void PathUtils::Normalize(const GlobalObject&, const nsAString& aPath,
nsString& aResult, ErrorResult& aErr) {
if (aPath.IsEmpty()) {
Expand Down
3 changes: 0 additions & 3 deletions dom/system/PathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class PathUtils final {
static void CreateUniquePath(const GlobalObject&, const nsAString& aPath,
nsString& aResult, ErrorResult& aErr);

static void ToExtendedWindowsPath(const GlobalObject&, const nsAString& aPath,
nsString& aResult, ErrorResult& aErr);

static void Normalize(const GlobalObject&, const nsAString& aPath,
nsString& aResult, ErrorResult& aErr);

Expand Down
32 changes: 0 additions & 32 deletions dom/system/tests/ioutils/test_ioutils_set_permissions.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,6 @@

let stat = await IOUtils.stat(tempFile);

if (Services.appinfo.OS === "WINNT") {
// setPermissions ignores the x bit on Windows.
is(stat.permissions, 0o666, "Permissions munged on Windows");
} else {
let umask = Services.sysinfo.getProperty("umask");
is(stat.permissions, 0o421 & ~umask, "Permissions match");
}

await IOUtils.setPermissions(tempFile, 0o400);
stat = await IOUtils.stat(tempFile);

if (Services.appinfo.OS === "WINNT") {
is(stat.permissions, 0o444, "Permissions munged on Windows");

// We need to make the file writable to delete it on Windows.
await IOUtils.setPermissions(tempFile, 0o600);
} else {
is(stat.permissions, 0o400, "Permissions match");
}

await cleanup(tempFile);
});

add_task(async function test_setPermissionsWithoutHonoringUmask() {
const tempDir = await PathUtils.getTempDir();
const tempFile = PathUtils.join(tempDir, "setPermissions.tmp");

await IOUtils.writeUTF8(tempFile, "");
await IOUtils.setPermissions(tempFile, 0o421, false);

let stat = await IOUtils.stat(tempFile);

if (Services.appinfo.OS === "WINNT") {
// setPermissions ignores the x bit on Windows.
is(stat.permissions, 0o666, "Permissions munged on Windows");
Expand Down
88 changes: 52 additions & 36 deletions toolkit/components/downloads/DownloadIntegration.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ ChromeUtils.defineModuleGetter(
"NetUtil",
"resource://gre/modules/NetUtil.jsm"
);
ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
ChromeUtils.defineModuleGetter(
this,
"PlacesUtils",
Expand Down Expand Up @@ -263,7 +264,7 @@ var DownloadIntegration = {

this._store = new DownloadStore(
list,
PathUtils.join(await PathUtils.getProfileDir(), "downloads.json")
OS.Path.join(OS.Constants.Path.profileDir, "downloads.json")
);
this._store.onsaveitem = this.shouldPersistDownload.bind(this);

Expand Down Expand Up @@ -370,9 +371,7 @@ var DownloadIntegration = {
Ci.nsIFile
);
directoryPath = directory.path;
await IOUtils.makeDirectory(directoryPath, {
createAncestors: false,
});
await OS.File.makeDir(directoryPath, { ignoreExisting: true });
} catch (ex) {
// Either the preference isn't set or the directory cannot be created.
directoryPath = await this.getSystemDownloadsDirectory();
Expand Down Expand Up @@ -498,7 +497,7 @@ var DownloadIntegration = {
referrerInfo: aDownload.source.referrerInfo,
fileSize: aDownload.currentBytes,
sha256Hash: hash,
suggestedFileName: PathUtils.filename(aDownload.target.path),
suggestedFileName: OS.Path.basename(aDownload.target.path),
signatureInfo: sigInfo,
redirects: channelRedirects,
},
Expand Down Expand Up @@ -605,31 +604,41 @@ var DownloadIntegration = {
// whatever reason.
zone = Ci.mozIDownloadPlatform.ZONE_INTERNET;
}
// Don't write zone IDs for Local, Intranet, or Trusted sites
// to match Windows behavior.
if (zone >= Ci.mozIDownloadPlatform.ZONE_INTERNET) {
let path = aDownload.target.path + ":Zone.Identifier";
try {
let zoneId = "[ZoneTransfer]\r\nZoneId=" + zone + "\r\n";
let { url, isPrivate, referrerInfo } = aDownload.source;
if (!isPrivate) {
let referrer = referrerInfo
? referrerInfo.computedReferrerSpec
: "";
zoneId +=
this._zoneIdKey("ReferrerUrl", referrer) +
this._zoneIdKey("HostUrl", url, "about:internet");
}
await IOUtils.writeUTF8(
await PathUtils.toExtendedWindowsPath(path),
zoneId
try {
// Don't write zone IDs for Local, Intranet, or Trusted sites
// to match Windows behavior.
if (zone >= Ci.mozIDownloadPlatform.ZONE_INTERNET) {
let streamPath = aDownload.target.path + ":Zone.Identifier";
let stream = await OS.File.open(
streamPath,
{ create: true },
{ winAllowLengthBeyondMaxPathWithCaveats: true }
);
} catch (ex) {
// If writing to the file fails, we ignore the error and continue.
if (!(ex instanceof DOMException)) {
Cu.reportError(ex);
try {
let zoneId = "[ZoneTransfer]\r\nZoneId=" + zone + "\r\n";
let { url, isPrivate, referrerInfo } = aDownload.source;
if (!isPrivate) {
let referrer = referrerInfo
? referrerInfo.computedReferrerSpec
: "";
zoneId +=
this._zoneIdKey("ReferrerUrl", referrer) +
this._zoneIdKey("HostUrl", url, "about:internet");
}
await stream.write(new TextEncoder().encode(zoneId));
} finally {
await stream.close();
}
}
} catch (ex) {
// If writing to the stream fails, we ignore the error and continue.
// The Windows API error 123 (ERROR_INVALID_NAME) is expected to
// occur when working on a file system that does not support
// Alternate Data Streams, like FAT32, thus we don't report this
// specific error.
if (!(ex instanceof OS.File.Error) || ex.winLastError != 123) {
Cu.reportError(ex);
}
}
}

Expand All @@ -651,19 +660,26 @@ var DownloadIntegration = {
));
// Permanently downloaded files are made accessible by other users on
// this system, while temporary downloads are marked as read-only.
let unixMode;
let options = {};
if (isTemporaryDownload) {
unixMode = 0o400;
options.unixMode = 0o400;
options.winAttributes = { readOnly: true };
} else {
unixMode = 0o666;
options.unixMode = 0o666;
}
// On Unix, the umask of the process is respected.
await IOUtils.setPermissions(aDownload.target.path, unixMode);
await 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
// prevent the download from completing.
if (!(ex instanceof DOMException)) {
// The setPermissions API error EPERM is expected to occur when working
// on a file system that does not support file permissions, like FAT32,
// thus we don't report this error.
if (
!(ex instanceof OS.File.Error) ||
ex.unixErrno != OS.Constants.libc.EPERM
) {
Cu.reportError(ex);
}
}
Expand Down Expand Up @@ -942,15 +958,15 @@ var DownloadIntegration = {
// We read the name of the directory from the list of translated strings
// that is kept by the UI helper module, even if this string is not strictly
// displayed in the user interface.
let directoryPath = PathUtils.join(
let directoryPath = OS.Path.join(
this._getDirectory(aName),
DownloadUIHelper.strings.downloadsFolder
);

// Create the Downloads folder and ignore if it already exists.
return IOUtils.makeDirectory(directoryPath, {
createAncestors: false,
}).then(() => directoryPath);
return OS.File.makeDir(directoryPath, { ignoreExisting: true }).then(
() => directoryPath
);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,18 @@ add_task(async function test_unix_permissions() {
}

let isTemporary = launchWhenSucceeded && (autoDelete || isPrivate);
let stat = await IOUtils.stat(download.target.path);
let stat = await OS.File.stat(download.target.path);
if (Services.appinfo.OS == "WINNT") {
// On Windows
// Temporary downloads should be read-only
Assert.equal(stat.permissions, isTemporary ? 0o444 : 0o666);
Assert.equal(stat.winAttributes.readOnly, !!isTemporary);
} 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.
Assert.equal(
stat.permissions,
stat.unixMode,
isTemporary ? 0o400 : 0o666 & ~OS.Constants.Sys.umask
);
}
Expand Down
22 changes: 11 additions & 11 deletions toolkit/components/downloads/test/unit/test_DownloadIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ add_task(async function test_getSystemDownloadsDirectory_exists_or_creates() {
) {
downloadDir = await DownloadIntegration.getSystemDownloadsDirectory();
Assert.equal(downloadDir, tempDir.path);
Assert.ok(await IOUtils.exists(downloadDir));
Assert.ok(await OS.File.exists(downloadDir));

let info = await IOUtils.stat(downloadDir);
Assert.equal(info.type, "directory");
let info = await OS.File.stat(downloadDir);
Assert.ok(info.isDir);
} else {
let targetPath = PathUtils.join(
let targetPath = OS.Path.join(
tempDir.path,
gStringBundle.GetStringFromName("downloadsFolder")
);
try {
await IOUtils.remove(targetPath);
await OS.File.removeEmptyDir(targetPath);
} catch (e) {}
downloadDir = await DownloadIntegration.getSystemDownloadsDirectory();
Assert.equal(downloadDir, targetPath);
Assert.ok(await IOUtils.exists(downloadDir));
Assert.ok(await OS.File.exists(downloadDir));

let info = await IOUtils.stat(downloadDir);
Assert.equal(info.type, "directory");
await IOUtils.remove(targetPath);
let info = await OS.File.stat(downloadDir);
Assert.ok(info.isDir);
await OS.File.removeEmptyDir(targetPath);
}
});

Expand Down Expand Up @@ -163,8 +163,8 @@ add_task(async function test_getPreferredDownloadsDirectory() {
downloadDir = await DownloadIntegration.getPreferredDownloadsDirectory();
Assert.notEqual(downloadDir, "");
Assert.equal(downloadDir, tempDir.path);
Assert.ok(await IOUtils.exists(downloadDir));
await IOUtils.remove(tempDir.path);
Assert.ok(await OS.File.exists(downloadDir));
await OS.File.removeEmptyDir(tempDir.path);

// Should return the system downloads directory beacause the path is invalid
// in the dir preference.
Expand Down

0 comments on commit 09e5216

Please sign in to comment.