Talk:Truncate a file: Difference between revisions

From Rosetta Code
Content added Content deleted
(The task is not limited to Unix)
Line 13: Line 13:


:The task is not limited to Unix. It should be possible to truncate a file on most systems that utilize disks and other storage media type devices. [[User:Markhobley|Markhobley]] 17:11, 19 July 2011 (UTC)
:The task is not limited to Unix. It should be possible to truncate a file on most systems that utilize disks and other storage media type devices. [[User:Markhobley|Markhobley]] 17:11, 19 July 2011 (UTC)

::Truncating on windows is not a problem, just call <code>SetEndOfFile()</code> or <code>truncate()</code> (NT is actually POSIX.1 compliant). It's the renaming part that's inconvenient (still not a real problem). Another thing, the requirement that the routine should bail out if requested size is larger than original is unrealistic. If it's important that the file must be the requested size, you should extend it; if it's not important, why not just leave it alone and move on? If you really cared, you should have checked its size beforehand anyway. --[[User:Ledrug|Ledrug]] 17:21, 19 July 2011 (UTC)

Revision as of 17:21, 19 July 2011

So just to be sure, if the actual file size is smaller than the given truncated size, it's an error? It doesn't just leave the file alone? --Mwn3d 14:09, 19 July 2011 (UTC)

POSIX truncate() and ftruncate() extends the file if specified size is larger than original. It's convenient when you want to reserve some disk space. --Ledrug 14:25, 19 July 2011 (UTC)

Assumes unix

It's pretty clear that this task assumes unix file system semantics. So, can we ignore other operating systems in this task? --Rdm 14:33, 19 July 2011 (UTC)

Why is it clear the task assumes unix behavior? --Ledrug 14:37, 19 July 2011 (UTC)
Quoting http://www.conifersystems.com/2008/10/21/windows-vs-unix-file-system-semantics/ The Windows delete and rename model is different. You wouldn’t know this from the Win32 APIs, but in order to delete or rename a file in Windows, you first have to open it! Once you’ve opened it can you call NtSetInformationFile with InformationClass of FileDispositionInformation or FileRenameInformation. Setting FileDispositionInformation doesn’t even delete the file; it merely enables delete-on-close for the file, and the delete-on-close request could very well be cancelled later.
And, as near as I can tell, you have to get rid of the association between a name and a file before you can give another file that name. --Rdm 14:43, 19 July 2011 (UTC)
I see, you are talking about the rename part. Truncating a file on windows works not very differently from unix. But it's true that unlike on unix where filename is just a link pointing to some inode, windows files are closely tied to the names, fair point. --Ledrug 14:55, 19 July 2011 (UTC)
Just an observation...I think the 'rename' bit is to guarantee an atomic replacement in the namespace. NTFS supports transactions which would accomplish this, although it'd be necessary to catch a transaction failure and retry. Other Windows-supported filesystems may or may not support transactions or suitable semantics. (FAT certainly doesn't. I don't know what other filesystems Windows may have native support for.) --Michael Mol 15:43, 19 July 2011 (UTC)
I wrote a DOS extension service that could truncate files by deleting the entries from the file allocation table and updating the directory entry with the new file size once. This was before Microsoft Windows '95 came along and broke everything. The restrictions on semantics are within Microsoft Windows itself, rather than with the filesystem, There may be an interface somewhere that allows truncation, because disk repair tools need to be able to do such things, although I know Microsoft like to retain a monopoly on such tools, so maybe they forgot to document the interface. Markhobley 17:11, 19 July 2011 (UTC)
The task is not limited to Unix. It should be possible to truncate a file on most systems that utilize disks and other storage media type devices. Markhobley 17:11, 19 July 2011 (UTC)
Truncating on windows is not a problem, just call SetEndOfFile() or truncate() (NT is actually POSIX.1 compliant). It's the renaming part that's inconvenient (still not a real problem). Another thing, the requirement that the routine should bail out if requested size is larger than original is unrealistic. If it's important that the file must be the requested size, you should extend it; if it's not important, why not just leave it alone and move on? If you really cared, you should have checked its size beforehand anyway. --Ledrug 17:21, 19 July 2011 (UTC)