LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2011, 08:20 AM   #1
BartekBl
LQ Newbie
 
Registered: Jul 2011
Posts: 8

Rep: Reputation: Disabled
safety of rename() function


Hi! I'm storing some important data in a file lets say "file.txt". I'm pretty fine with old version of the data, but pretty screwed if I lose it all. Algorithm currently looks like this:

(I'm writing in c++)

1) rename() "file.txt" to "file.txt.tmp"
2) fopen() "file.txt" for writing, write the data and fclose()
3) verify data and if correct remove() "file.txt.tmp"

I'm worried that first step can in extreme situation be my undoing. Can you help me here, or should I change renaming into copy-verify-remove?

Target system will be 2.6 ARM Linux, installed on "System on Module" board.
 
Old 08-30-2011, 08:55 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by BartekBl View Post
I'm worried that first step can in extreme situation be my undoing. Can you help me here, or should I change renaming into copy-verify-remove?
Any situation that extreme could make any part of your program your undoing. In Linux rename is a system call, so it doesn't get much safer than that. You might instead copy (manually), write the new data to the original file, and leave the backup. That will make your program safe for hard links and it will make a backup of your old data just in case.
Kevin Barry
 
1 members found this post helpful.
Old 08-30-2011, 08:59 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Why would rename modify or remove your vital file? What's the concern?
 
Old 08-31-2011, 12:12 AM   #4
BartekBl
LQ Newbie
 
Registered: Jul 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
If rename gets interrupted in the middle of its work the file can disappear from the file system even though the data remains intact.

ta0kira: thanks
 
Old 08-31-2011, 04:53 AM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
May I ask where your evidence is for that claim? Is it independant of filesystem used too?
 
Old 08-31-2011, 07:00 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by BartekBl View Post
If rename gets interrupted in the middle of its work the file can disappear from the file system even though the data remains intact.
This should never happen if you're keeping the file on the same filesystem, unless you have either a serious filesystem problem or a serious hardware problem.
Kevin Barry
 
Old 08-31-2011, 09:43 AM   #7
BartekBl
LQ Newbie
 
Registered: Jul 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Well... it was one of my major worries exactly because I DON'T know how it works, not because I know something.
 
Old 08-31-2011, 10:10 AM   #8
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Better start assigning every variable twice then, or redoing each calculation & conditional check, just in case...
 
Old 08-31-2011, 01:40 PM   #9
BartekBl
LQ Newbie
 
Registered: Jul 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
Good one, but missing the point. I know assembly language, so I know how C / C++ works "under the cover", what's reliable end what's not. Furthermore I know TTL, CMOS, so I know how assembly works. And so on... But I have never looked into Linux's source code and I have taken little interest in file systems structure.

If you are sure that rename function is safe under Linux with ext2, well... that's what I'm asking for, isn't it?

Thank all of you guys for your input.
 
Old 08-31-2011, 02:07 PM   #10
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Basically ta0kira nailed it right away, you already have to make many assumptions about how your code will be executed, but beyond doing a copy then (re)move of the old file instead of rename, you're trying to solve integrity at the wrong place/layer.
If the data's so vital, do you have checksums/redundancy codes in the format to ensure it wasn't cut off mid write without detection, etc?
 
Old 08-31-2011, 08:46 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The concept you're looking for is 'atomic'.
As it happens, it is
Quote:
This rename() function is equivalent for regular files to that defined by the ISO C standard. Its inclusion here expands that definition to include actions on directories and specifies behaviour when the new parameter names a file that already exists. That specification requires that the action of the function be atomic.
http://linux.die.net/man/3/rename

 
Old 09-01-2011, 04:22 AM   #12
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
And yet
Quote:
If a call to rename succeeds it is guaranteed to have been atomic from the point of view of the current host (i.e., another program would only see the file with the old name or the file with the new name, not both or neither of them).
But I think the point/fear was what if it isn't successful, might it leave neither, as we're talking about atomic w.r.t. host and not at e.g. processor instruction or disk drive commands level. But that's what filesystems are for, with things like journaling and copy-on-write.
 
Old 09-01-2011, 08:36 AM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Proud View Post
But I think the point/fear was what if it isn't successful, might it leave neither, as we're talking about atomic w.r.t. host and not at e.g. processor instruction or disk drive commands level. But that's what filesystems are for, with things like journaling and copy-on-write.
Atomicity in this case depends on a properly-functioning file-system driver and properly-functioning hardware. Certainly the operation isn't atomic from the point-of-view of the kernel itself. The situations that would compromise rename could therefore compromise open, write, mmap, mknod, mount, link, unlink, chmod, chown, and probably other system calls.

You can't completely protect a single copy of your data, which is why you need to make backups with frequency and numerosity determined by the importance of the data.
Kevin Barry
 
Old 09-02-2011, 04:42 PM   #14
TimothyEBaldwin
Member
 
Registered: Mar 2009
Posts: 249

Rep: Reputation: 27
On ext2 there is danger of the file not being accessible via either of the old or new names, however if there are no other names fsck will put the file in lost+found. With journaled filesystems, in the event of the operation being interrupted it either happens entirely or not at all.

The normal approach is to rename the new file into the place of the old file.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename network interface (Rename ppp0 ...) Nicolas1390 Linux - Newbie 2 08-08-2011 02:14 AM
CLI syntax for rename function? buccaneere Programming 11 10-19-2009 06:59 PM
How does the C function rename work in Linux? dman65 Programming 1 03-09-2009 08:54 AM
Thread safety jkoshi Linux - Software 3 05-03-2007 01:03 PM
Firewall Safety bjb_nyj101 Linux - Security 13 04-02-2007 01:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration