NFS API: can you create a File object before a new file is committed


#1

I’m looking at the NFS API and it looks as if the only way to create a new file is with create() which means you have to commit the file immediately.

Is this correct? Or is there a way to create a new file, then make calls to write(), before either abandoning it or committing it to the network.

It looks to me that you can do that after the file has been inserted into the MD, because then you can do fetch(), open(), write() ... close(), update(), but I can’t see a way to support this way of creating a new file using the NFS API.

Can someone confirm or correct that?

Assuming that’s the case, how can I avoid creating an empty file every time in order to achieve the effect of creating a new file which I can write to multiple times, before I close it and finally insert() into the MD?

Would it work to keep hold of an NFS File object and re-use it for each new file by opening it for overwrite, doing multiple writes, closing and then doing insert()? Then repeating this with that same object?


#2

Glad you asked this because it clarifies a point that I either forget or didn’t completely realize.

Yes, you can accomplish what you asked here.

When you open() a file without passing any arguments, a new file context object will be created in OVERWRITE_MODE, however when you make multiple writes to that file context, each write will be appended to the file context.

You’ll see this in effect being used in web-hosting-manager, where multiple writes are called before closing and committing the file to the network. (The purpose in this case is to upload small chunks at a time to show progress in the UI.)

If you close that file context and open it again in OVERWRITE_MODE, then make a write it will then overwrite the previous contents.


#3

Thanks Hunter, that’s good news.

@dgeddes something to mention in the docs for NFS open().

EDIT: just for my reference, here’s the example call to NFS open() without any arguments:

const file = await nfs.open();