From owner-freebsd-fs Thu Mar 15 22: 0:21 2001 Delivered-To: freebsd-fs@freebsd.org Received: from smtp05.primenet.com (smtp05.primenet.com [206.165.6.135]) by hub.freebsd.org (Postfix) with ESMTP id 97E9C37B718 for ; Thu, 15 Mar 2001 22:00:15 -0800 (PST) (envelope-from tlambert@usr05.primenet.com) Received: (from daemon@localhost) by smtp05.primenet.com (8.9.3/8.9.3) id WAA27841; Thu, 15 Mar 2001 22:54:42 -0700 (MST) Received: from usr05.primenet.com(206.165.6.205) via SMTP by smtp05.primenet.com, id smtpdAAAkYaGv2; Thu Mar 15 22:54:38 2001 Received: (from tlambert@localhost) by usr05.primenet.com (8.8.5/8.8.5) id XAA04319; Thu, 15 Mar 2001 23:00:07 -0700 (MST) From: Terry Lambert Message-Id: <200103160600.XAA04319@usr05.primenet.com> Subject: Re: Truncating a file. To: andre@netvision.com.br Date: Fri, 16 Mar 2001 06:00:02 +0000 (GMT) Cc: fs@FreeBSD.ORG In-Reply-To: <01031502201400.28129@nv12> from "=?iso-8859-1?q?Andr=E9=20Luiz=20dos=20Santos?=" at Mar 15, 2001 02:20:14 AM X-Mailer: ELM [version 2.5 PL2] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-fs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org > 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