Skip to content

Commit

Permalink
Allows permissions on temp files, fixes #379
Browse files Browse the repository at this point in the history
  • Loading branch information
botic committed Oct 27, 2017
1 parent 40b986d commit c920133
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
27 changes: 19 additions & 8 deletions modules/ringo/utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,30 @@ function resolveId(parent, child) {
* Create a new empty temporary file in the default directory for temporary files.
* @param {String} prefix the prefix of the temporary file; must be at least three characters long
* @param {String} suffix the suffix of the temporary file; may be undefined or null
* @param {String} directory optional directory in which to create the file. Pass undefined
* to use the system's default location for temporary files
* @param {String} directory optional directory in which to create the file. Omit to use the system's temp file directory.
* @param {Number|String|java.util.Set<PosixFilePermission>} permissions optional POSIX permissions to apply
*
* @returns {String} the temporary file's path
*/
function createTempFile(prefix, suffix, directory) {
function createTempFile(prefix, suffix, directory, permissions) {
suffix = suffix || null;
directory = directory ? getPath(directory) : null;

return (
directory !== null ?
Files.createTempFile(directory, prefix, suffix) :
Files.createTempFile(prefix, suffix)
).toString();
const posixPermissions = (permissions !== undefined ? new PosixPermissions(permissions) : null);

if (posixPermissions !== null) {
return (
directory !== null ?
Files.createTempFile(directory, prefix, suffix, posixPermissions.toJavaFileAttribute()) :
Files.createTempFile(prefix, suffix, posixPermissions.toJavaFileAttribute())
).toString();
} else {
return (
directory !== null ?
Files.createTempFile(directory, prefix, suffix) :
Files.createTempFile(prefix, suffix)
).toString();
}
}

/**
Expand Down
45 changes: 45 additions & 0 deletions test/ringo/utils/files_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,51 @@ exports.testPosixPermissions = function() {
java.nio.file.Files.delete(tempPath);
};

exports.testCreateTempFileWithPermissions = function () {
let tempFile = files.createTempFile("ringo", null, null, 0000);

This comment has been minimized.

Copy link
@botic

botic Oct 27, 2017

Author Member

@grob it will look like this. imho the way to go, since it follows the normal pattern to provide permissions at last.

assert.isNotNull(tempFile); // Creation w/ prefix only.
assert.isTrue(/[\/\\]ringo\w*\.tmp$/.test(tempFile));
assert.isTrue(typeof tempFile === "string", "Result must be string!");

let nativePermissions = java.nio.file.Files.getPosixFilePermissions(
java.nio.file.FileSystems.getDefault().getPath(tempFile)
);
assert.strictEqual(
"[PosixPermissions " + java.nio.file.attribute.PosixFilePermissions.toString(nativePermissions) + "]",
(new files.PosixPermissions(0000)).toString()
);
fs.remove(tempFile);

tempFile = files.createTempFile("ringo", ".js", null, 0000);
assert.isNotNull(tempFile); // Creation w/ prefix only.
assert.isTrue(/[\/\\]ringo\w*\.js$/.test(tempFile));
assert.isTrue(typeof tempFile === "string", "Result must be string!");

nativePermissions = java.nio.file.Files.getPosixFilePermissions(
java.nio.file.FileSystems.getDefault().getPath(tempFile)
);
assert.strictEqual(
"[PosixPermissions " + java.nio.file.attribute.PosixFilePermissions.toString(nativePermissions) + "]",
(new files.PosixPermissions(0000)).toString()
);
fs.remove(tempFile);

tempFile = files.createTempFile("ringo", ".js", java.lang.System.getProperty("java.io.tmpdir"), 0111);
assert.isNotNull(tempFile); // Creation w/ prefix only.
assert.isTrue(/[[\/\\]ringo\w*\.js$/.test(tempFile));
assert.isTrue(typeof tempFile === "string", "Result must be string!");
assert.isTrue(tempFile.indexOf(java.lang.System.getProperty("java.io.tmpdir")) === 0);

nativePermissions = java.nio.file.Files.getPosixFilePermissions(
java.nio.file.FileSystems.getDefault().getPath(tempFile)
);
assert.strictEqual(
"[PosixPermissions " + java.nio.file.attribute.PosixFilePermissions.toString(nativePermissions) + "]",
(new files.PosixPermissions(0111)).toString()
);
fs.remove(tempFile);
};

if (require.main === module) {
require('system').exit(require("test").run(module.id));
}

0 comments on commit c920133

Please sign in to comment.