Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

general question on writing a file

Posted on 2004-11-17
9
Medium Priority
?
241 Views
Last Modified: 2010-04-15
Hello guys,

I have a program (written in c) which can save files. It runs on red hat linux 7
Now at certain moments the customer wants to make a backup of the files. How can I be sure that my program isn't writing a file at that time : since I write my files line by line (I don't have one big buffer, I open a file, write line by line and then close it), I can be that at the time of the automateed backup, there is only half a file present since the program is still writing.
Are there some known solutions to this problem ?

thnx!

0
Comment
Question by:themuppeteer
  • 4
  • 3
  • 2
9 Comments
 
LVL 5

Accepted Solution

by:
ddunlea earned 512 total points
ID: 12603531
Hi themuppeteer,

I would recommend writing top a temporary file and once it has been fully written, replace the original with the temporary file.

Regards
0
 
LVL 2

Author Comment

by:themuppeteer
ID: 12603591
Hello ddunlea,
thanks for the quick answer.
Yes well I would prefer not changing the original program since if I do, it has to go thrue the complete test cycle again and then we lose a lot of time..+ there isn't a lot of disk space on the device that my program runs on and therefore I must also be careful. But it is a good idea and I'll put it away for the moment  in case nothing more practical for my case comes up.

I was more thinking/hoping that there would be a way for the backup system (which still has to be developed, we are thinking about scp) to let it check if a certain file is opened by some program and in that case log something about that or something.. Do you know if you check in linux if a file is opened ?

regards,
themuppeteer
0
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 508 total points
ID: 12603603
Hi  themuppeteer,

Instead of using streams to read/write the files, open them using the lower-level open() function and use read() and write() to move the data.

open() allows you to control file level locking and share modes so you should be able to set up whatever controls suit your needs.


Good Luck,
Kent
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Author Comment

by:themuppeteer
ID: 12603638
Hello Kent,

I don't use streams, this is the function to write a file (note that the list thing is our own type):

void writeFile(char* name,list* lst)
{
   FILE* file;
   char* str;

   file=fopen(name,"wt");
   if (file==NULL) return;

   listStart(lst);

   while ((str=listNext(lst))!=NULL)
      fprintf(file,"%s\n",str);

   fflush(file);
   fclose(file);
}

Should something be changed to this (I hope not, but if I can lock a file I should consider it)?

Regards,
themuppeteer
0
 
LVL 5

Expert Comment

by:ddunlea
ID: 12603662
Hi themuppeteer,

I agree with Kdo that you could use oepn() to lock the file while reading it. But this doesn't change the fact that you will have to edit your original program. Which you clearly want to avoid.

If you want to completely avoid modifying the original program, then here is what I would suggest - If the file you want to back up is small, then do as follows:

Check the timestamp
Run backup
Re-Check timestamp and compare to original
If different, re-run
Else Done

If the file to be backed up is large, you can write a program that polls the time stamp whilst the file is being copied such that the copy can be aborted part-way through if the file changes.

Regards
0
 
LVL 2

Author Comment

by:themuppeteer
ID: 12603793
sounds like a plan, problem there is that with scp, we will copy a complete directory and subdirectorys , not file per file. Since the customer can also put his own files in that directory if he choses and they should be backed up also.
So there there is a problem with the timestamps..
Another thing is that sometimes file A and file B have dependencies, so you should need a "global" timestamp (more like a checksum) to do this. Since otherwise you could have: file A written, file B backed up, file B written , file A backed up and you're in trouble since A and B don't correspond..
But it is also a good idea.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12603807
Howdy....

Sorry to say, but we've got a breakdown in terminology.  fopen() opens a stream, so you do use streams.

At some level, fopen() will call open() to establish the file access.  Here's a head start.



void writeFile(char* name,list* lst)
{
   int FileHandle;
   char* str;

   FileHandle = open (name, O_WRONLY|O_BINARY|O_TRUNC|O_CREAT, 0);  /*  0 is the share attributes.  You might want some level of sharing  */
   if (FileHandle < 0)
     return;

   listStart(lst);

   while ((str=listNext(lst))!=NULL)
   {
      write (FileHandle, str, strlen (str));
      write (FileHandle, "\r\n", 2);              /* write (FileHandle, "\n", 1) on unix systems  */
   }

   close (FileHandle);
}
0
 
LVL 5

Expert Comment

by:ddunlea
ID: 12603882
Hi themuppeteer,

rsync. It's the answer. Definitely. Mirror the files with rsync. Build a checksum of some sort based on the files you have gathered. Re-run rsync, which will not copy anything if no changes have been made. Re-check the checksum. If it has changed, re-run rsych, otherwise, you're done.

0
 
LVL 2

Author Comment

by:themuppeteer
ID: 12603967
woops, I was thinking c++ streams.. I didn't know fopen opened a stream.
Anyway, thanks for  modifying the code Kent.

Rsync, I worked once shortly with it before and it might indeed be the solution.

I'll bring both up in the next meeting on the subject.

I'm gonna thank you guys for your time and effort,
and I'm gonna split points (hope nobody minds about the one unsplittable point which I'm gonna give to the first replier for being first :)).

thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question