Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Mar 2001 06:00:02 +0000 (GMT)
From:      Terry Lambert <tlambert@primenet.com>
To:        andre@netvision.com.br
Cc:        fs@FreeBSD.ORG
Subject:   Re: Truncating a file.
Message-ID:  <200103160600.XAA04319@usr05.primenet.com>
In-Reply-To: <01031502201400.28129@nv12> from "=?iso-8859-1?q?Andr=E9=20Luiz=20dos=20Santos?=" at Mar 15, 2001 02:20:14 AM

next in thread | previous in thread | raw e-mail | index | archive | help
>   With ftruncate, you can remove part of the end of a file. Is there a way to 
> remove part of the beginning of a file?
>   I'm developing a SOCKS5 server that stores the data received from the first 
> connection to a local file, and when the second connection is writable, read 
> that file and write the data to this second connection. As data is read from 
> the local file, its beginning becomes useless, so I'd like to truncate it 
> out. Is it possible?

FreeBSD doesn't support the defacto industry standard F_FREESP
fcntl(2) command argument (which would free the area referred
to by the contents of a flock structure).

It would be trivial to implement support for it, if you wanted
to do so, as long as you realize that not all FS types actually
support sparse files (which is what you are actually asking to
be able to create from a preexisting non-spparse file).

--

I'd actually recommend that you divide the file up into an index
block region and a data block region.  Use the index blocks at
the front of the file to find data blocks at the end.  This will
let you move the data blocks around to get rid of space in the
middle when a data block becomes empty, and then truncate the
end of the file.  Wasted space in an index block is not really a
problem, since it can be small (I suggest an off_t for the data
block for the index start, plus a bitmap of data blocks allocated
contiguously for the number of bits in the bitmap).

Don't use [f]truncate(2) if you are using mmap(2), unless you
first unmap the region and remap it (incremental munmap(2) is
undefined, or was, the last time I looked).  You will only be
able to truncate on FS block (or fragment) size boundaries, in
any case, regardless of what the file size says, since those
allocations will be there, whether they are reflected or not...
so you ought to use statfs(2) to the the f_bsize parameter, and
allocate data blocks in those increments and on that boundary
(e.g. start_log % f_bsize == 0).


					Terry Lambert
					terry@lambert.org
---
Any opinions in this posting are my own and not those of my present
or previous employers.

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-fs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200103160600.XAA04319>