开发者

CGI Buffering issue

I have a server side C based CGI code as:

cgiFormFileSize("UPDATEFILE", &size);   //UPDATEFILE = file being uploaded
c开发者_开发技巧giFormFileName("UPDATEFILE", file_name, 1024);
cgiFormFileContentType("UPDATEFILE", mime_type, 1024);
buffer = malloc(sizeof(char) * size);

if (cgiFormFileOpen("UPDATEFILE", &file) != cgiFormSuccess) {
    exit(1);
}
output = fopen("/tmp/cgi.tar.gz", "w+");

inc = size/(1024*100);
fptr = fopen("progress_bar.txt", "w+");    
while (cgiFormFileRead(file, b, sizeof(b), &got_count) == cgiFormSuccess)
{
    fwrite(b,sizeof(char),got_count,output);
    i++;
    if(i == inc && j<=100)
    {
     fprintf(fptr,"%d", j);
     fflush(fptr);
     i = 0;
     j++;   // j is the progress bar increment value
    }
}
fclose(fptr);
cgiFormFileClose(file);
retval = system("mkdir /tmp/update-tmp;\
                 cd /tmp/update-tmp;\
                 tar -xzf ../cgi.tar.gz;\
                 bash -c /tmp/update-tmp/update.sh");

However, this doesn't work the way as is seen above. Instead of printing 1,2,...100 to progress_bar.txt (referred by fptr)one by one it prints at ONE GO, seems it buffers and then writes to the file. fflush() also didn't work.

Any clue/suggestion would be really appreciated.


First, open the file before the loop and close after it ends. Too much IO.

The problem is here w+ - this truncates your file. use a+. (fopen help)


It is writing it one-by-one, it's just that it does it so fast that you're vanishingly unlikely to ever see the file with a value other than 99 in it.

This is easily demonstrated if you put a sleep(1) within the loop, so that it's slow enough for you to catch it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜