News for hackers. Stuff that matters.
Re:Be wary of functions which take several parameters of the... by yellowapple on Thursday September 26, 2019 @09:05PM (#21087482)
Or sidestep this problem entirely by being explicit:

    contents = ReadFile('foo')
    WriteFile('bar', contents)
Or more concisely:

    WriteFile('bar', ReadFile('foo'))
(Of course, this would likely preclude the ability to do some means of copying that doesn't involve reading the whole file into memory).
Re:Be wary of functions which take several parameters of the... by jiggawatts on Thursday September 26, 2019 @09:18PM (#21087538)
Please don't ever recommend such code, even in jest.

This is Wrong, with a capital W.

The file copy API is fundamentally different to reading and and then writing the contents of a file.

The "copy" system call on most platforms also:

- Copies metadata such as attributes.

- Copies alternate data streams.

- Preserves advanced features such as sparse files, compression, encryption, BTRFS/ReFS/ZFS integrity settings.

- Can copy externally archived files without restoring them.

- Can copy files server-to-server without transferring the data to the client computer.

- Copies block-by-block instead of all at once, allowing files to be copied that do not fit into memory.

- Copies asynchronously so that reads and writes are overlapped

- Etc...

Re:Be wary of functions which take several parameters of the... by viraptor on Friday September 27, 2019 @01:34AM (#21088753)
- can be almost instant and copy only metadata on CoW filesystems
Re:Be wary of functions which take several parameters of the... by yellowapple on Friday September 27, 2019 @12:58PM (#21093926)
There are lots of usecases where the things you mention are either not important or outright bad. My code also allows for extra data processing steps in between should the use case evolve beyond mere copying.

The code above could, however, just deal with references to some file handle and not actually read or write the contents (though ReadFile() or WriteFile() would be less-than-ideal names for such functionality).

Re:Be wary of functions which take several parameters of the... by kedean on Friday September 27, 2019 @03:00PM (#21095212)
That completely ignores the purpose of the abstraction. Copy is a high level concept that, yes, can sometimes be reduced to a read/write pipeline, but in many cases cannot. Interfaces and abstractions are important concepts that you shouldn't naively discard.

You're also focusing too much on the example, and missing the greater problem. There are other cases that fit the original problem, such a sending an email (who is the sender and who is the receiver?)

Re:Be wary of functions which take several parameters of the... by yellowapple on Friday September 27, 2019 @06:31PM (#21096847)
> Copy is a high level concept

My point is that the high-level concept is not always appropriate. Sometimes it's overkill. Sometimes it's outright wrong.

> sending an email (who is the sender and who is the receiver?)

The sender is already encoded in the message itself in that case. The receiver is specified as a specific SMTP message, last I checked. So this problem wouldn't really apply here.

Even if it did, though, the separated functions would still be useful here:

    msg = NewMessageFrom("[email protected]")
    AddSubject(msg, "Did you ever hear the tragedy of Darth Plagueis the Wise?")
    AddBody(msg, "I thought not.  It's not a story the Jedi would tell you.")
    Send(msg, "[email protected]")
Re:Be wary of functions which take several parameters of the... by bluGill on Friday September 27, 2019 @08:12AM (#21090983)
If your file operations are designed for it they can recognize the copy situation above and fix it. I don't think it is practical to implement it, but I can design a toy language that would do the above. (Toy because to make it possible to do the above I would have to compromise other things wroth having)
Re:Be wary of functions which take several parameters of the... by kragen on Friday September 27, 2019 @10:23AM (#21092311)
You can definitely make the API you describe do the things you say it can't, as well as the things other grouchy commenters are saying it can't. But in that case it might be better to rename ReadFile to Source and WriteFile to CopyTo.