truncate a file

I had asked this question previously but I closed out the question prematurely:

"Is there a way to truncate a file while it is open and being written to? I'm using an application that hasn't been set up with log file rolling and I'm trying to manange it externally."

I put togther a program that uses the truncate() function which I though was working the way I wanted to, but it turns out it won't truncate a file while another program has that file open. I even tried suspending the first process, truncating the file, and then resuming the suspended process. The file size was reduced, but after I resumed the process, the file was restored to the size it had before I suspended the process (argh).

Any input much appreciated,
Ollie


opikeAsked:
Who is Participating?
 
NopiusConnect With a Mentor Commented:
opike, what OS/version are you using?
All above is correct, you CANNOT truncate open file externally.

Sometimes there are no direct ways of doing things (yes, it's your case), but some workarounds (dirty hacks) are possible :-)

I know, truncating of file, open by another process (it doesn't matter if it was opened with APPEND or WRITE mode) is _possible_.
You only need to fix some fields in one kernel table and their copies in userspace of running process.
With kernel space you may write your own syscall handler (yes, it's possible) :-)
With user process space you may also use new syscall or it might be possible to use ptrace() syscall.
If you are ready to go with all these difficulties (read: this truncation is _so_ importent for you), we will proceed.

Have a nice weekend.

Nopius
0
 
ozoConnect With a Mentor Commented:
head file > temp
mv temp file

but any other writes that the process does to the file before closing it will be lost

0
 
ravenplConnect With a Mentor Commented:
I guess in Your case it's impossible. Thats, becouse the original application haven't opened the file in APPEND mode (that's why it restores to the original size after truncate)...
In such case, the orig application (after resuming) writes to outside the file (at original position), which is fine under UNIX systems (the file grows and it's propably sparse file) :(
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ahoffmannConnect With a Mentor Commented:
> "Is there a way to truncate a file while it is open and being written to? ..
as long as the application has the file open, you can truncate it as ozo suggested, but that truncation will only be "visible" at user level, the filesystem still occupies the whole place.
So it's impossible to truncate the size of file from another process while it is in use.
0
 
gheistConnect With a Mentor Commented:
> "Is there a way to truncate a file while it is open and being written to? I'm using an application that hasn't been set up with log file rolling and I'm trying to manange it externally."

If opening process only appends file then
: > file
(null command truncating and outputing to file)
will help

If program writes to file position you have to create a script that makes program reopen file after truncation.
0
 
ravenplCommented:
ahoffmann: unfortunatelly You wrong(at least concerning linux).
If the another process has the file open in O_APPEND mode, everything would work fine. Otherwise, on next write, the filesystem will create sparse file.
But anyway, after truncating, the file is truncated (also in filesystem meaning)
0
 
ahoffmannCommented:
ravenpl, sometimes we learn new things every day, thanks!
0
 
root_startConnect With a Mentor Commented:
A way to "truncate" a file could be the following: cat /dev/null > <file>

I hope it helps.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.