What is the cause of file truncation?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users What is the cause of file truncation?
# 1  
Old 10-03-2005
What is the cause of file truncation?

Hi,

I have a program that gets called from the front end of my application. Actually it creates some temporary files and uses them and deletes them at last. But sometimes, say once in 6 times, some of these temporary files are getting truncated in the middle and because of this my program is behaving irregularly. My application runs on AIX.

I am not sure -
1) whether some other process is truncating the files, or
2) My program itself is writing the files incompletely.

If I restart the same operation again, I am able to proceede correctly. This kind of trucation of files is happening only some times, say once in 6 times.

I want to monitor these temporary files from creation to the deletion - like what processes are writing to them, using them, truncating them etc.

Can you please tell me, is there a way to do this task? Or, any other better way of solving this problem is possible?

Thanks,
Venkat.
# 2  
Old 10-03-2005
I am not sure if this would help.

Did you try strace ? Read the man pages. It outputs all the system calls by a process. strace is usually for the whole application. In your case it would be the program.

vino
# 3  
Old 10-03-2005
Make sure you call fflush() after every write to your temp files.

This sounds like a program design issue more than a problem with the filesystem.
# 4  
Old 10-04-2005
Quote:
Originally Posted by jim mcnamara
Make sure you call fflush() after every write to your temp files.

This sounds like a program design issue more than a problem with the filesystem.
No. I think it cannot be. Because, it is working well on other environments. It's the problem only on my system/environment. Moreover, it has fflush() after every write.
# 5  
Old 10-04-2005
Are you checking return codes on ALL your file calls?

If you are working on a busy disk where apps create a lot of temp files (like /var/tmp), it is possible for write() not complete successfully because of transient disk full errors. Since this only happens once in a while, this must be the case.

Also consider defining TMPDIR to point to a filesystem with lots of free space or with low disk contention.

If you don't check return codes, the program runs merrily on, regardless of disk free space. I've seen your problem exactly as you describe it under these cricumstances.
# 6  
Old 10-04-2005
I have observed this on one of our systems too. I tried to simulate this using the following programs:

fop.c - uses fopen and fwrite
Code:
#include<stdio.h>
#include<string.h>
#include<errno.h>

int main() {
        FILE *fp;
        char str[]="test";
        int ret;

        fp=fopen("/mount_pt/testfile","w");
        if(fp==NULL) {
                fprintf(stdout,"errno: %d",errno);
                exit(-1);
        }
        ret=fwrite(str,1,strlen(str),fp);
        fprintf(stdout,"ret of write: %d",ret);
        if(ret==0) {
                fprintf(stdout,"couldnot write! errno: %d",errno);
                exit(-1);
        }
        fclose(fp);
}

op.c - uses open and write
Code:
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>

int main() {
        int fd;
        char str[]="test";
        int ret;

        fd=open("/mount_pt/testfile",O_CREAT|O_RDWR,0664);
        if(fd==-1) {
                fprintf(stdout,"errno: %d",errno);
                exit(-1);
        }
        ret=write(fd,str,strlen(str));
        fprintf(stdout,"ret of write: %d",ret);
        if(ret==-1) {
                fprintf(stdout,"couldnot write! errno: %d",errno);
                exit(-1);
        }
        close(fd);
}

I simulated a full filesystem by creating a 4MB filesystem and filling it up. Then ran the op.c and fop.c programs on this. op.c gives an error when trying to 'write'. However, fop.c goes through successfully - fwrite even returns the expected values, but all that is created is a 0 byte file.

This may have something to do with the buffering that is done when 'fwrite'ing - this causes the 'fwrite' to return success, even though 'write' fails.
But this does not really sound right.. could any one shed light on this?

Last edited by blowtorch; 10-04-2005 at 03:57 PM..
# 7  
Old 10-04-2005
blowtorch, your problem is due to buffering as you suspect. You could use setvbuf() to unbuffer. Or you could check the return code from fclose() which will detect the problem. Ideally, you check the return code from close() as well... although no one ever does. With an NFS mounted filesystem, close() could be the syscall that detects a full filesystem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. UNIX and Linux Applications

Space truncation

HI All- We have script like the followinga='h1 ' b='12434 ' c=' fagkasdbkZ<M' output=$a$b$c echo $output > /home/dsomasun/fil_concat.txtBut in the output file spaces are truncated and Output is coming like the belowh1 1234 fagkasdbkZ<M please advise Please wrap all code,... (4 Replies)
Discussion started by: dhilipans1988
4 Replies

3. UNIX for Advanced & Expert Users

Truncation Occurs When Outputting Shell Script to stderr

Operating System: Solaris 10, Shell We are outputting the results of our scripts to the stderr file. However we have encountered a problem where some of the lines in the file are truncated. Is there a way to increase the terminal or column size within the script so that this does not... (4 Replies)
Discussion started by: fazzasx
4 Replies

4. Filesystems, Disks and Memory

SCO 5.6 + file truncation

Sir, my apologies for I had put this question in a different forum earlier. I am running an appliction in SCO 5.6. If the records are more than one lakh, the output file is getting truncated to one lakh. Please help me in rectifying the issue whereby it allows creation of output with more than... (0 Replies)
Discussion started by: chssastry
0 Replies

5. Post Here to Contact Site Administrators and Moderators

SCO 5.6 File truncation

Sir, my apologies for not being able to correspond for a long time. I am running an appliction in SCO 5.6. If the records are more than one lakh, the output file is getting truncated to one lakh. Please help me in rectifying the issue whereby it allows creation of output with more than one lakh... (0 Replies)
Discussion started by: chssastry
0 Replies

6. UNIX for Advanced & Expert Users

Syslog Truncation / Size Limit?

I searched and could not find an answer to this, and am having difficulty, so I would appreciate any assistance that can be offered. We're experiencing logs that are having all characters after the 850th one written being truncated. I am wondering if there is a default limit to the syslog... (1 Reply)
Discussion started by: Puck
1 Replies

7. UNIX for Advanced & Expert Users

STDOUT truncation

Operating system - Solaris 8, Korn shell, xterm Command /usr/proc/bin/ptree outputs the process tree exactly as I want with all detail However, /usr/proc/bin/ptree | more truncates the process descriptions at an 80 character point. If the process tree has marched enough to the right,... (3 Replies)
Discussion started by: JerryHone
3 Replies

8. AIX

PS output truncation in AIX

folks; i need help with a truncation problem in AIX when running "ps" command. when i run ps in SUN, i got the full output but when i run it in AIX the output is truncated. is there any arguments i can pass to give longer output so i can overcome the issue? here's the details:(please note the... (2 Replies)
Discussion started by: moe2266
2 Replies

9. UNIX for Advanced & Expert Users

truncation when vi a file

Hi, I'm using the vi that comes with Solaris 2.8. When vi a file with lines that have long continuous characters e.g. more than 256 chars, these lines got truncated. This is a known limitation of the out of the box Solaris vi, and there is a suggestion to get another vi. Is there an... (0 Replies)
Discussion started by: selur-xinu
0 Replies

10. Solaris

solaris 2.5.1 /usr/ucb/ps truncation problems

we aheva couple of old sun OS boxes, that we are trying to parse /usr/ucb/ps output. However it seems that something is occuring that is causeing th output of "/usr/ucb/ps -auxwww" to cut short the process name, whereas "ps -eaf" can display the entire process name. It will work for a while... (2 Replies)
Discussion started by: adralph
2 Replies
Login or Register to Ask a Question