mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 05:04:48 +00:00
Implement renameSync
This commit is contained in:
parent
b2b4299e3b
commit
641e3d404d
5 changed files with 94 additions and 0 deletions
|
@ -8,6 +8,7 @@ export {
|
||||||
makeTempDirSync,
|
makeTempDirSync,
|
||||||
mkdirSync,
|
mkdirSync,
|
||||||
readFileSync,
|
readFileSync,
|
||||||
|
renameSync,
|
||||||
statSync,
|
statSync,
|
||||||
lStatSync,
|
lStatSync,
|
||||||
writeFileSync
|
writeFileSync
|
||||||
|
|
26
js/os.ts
26
js/os.ts
|
@ -336,3 +336,29 @@ export function writeFileSync(
|
||||||
const msg = fbs.WriteFileSync.endWriteFileSync(builder);
|
const msg = fbs.WriteFileSync.endWriteFileSync(builder);
|
||||||
send(builder, fbs.Any.WriteFileSync, msg);
|
send(builder, fbs.Any.WriteFileSync, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renames (moves) oldpath to newpath.
|
||||||
|
* import { renameSync } from "deno";
|
||||||
|
* const oldpath = 'from/path';
|
||||||
|
* const newpath = 'to/path';
|
||||||
|
*
|
||||||
|
* renameSync(oldpath, newpath);
|
||||||
|
*/
|
||||||
|
export function renameSync(oldpath: string, newpath: string): void {
|
||||||
|
/* Ideally we could write:
|
||||||
|
const res = send({
|
||||||
|
command: fbs.Command.RENAME_SYNC,
|
||||||
|
renameOldPath: oldpath,
|
||||||
|
renameNewPath: newpath
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
const builder = new flatbuffers.Builder();
|
||||||
|
const _oldpath = builder.createString(oldpath);
|
||||||
|
const _newpath = builder.createString(newpath);
|
||||||
|
fbs.RenameSync.startRenameSync(builder);
|
||||||
|
fbs.RenameSync.addOldpath(builder, _oldpath);
|
||||||
|
fbs.RenameSync.addNewpath(builder, _newpath);
|
||||||
|
const msg = fbs.RenameSync.endRenameSync(builder);
|
||||||
|
send(builder, fbs.Any.RenameSync, msg);
|
||||||
|
}
|
||||||
|
|
|
@ -204,3 +204,41 @@ testPerm({ write: false }, function mkdDirSyncPerm() {
|
||||||
assertEqual(err.name, "deno.PermissionDenied");
|
assertEqual(err.name, "deno.PermissionDenied");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testPerm({ write: true }, function renameSync() {
|
||||||
|
const testDir = deno.makeTempDirSync() + "/test-rename";
|
||||||
|
const oldpath = testDir + "/oldpath"
|
||||||
|
const newpath = testDir + "/newpath"
|
||||||
|
deno.mkdirSync(oldpath);
|
||||||
|
deno.renameSync(oldpath, newpath);
|
||||||
|
const newPathInfo = deno.statSync(newpath);
|
||||||
|
assert(newPathInfo.isDirectory());
|
||||||
|
|
||||||
|
let caughtErr = false;
|
||||||
|
let oldPathInfo;
|
||||||
|
|
||||||
|
try {
|
||||||
|
oldPathInfo = deno.statSync(oldpath);
|
||||||
|
} catch (err) {
|
||||||
|
caughtErr = true;
|
||||||
|
// TODO assert(err instanceof deno.NotFound).
|
||||||
|
assert(err);
|
||||||
|
assertEqual(err.name, "deno.NotFound");
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(caughtErr);
|
||||||
|
assertEqual(oldPathInfo, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(function renameSyncPerm() {
|
||||||
|
let err;
|
||||||
|
try {
|
||||||
|
const oldpath = "/oldbaddir";
|
||||||
|
const newpath = "/newbaddir";
|
||||||
|
deno.renameSync(oldpath, newpath);
|
||||||
|
} catch (err_) {
|
||||||
|
err = err_;
|
||||||
|
}
|
||||||
|
// TODO assert(err instanceof deno.PermissionDenied).
|
||||||
|
assert(err);
|
||||||
|
assertEqual(err.name, "deno.PermissionDenied");
|
||||||
|
});
|
||||||
|
|
|
@ -40,6 +40,7 @@ pub extern "C" fn msg_from_js(d: *const DenoC, buf: deno_buf) {
|
||||||
msg::Any::MakeTempDir => handle_make_temp_dir,
|
msg::Any::MakeTempDir => handle_make_temp_dir,
|
||||||
msg::Any::MkdirSync => handle_mkdir_sync,
|
msg::Any::MkdirSync => handle_mkdir_sync,
|
||||||
msg::Any::ReadFileSync => handle_read_file_sync,
|
msg::Any::ReadFileSync => handle_read_file_sync,
|
||||||
|
msg::Any::RenameSync => handle_rename_sync,
|
||||||
msg::Any::SetEnv => handle_set_env,
|
msg::Any::SetEnv => handle_set_env,
|
||||||
msg::Any::StatSync => handle_stat_sync,
|
msg::Any::StatSync => handle_stat_sync,
|
||||||
msg::Any::WriteFileSync => handle_write_file_sync,
|
msg::Any::WriteFileSync => handle_write_file_sync,
|
||||||
|
@ -673,3 +674,25 @@ fn handle_timer_clear(
|
||||||
remove_timer(d, msg.id());
|
remove_timer(d, msg.id());
|
||||||
Ok(null_buf())
|
Ok(null_buf())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_rename_sync(
|
||||||
|
d: *const DenoC,
|
||||||
|
base: msg::Base,
|
||||||
|
_builder: &mut FlatBufferBuilder,
|
||||||
|
) -> HandlerResult {
|
||||||
|
let msg = base.msg_as_rename_sync().unwrap();
|
||||||
|
let oldpath = msg.oldpath().unwrap();
|
||||||
|
let newpath = msg.newpath().unwrap();
|
||||||
|
let deno = from_c(d);
|
||||||
|
|
||||||
|
debug!("handle_rename_sync {} {}", oldpath, newpath);
|
||||||
|
if !deno.flags.allow_write {
|
||||||
|
let err = std::io::Error::new(
|
||||||
|
std::io::ErrorKind::PermissionDenied,
|
||||||
|
"allow_write is off.",
|
||||||
|
);
|
||||||
|
return Err(err.into());
|
||||||
|
}
|
||||||
|
fs::rename(Path::new(oldpath), Path::new(newpath))?;
|
||||||
|
Ok(null_buf())
|
||||||
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ union Any {
|
||||||
MkdirSync,
|
MkdirSync,
|
||||||
ReadFileSync,
|
ReadFileSync,
|
||||||
ReadFileSyncRes,
|
ReadFileSyncRes,
|
||||||
|
RenameSync,
|
||||||
StatSync,
|
StatSync,
|
||||||
StatSyncRes,
|
StatSyncRes,
|
||||||
SetEnv,
|
SetEnv,
|
||||||
|
@ -181,6 +182,11 @@ table ReadFileSyncRes {
|
||||||
data: [ubyte];
|
data: [ubyte];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
table RenameSync {
|
||||||
|
oldpath: string;
|
||||||
|
newpath: string;
|
||||||
|
}
|
||||||
|
|
||||||
table StatSync {
|
table StatSync {
|
||||||
filename: string;
|
filename: string;
|
||||||
lstat: bool;
|
lstat: bool;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue