Skip to content

Implemented deno.chmodSync for mac/linux os users. #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions js/chmod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as fbs from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as dispatch from "./dispatch";
/**
* Synchronously changes the file permissions.
*
* import { chmodSync } from "deno";
* chmodSync(path, mode);
*/
export function chmodSync(path: string, mode: number): void {
dispatch.sendSync(...req(path, mode));
}
function req(
path: string,
mode: number
): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const path_ = builder.createString(path);
fbs.Chmod.startChmod(builder);
fbs.Chmod.addPath(builder, path_);
fbs.Chmod.addMode(builder, mode);
const msg = fbs.Chmod.endChmod(builder);
return [builder, fbs.Any.Chmod, msg];
}
25 changes: 25 additions & 0 deletions js/chmod_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";
testPerm({ write: true }, function chmodSyncSuccess() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const filename = deno.makeTempDirSync() + "/test.txt";
deno.writeFileSync(filename, data, 0o666);
let fileInfo = deno.statSync(filename);
console.log(fileInfo.mode, 0o666); // This is printing 33206 438
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ry deno.statSync(filename).mode is not working properly.

Checkout the line 802 in the build log Here.
802: 33206 438. This is coming from the line console.log(fileInfo.mode, 0o666); on which I am commenting.
I had initially opened an issue #756. At that time it worked on travis. Now it is not working on travis also. Can you check if you are also having the same behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srijanreddy98 You might want to apply a mask (0o777). For example in python

oct(33206) == '0100666'
33206 & 0o777 == 0o666

deno.chmodSync(filename, 0o777);
fileInfo = deno.statSync(filename);
assertEqual(fileInfo.mode, 0o777);
});
testPerm({ write: false }, function chmodSyncPerm() {
let err;
try {
const filename = deno.makeTempDirSync() + "/test.txt";
deno.chmodSync(filename, 0o777);
} catch (e) {
err = e;
}
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});
8 changes: 4 additions & 4 deletions js/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,10 @@ export class DenoCompiler
*/
compile(moduleMetaData: ModuleMetaData): OutputCode {
const recompile = !!this.recompile;
this._log(
"compiler.compile",
{ filename: moduleMetaData.fileName, recompile }
);
this._log("compiler.compile", {
filename: moduleMetaData.fileName,
recompile
});
if (!recompile && moduleMetaData.outputCode) {
return moduleMetaData.outputCode;
}
Expand Down
16 changes: 12 additions & 4 deletions js/compiler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,24 @@ test(function compilerGetScriptFileNames() {
test(function compilerRecompileFlag() {
setup();
compilerInstance.run("foo/bar.ts", "/root/project");
assertEqual(getEmitOutputStack.length, 1, "Expected only a single emitted file.");
assertEqual(
getEmitOutputStack.length,
1,
"Expected only a single emitted file."
);
// running compiler against same file should use cached code
compilerInstance.run("foo/bar.ts", "/root/project");
assertEqual(getEmitOutputStack.length, 1, "Expected only a single emitted file.");
assertEqual(
getEmitOutputStack.length,
1,
"Expected only a single emitted file."
);
compilerInstance.recompile = true;
compilerInstance.run("foo/bar.ts", "/root/project");
assertEqual(getEmitOutputStack.length, 2, "Expected two emitted file.");
assert(
getEmitOutputStack[0] === getEmitOutputStack[1],
"Expected same file to be emitted twice."
getEmitOutputStack[0] === getEmitOutputStack[1],
"Expected same file to be emitted twice."
);
teardown();
});
Expand Down
1 change: 1 addition & 0 deletions js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export { ErrorKind, DenoError } from "./errors";
export { libdeno } from "./libdeno";
export { arch, platform } from "./platform";
export { trace } from "./trace";
export { chmodSync } from "./chmod";
export const args: string[] = [];
4 changes: 1 addition & 3 deletions js/read_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ export async function readlink(name: string): Promise<string> {
return res(await dispatch.sendAsync(...req(name)));
}

function req(
name: string
): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
function req(name: string): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const name_ = builder.createString(name);
fbs.Readlink.startReadlink(builder);
Expand Down
1 change: 1 addition & 0 deletions js/unit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This test is executed as part of tools/test.py
// But it can also be run manually: ./out/debug/deno js/unit_tests.ts
import "./compiler_test.ts";
import "./chmod_test.ts";
import "./console_test.ts";
import "./fetch_test.ts";
import "./os_test.ts";
Expand Down
32 changes: 29 additions & 3 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use msg;
use remove_dir_all::remove_dir_all;
use std;
use std::fs;
extern crate libc;
use std::ffi::CString;
#[cfg(any(unix))]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
Expand Down Expand Up @@ -47,6 +49,7 @@ pub extern "C" fn msg_from_js(i: *const isolate, buf: deno_buf) {
msg::Any::Start => handle_start,
msg::Any::CodeFetch => handle_code_fetch,
msg::Any::CodeCache => handle_code_cache,
msg::Any::Chmod => handle_chmod,
msg::Any::Environ => handle_env,
msg::Any::FetchReq => handle_fetch_req,
msg::Any::TimerStart => handle_timer_start,
Expand Down Expand Up @@ -147,7 +150,7 @@ fn permission_denied() -> DenoError {
fn not_implemented() -> DenoError {
DenoError::from(std::io::Error::new(
std::io::ErrorKind::Other,
"Not implemented"
"Not implemented",
))
}

Expand Down Expand Up @@ -302,7 +305,8 @@ fn handle_env(i: *const isolate, base: &msg::Base) -> Box<Op> {
..Default::default()
},
)
}).collect();
})
.collect();
let tables = builder.create_vector(&vars);
let msg = msg::EnvironRes::create(
builder,
Expand Down Expand Up @@ -415,7 +419,8 @@ where
.and_then(|_| {
cb();
Ok(())
}).select(cancel_rx)
})
.select(cancel_rx)
.map(|_| ())
.map_err(|_| ());

Expand Down Expand Up @@ -704,6 +709,27 @@ fn handle_symlink(i: *const isolate, base: &msg::Base) -> Box<Op> {
}
}

fn handle_chmod(i: *const isolate, base: &msg::Base) -> Box<Op> {
let deno = from_c(i);
if !deno.flags.allow_write {
return odd_future(permission_denied());
};
let msg = base.msg_as_chmod().unwrap();
let path = msg.path().unwrap();
let mode = msg.mode();
debug!("handle_chmod {} {}", path, mode);
if cfg!(target_family = "unix") {
Box::new(futures::future::result(|| -> OpResult {
let path_as_cstring = CString::new(path).unwrap();
unsafe {
libc::chmod(path_as_cstring.as_ptr(), mode);
}
Ok(None)
}()))
} else {
Box::new(futures::future::result(|| -> OpResult { Ok(None) }()))
}
}
fn handle_read_link(_i: *const isolate, base: &msg::Base) -> Box<Op> {
let msg = base.msg_as_readlink().unwrap();
let cmd_id = base.cmd_id();
Expand Down
7 changes: 7 additions & 0 deletions src/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ union Any {
CodeFetch,
CodeFetchRes,
CodeCache,
Chmod,
Exit,
TimerStart,
TimerReady,
Expand Down Expand Up @@ -222,6 +223,12 @@ table Stat {
lstat: bool;
}

table Chmod {
path: string;
mode: uint32;
}


table StatRes {
is_file: bool;
is_symlink: bool;
Expand Down