Skip to content

openSync/writeSync/closeSync produces empty file #7879

Description

@gracjan

Code:

var fd = fs.openSync("abc.txt", "w");
var buf = Buffer.from("Was here.\n");
fs.writeSync(fd, buf);
fs.closeSync(fd);

produces empty file. Expected is that it writes whole buffer. Note that fs.writeSync(fd, "Was here.\n"); writes whole string.

  • Version:
    v6.2.2
  • Platform:
    Darwin MacBook-Pro-3.local 14.5.0 Darwin Kernel Version 14.5.0: Thu Apr 21 20:40:54 PDT 2016; root:xnu-2782.50.3~1/RELEASE_X86_64 x86_64
  • Subsystem:
    fs(?)

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jul 26, 2016
  2. saghul commented on Jul 26, 2016

    @saghul
    Member

    The writeSync call returns 0, which means no data was written. Didn't have time to investigate further.

  3. addaleax commented on Jul 26, 2016

    @addaleax
    Member

    fs.writeSync(fd, buf, 0, buf.length); should work; right now, fs.writeSync always uses the length argument, even when it’s undefined. I doubt that’s intentional, though.

  4. papandreou commented on Jul 26, 2016

    @papandreou
    Contributor

    Sounds like you've run into the same problem for writeSync as I did with write a few days ago: #7856

  5. gracjan commented on Jul 26, 2016

    @gracjan
    Author

    @papandreou: looks alike. Will the fix there also fix my issue?

  6. papandreou commented on Jul 26, 2016

    @papandreou
    Contributor

    I don't think it will in its current state, no. I'm on holiday for the next week, but will make sure to pick it up if noone else has landed a fix in the mean time. It's weird, seems like this bug has been there since at least 0.6. It really is the most basic form of fs.write(Sync) -- and now the two of us both discover it within half a week :)

  7. gracjan commented on Jul 26, 2016

    @gracjan
    Author

    @papandreou, alternatevly you could change this here

    // Wrapper for write(2).
    so that WriteBuffer accepts the following spec:

    // Wrapper for write(2).
    //
    // bytesWritten = write(fd, buffer, offset, length, position, callback)
    // 0 fd        integer. file descriptor
    // 1 buffer    the data to write
    // 2 offset    where in the buffer to start from if integer, 0 if undefined or null
    // 3 length    how much to write if integer, buffer length minus offset if undefined or null
    // 4 position  if integer, position to write at in the file.
    //             if null or undefined, write from the current position
    

    As I understand WriteBuffer is common meeting point for all write and writeSync functions. Thos functions could be simplified after WriteBuffer gets new functionality.

  8. papandreou commented on Dec 27, 2016

    @papandreou
    Contributor

    Fixed by #7856 (released in 7.2.0). This issue can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions