Skip to content
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

Implement deno.truncate #805

Merged
merged 17 commits into from
Sep 30, 2018
Merged

Implement deno.truncate #805

merged 17 commits into from
Sep 30, 2018

Conversation

ztplz
Copy link
Contributor

@ztplz ztplz commented Sep 23, 2018

Node: fs.truncate(path[, len], callback)
Go: func Truncate(path string, length int64) (err error)
Python: os.truncate(path, length)
Rust: pub fn set_len(&self, size: u64) -> Result<()>

Copy link
Contributor

@kevinkassimo kevinkassimo left a comment

Choose a reason for hiding this comment

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

Looks good, 2 comments

src/handlers.rs Outdated
if !deno.flags.allow_write {
return odd_future(permission_denied());
}
let msg = base.msg_as_truncate().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this extra space (you might want to run ./tools/format.py and ./tools/lint.py)

src/handlers.rs Outdated
let len = msg.len();
Box::new(futures::future::result(|| -> OpResult {
debug!("handle_mkdir {}", name);
debug!("handle_mkdir {}", len);
Copy link
Contributor

@kevinkassimo kevinkassimo Sep 23, 2018

Choose a reason for hiding this comment

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

I believe you could do

debug!("handle_truncate {} {}", name, len);

Also, it should be handle_truncate instead of handle_mkdir

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks you

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

Looks good - thanks!

Just a few questions...

const enc = new TextEncoder();
const d = enc.encode("Hello");
const filename = deno.makeTempDirSync() + "/test_truncateSync.txt";
deno.writeFileSync(filename, d, 0o666);
Copy link
Member

Choose a reason for hiding this comment

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

Remove optional mode argument 0o666, it's not relevant to this test.

assertEqual(data.length, 0);

// delete test txt
deno.removeSync(filename);
Copy link
Member

Choose a reason for hiding this comment

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

I would remove all the comments here - it's self explanatory. Also remove blank links where appropriate.

src/handlers.rs Outdated
let len = msg.len();
Box::new(futures::future::result(|| -> OpResult {
debug!("handle_truncate {} {}", name, len);
let mut options = fs::OpenOptions::new();
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that method would truncate the whole file to length 0

debug!("handle_truncate {} {}", name, len);
let mut options = fs::OpenOptions::new();
let f = options.write(true).open(name)?;
f.set_len(len as u64)?;
Copy link
Member

Choose a reason for hiding this comment

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

This is how you truncate in rust? This seems odd.
Can you link me to documentation? I can't find anything on this....

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe he meant this call: File.set_len

Copy link
Member

Choose a reason for hiding this comment

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

Thanks

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 Here is link. https://doc.rust-lang.org/std/fs/struct.File.html#method.set_len In order for use set_len, write access must be used.

js/unit_tests.ts Outdated
@@ -17,5 +17,8 @@ import "./symlink_test.ts";
import "./platform_test.ts";
import "./text_encoding_test.ts";
import "./trace_test.ts";
import "./truncate_test.ts";

import "../website/app_test.js";
Copy link
Member

Choose a reason for hiding this comment

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

Don't include this change

@ry
Copy link
Member

ry commented Sep 27, 2018

Hi - I’ve landed a bunch of changes to the handlers structure including blocking! macros. Can you rebase/update this PR to use them?

@ztplz
Copy link
Contributor Author

ztplz commented Sep 27, 2018

@ry Sure.

src/handlers.rs Outdated
let msg = base.msg_as_truncate().unwrap();
let name = msg.name().unwrap();
let len = msg.len();
Box::new(futures::future::result(|| -> OpResult {
Copy link
Member

@ry ry Sep 30, 2018

Choose a reason for hiding this comment

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

Please use blocking!() here. You'll see examples above.

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 Done.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM - thank you!

@ry ry merged commit 062b22f into denoland:master Sep 30, 2018
@ztplz ztplz deleted the truncate branch September 30, 2018 19:23
ry added a commit to ry/deno that referenced this pull request Oct 4, 2018
- Improve fetch headers (denoland#853)
- Add deno.truncate (denoland#805)
- Add copyFile/copyFileSync (denoland#863)
- Limit depth of output in console.log for nested objects, and add
  console.dir (denoland#826)
- Guess extensions on extension not provided (denoland#859)
- Renames:
  deno.platform -> deno.platform.os
  deno.arch -> deno.platform.arch
- Upgrade TS to 3.0.3
- Add readDirSync(), readDir()
- Add support for TCP servers and clients. (denoland#884)
  Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
@ry ry mentioned this pull request Oct 4, 2018
ry added a commit that referenced this pull request Oct 4, 2018
- Improve fetch headers (#853)
- Add deno.truncate (#805)
- Add copyFile/copyFileSync (#863)
- Limit depth of output in console.log for nested objects, and add
  console.dir (#826)
- Guess extensions on extension not provided (#859)
- Renames:
  deno.platform -> deno.platform.os
  deno.arch -> deno.platform.arch
- Upgrade TS to 3.0.3
- Add readDirSync(), readDir()
- Add support for TCP servers and clients. (#884)
  Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants