Bug 1746676 - Remove OS.File.mac{Get,Set,Remove}XAttr r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D133839
This commit is contained in:
Barret Rennie 2021-12-31 20:27:57 +00:00
Родитель 761b1749aa
Коммит 60d01dae74
5 изменённых файлов: 0 добавлений и 298 удалений

Просмотреть файл

@ -1117,73 +1117,6 @@ File.remove = function remove(path, options) {
return Scheduler.post("remove", [Type.path.toMsg(path), options], path);
};
// Extended attribute functions are MacOS only at this point.
if (SharedAll.Constants.Sys.Name == "Darwin") {
/**
* Get an extended attribute (xattr) from a file.
*
* @param {string} path The name of the file.
* @param {string} name The name of the extended attribute.
*
* @returns {promise}
* @resolves {Uint8Array} An array containing the value of the attribute.
* @rejects {OS.File.Error} In case of an I/O error or invalid options.
*/
File.macGetXAttr = function macGetXAttr(path, name) {
let promise = Scheduler.post(
"macGetXAttr",
[Type.path.toMsg(path), Type.cstring.toMsg(name)],
[path, name]
);
return promise.then(data => {
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
});
};
/**
* Remove an extended attribute (xattr) from a file.
*
* @param {string} path The name of the file.
* @param {string} name The name of the extended attribute.
*
* @returns {promise}
* @resolves {null}
* @rejects {OS.File.Error} In case of an I/O error.
*/
File.macRemoveXAttr = function macRemoveXAttr(path, name) {
return Scheduler.post(
"macRemoveXAttr",
[Type.path.toMsg(path), Type.cstring.toMsg(name)],
[path, name]
);
};
/**
* Set an extended attribute (xattr) on a file.
*
* @param {string} path The name of the file.
* @param {string} name The name of the extended attribute.
* @param {Uint8Array} value A byte array containing the value to set the
* xattr to.
*
* @returns {promise}
* @resolves {null}
* @rejects {OS.File.Error} In case of an I/O error.
*/
File.macSetXAttr = function macSetXAttr(path, name, value, number) {
return Scheduler.post(
"macSetXAttr",
[
Type.path.toMsg(path),
Type.cstring.toMsg(name),
Type.void_t.in_ptr.toMsg(value),
value.length,
],
[path, name, value]
);
};
}
/**
* Create a directory and, optionally, its parent directories.
*

Просмотреть файл

@ -302,35 +302,6 @@ if (this.Components) {
removeDir(path, options) {
return File.removeDir(Type.path.fromMsg(path), options);
},
macGetXAttr(path, name) {
let data = File.macGetXAttr(
Type.path.fromMsg(path),
Type.cstring.fromMsg(name)
);
return new Meta(
{
buffer: data.buffer,
byteOffset: data.byteOffset,
byteLength: data.byteLength,
},
{
transfers: [data.buffer],
}
);
},
macRemoveXAttr(path, name) {
return File.macRemoveXAttr(
Type.path.fromMsg(path),
Type.cstring.fromMsg(name)
);
},
macSetXAttr(path, name, value) {
return File.macSetXAttr(
Type.path.fromMsg(path),
Type.cstring.fromMsg(name),
Type.voidptr_t.fromMsg(value)
);
},
new_DirectoryIterator: function new_DirectoryIterator(path, options) {
let directoryPath = Type.path.fromMsg(path);
let iterator = new File.DirectoryIterator(directoryPath, options);

Просмотреть файл

@ -919,99 +919,6 @@
return serialized;
};
// Extended attribute functions are MacOS only at this point.
if (OS.Constants.Sys.Name == "Darwin") {
/**
* Get an extended attribute (xattr) from a file.
*
* @param {string} path The name of the file.
* @param {string} name The name of the extended attribute.
*
* @returns {Uint8Array} An array containing the value of the attribute.
* @throws {OS.File.Error} In case of an I/O error.
*/
File.macGetXAttr = function getxattr(path, name) {
// Get the size of the xattr
let result = UnixFile.getxattr(
path,
name,
null,
0,
// The position arg is not used unless
// interacting with a resouce fork.
0 /* position arg */,
0 /* follow links */
);
if (result == -1) {
throw new File.Error("getxattr", ctypes.errno, path);
}
// Loop until we fetch the whole xattr. This covers the edge case
// where the xattr is set in between calls and attempts to avoid us
// less than the full size of the xattr.
while (true) {
let size = result;
let buffer = new Uint8Array(size);
result = UnixFile.getxattr(
path,
name,
buffer,
size,
// The position arg is not used unless
// interacting with a resouce fork.
0 /* position arg */,
0 /* follow links */
);
if (result == -1) {
throw new File.Error("getxattr", ctypes.errno, path);
}
if (size == result) {
return buffer;
}
}
};
/**
* Remove an extended attribute (xattr) from a file.
*
* @param {string} path The name of the file.
* @param {string} name The name of the extended attribute.
*
* @throws {OS.File.Error} In case of an I/O error.
*/
File.macRemoveXAttr = function removexattr(path, name) {
let result = UnixFile.removexattr(path, name, 0 /* follow links */);
if (result == -1) {
throw new File.Error("removexattr", ctypes.errno, path);
}
};
/**
* Set an extended attribute (xattr) on a file.
*
* @param {string} path The name of the file.
* @param {string} name The name of the extended attribute.
* @param {Uint8Array} value The value of the xattr to be set.
*
* @throws {OS.File.Error} In case of an I/O error.
*/
File.macSetXAttr = function setxattr(path, name, value) {
let size = value.length;
let result = UnixFile.setxattr(
path,
name,
value,
size,
// The position arg is not used unless
// interacting with a resouce fork.
0 /* position arg */,
0 /* follow links */
);
if (result == -1) {
throw new File.Error("setxattr", ctypes.errno, path);
}
};
}
let gStatData = new Type.stat.implementation();
let gStatDataPtr = gStatData.address();

Просмотреть файл

@ -1,106 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/**
* Tests to ensure that extended attribute (xattr) related operations function
* correctly.
*
* These tests are MacOS specific.
*/
/**
* Test that happy path functionality works.
*/
add_task(async function test_mac_xattr() {
// Create a file to manipulate
let path = OS.Path.join(
OS.Constants.Path.tmpDir,
"test_osfile_mac_xattr.tmp"
);
await OS.File.writeAtomic(path, new Uint8Array(1));
try {
let expected_size = 2;
let xattr_value = new Uint8Array(expected_size);
// We need a separate array storing the expected value as xattr_value will
// be transferred and become invalid after we set the xattr.
let expected_value = new Uint8Array(expected_size);
for (let i = 0; i < expected_size; i++) {
let num = Math.floor(Math.random() * 0xff);
xattr_value[i] = num;
expected_value[i] = num;
}
// Set attribute on file.
await OS.File.macSetXAttr(path, "user.foo", xattr_value);
// Make sure attribute is set and getting it works.
let xattr_readback = await OS.File.macGetXAttr(path, "user.foo");
Assert.equal(xattr_readback.length, expected_size);
for (let i = 0; i < expected_size; i++) {
Assert.equal(xattr_readback[i], expected_value[i]);
}
// Remove the attribute and verify it is no longer set.
await OS.File.macRemoveXAttr(path, "user.foo");
let got_error = false;
try {
// Should throw since the attribute is no longer set.
await OS.File.macGetXAttr(path, "user.foo");
} catch (e) {
Assert.ok(e instanceof OS.File.Error);
Assert.ok(e.toString().includes("getxattr"));
got_error = true;
}
Assert.ok(got_error);
} finally {
await removeTestFile(path);
}
});
add_task(async function test_mac_xattr_missing_file() {
let path = OS.Path.join(
OS.Constants.Path.tmpDir,
"test_osfile_mac_xattr_missing_file.tmp"
);
// Intentionally don't create a file, we want to make sure ops fail.
let exists = await OS.File.exists(path);
Assert.equal(false, exists);
// Setting on non-existent file fails.
let got_first_error = false;
try {
let xattr_value = new Uint8Array(1);
await OS.File.macSetXAttr(path, "user.foo", xattr_value);
} catch (e) {
Assert.ok(e instanceof OS.File.Error);
Assert.ok(e.toString().includes("setxattr"));
got_first_error = true;
}
Assert.ok(got_first_error);
// Getting xattr from non-existent file fails.
let got_second_error = false;
try {
await OS.File.macGetXAttr(path, "user.foo");
} catch (e) {
Assert.ok(e instanceof OS.File.Error);
Assert.ok(e.toString().includes("getxattr"));
got_second_error = true;
}
Assert.ok(got_second_error);
// Removing xattr from non-existent file fails.
let got_third_error = false;
try {
await OS.File.macRemoveXAttr(path, "user.foo");
} catch (e) {
Assert.ok(e instanceof OS.File.Error);
Assert.ok(e.toString().includes("removexattr"));
got_third_error = true;
}
Assert.ok(got_third_error);
});

Просмотреть файл

@ -24,9 +24,6 @@ skip-if = os == "win" || os == "android"
[test_osfile_closed.js]
[test_osfile_error.js]
[test_osfile_kill.js]
# MacOS test
[test_osfile_mac_async_xattr.js]
skip-if = os != "mac"
# Windows test
[test_osfile_win_async_setPermissions.js]
skip-if = os != "win"