Proper way to append lines to a plaintext file

Hello all,

I’m working with text files in a solid pod hosted with CSS. If I wanted to append a line to a text file, what would be the correct way of doing it? I know one way is to retrieve all the data, append to the end of it, then save it back using a PUT, but I wanted to know if anyone had a suggestion or approach that wouldn’t require a full read into memory then saving the entire updated file back to the pod.

Hi :slight_smile:

As far as I know, the only way with plaintext files is to retrieve all of it, then append client side and make a PUT to overwrite the original file (potentially with some headers that suggest the server to only proceed if it was not modified in the meantime, though honoring these headers this is not a MUST for solid pods iirc).

If you use eg text/turtle to save the data, you can use the N3 patch, which will be processed server side and only requires APPEND permissions. The specification including an example is here, a similar discussion on this forum is here.

In general, you probably could make a turtle file to better describe the semantics of the text (eg use an ontology to mark a comment as a comment, such that computer programs can understand it). So it could be a win-win situation (being able to patch the file and being more expressive about the text itself).

1 Like

I see, that’s what I figured but I thought I’d ask. Due to some constraints, writing an RDF or turtle file isn’t great right now, but maybe down the road. For now I’ll just create a separate file each time through POST and then query all the files in the container. Thanks for the help, though.

N3 patch … only requires APPEND permissions

This is only true for patches that insert into existing documents. N3 patch can also be use to create new documents or to delete triples from existing documents both of which operations require WRITE permissions.

3 Likes