~sircmpwn/hare-users

6 4

How to append bytes to a file?

Details
Message ID
<D0KMU3TROWU3.1TSYK02KC80QZ@etik.com>
DKIM signature
pass
Download raw message
Sorry if it is a noob question, but I'm having issues appending data
somewhere in a file.

This code works on my machine and writes the buffer to a file :
use os;
use io;
use fs;
use fmt;
use strings;

export fn main() void = {
	let file = os::open("file", fs::flag::WRONLY)!;
	defer io::close(file)!;
	let buf : []u8 = strings::toutf8("Coucou hibou");

	match (io::writeall(file, buf)) {
	case let i : size => fmt::printfln("Written {} bytes", i)!;
	case let err: io::error => fmt::printfln("{}", io::strerror(err))!;
	};
};

Then, I looked at the documentation and used the fs::flag::APPEND mode
instead of fs::flag::WRONLY. However, the programm outputed "Bad file
descriptor".

I'm running Alpine linux 3.19, and installed hare from the alpine repos
(version hare dev+d94f355).

If someone knows why this happens, thanks for your answer!

-- 
Elio B.
Details
Message ID
<8508591e-6356-4422-ac4f-f0cc66056e3a@app.fastmail.com>
In-Reply-To
<D0KMU3TROWU3.1TSYK02KC80QZ@etik.com> (view parent)
DKIM signature
pass
Download raw message
Hi!

I think you want this:

    os::open("file", fs::flag::WRONLY | fs::flag::APPEND)!

I wonder if we should clear up the documentation on this somehow, I think you're the second person to ask this.

Vlad
Details
Message ID
<Zh0LEnugBx0RGDP0@altai>
In-Reply-To
<D0KMU3TROWU3.1TSYK02KC80QZ@etik.com> (view parent)
DKIM signature
pass
Download raw message
> If someone knows why this happens, thanks for your answer!

WRONLY, RDONLY and RDWR are three different open modes for each other. If you
don't have WRONLY or RDWR flag on a file, you cannot write to it.

But it seems you cannot append data with only WRONLY flag. By default it
discards old content. You should use both of WRONLY and APPEND like this:

```
use os;
use io;
use fs;
use fmt;
use strings;

export fn main() void = {
	let file = os::open("file", fs::flag::WRONLY | fs::flag::APPEND)!;
	defer io::close(file)!;
	let buf: []u8 = strings::toutf8("coucou hibou\n");

	match (io::writeall(file, buf)) {
	case let sz: size =>
		fmt::printfln("written {} bytes", sz)!;
	case let err: io::error =>
		fmt::errorln(io::strerror(err))!;
	};
};
```

This is worked for me. For more detailed information about open's behaviour,
you can see open(2) man page.
Details
Message ID
<D0KQFCDQY0QT.BIFJ4Y742IBH@etik.com>
In-Reply-To
<8508591e-6356-4422-ac4f-f0cc66056e3a@app.fastmail.com> (view parent)
DKIM signature
pass
Download raw message
On Mon Apr 15, 2024 at 1:01 PM CEST, Vlad-Stefan Harbuz wrote:


>     os::open("file", fs::flag::WRONLY | fs::flag::APPEND)!
It's that, thank you !
I should have figured that it is similar to C, but didnt get that you
can pass unions as parameters.

-- 
Elio B.
Details
Message ID
<71fcd671-9305-4402-ac21-dc69d8f25992@app.fastmail.com>
In-Reply-To
<D0KQFCDQY0QT.BIFJ4Y742IBH@etik.com> (view parent)
DKIM signature
pass
Download raw message
On Mon, 15 Apr 2024, at 14:31, Elio wrote:
> It's that, thank you !
> I should have figured that it is similar to C, but didnt get that you
> can pass unions as parameters.

Oh, that's not a union, it's a bitwise OR:
https://en.wikipedia.org/wiki/Bitwise_operations_in_C
Details
Message ID
<D0KR8V3GXK5D.ZQ9IR0K88UQI@etik.com>
In-Reply-To
<71fcd671-9305-4402-ac21-dc69d8f25992@app.fastmail.com> (view parent)
DKIM signature
pass
Download raw message
On Mon Apr 15, 2024 at 3:33 PM CEST, Vlad-Stefan Harbuz wrote:

> Oh, that's not a union, it's a bitwise OR:
Oh, thank you. I think I learned something important

-- 
Elio B.
Details
Message ID
<D0KYEG0QJLBK.2G9M9ZT3GHNW2@turminal.net>
In-Reply-To
<8508591e-6356-4422-ac4f-f0cc66056e3a@app.fastmail.com> (view parent)
DKIM signature
pass
Download raw message
On Mon Apr 15, 2024 at 1:01 PM CEST, Vlad-Stefan Harbuz wrote:
> Hi!
>
> I think you want this:
>
>     os::open("file", fs::flag::WRONLY | fs::flag::APPEND)!
>
> I wonder if we should clear up the documentation on this somehow, I think you're the second person to ask this.

The problem here is that Hare copied the api directly from C, when we
could have split the flags parameter into 'mode' and 'flags', where mode is one
of RDONLY, WRONLY, RDWR, and everything else is flags. I'm not sure if this is
still fixable, it's a breaking change, and opening files is something a lot of
Hare code does.
Reply to thread Export thread (mbox)