9fans archive / 2000 / 04 / 31 / prev next
From: Alexander Viro viro@mat...
Subject: [9fans] truncate syscall
Date: Mon, 10 Apr 2000 13:41:29 -0400 (EDT)
On Mon, 10 Apr 2000, Tom Duff wrote:
> > mmap(). truncate() is a bad name - it's setsize(). IOW, it can extend
> > files. Add the mmap() semantics in that respect and there you go -
> > open()/ftruncate() to set the size/mmap() the region/start working; is
> > quite common. Yes, you can kludge around it with lseek();write(); but
> > that's a kludge, unless we accept that zero-length write() changes the
> > file size. Which is not true under a lot of Unices (I seriously suspect
> > that it's explicitly prohibited by POSIX or something like that).
>
> int f=create("file", OREAD, 0666);
> if(n){
> seek(f, n-1, 0);
> write(f, "", 1);
> }
>
> What exactly is kludgy about this?
Almost noting (except that you may want to change the size of existing
file and thus it turns into lseek(...,SEEK_END) and comparing the sizes).
However, I suspect that I know the reason why this question appeared -
implementation of 9p _client_. And ->setattr() or equivalents in many
variants of VFS allow changing the size. So it boils down to graceful
error value or finding a way to do it over 9p...
> I've been using UNIX & Plan 9 for 26 years,
> and not once have I wanted to chop the tail
> off a file. I find it really hard to believe
> that you need this so badly that you want to
> change 9p. Why can't you afford to rewrite
> the file?
Usually OK for applications, but putting this kind of code into the
kernel... Ewww...