You are not logged in.

#1 2010-08-09 15:39:57

patrickthebold
Member
Registered: 2008-12-15
Posts: 55

Quick Question on Hardlinks

If a file has two hard links and I edit it, should there still be two links to it? 

For example, when I do do the following "a" and "b" have the same inode:

sh-3.2$ echo foo > a
sh-3.2$ ln a b
sh-3.2$ echo bar >> b
sh-3.2$ ls -i
14049345 a  14049345 b

However if I edit a file with gnumeric I get different inodes:

sh-3.2$ gnumeric
Writing file:///a.gnumeric
sh-3.2$ ln a.gnumeric b.gnumeric
sh-3.2$ gnumeric b.gnumeric 
Reading file:///b.gnumeric
Writing file:///b.gnumeric
sh-3.2$ ls -i
14049346 a.gnumeric   14049347 b.gnumeric

Would this be considered a bug in gnumeric?  Should I not expect the two files to stay linked?  I'm not really sure how to use hard links with the two different behaviors.

Offline

#2 2010-08-09 15:55:26

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Quick Question on Hardlinks

No, apparently this is the recommended way of doing things.

Edit: You would expect Gnumeric to edit the file in place. What I think it is doing is replacing the entire contents of the file on write. I'm not sufficiently familiar with Gnumeric to explain why they chose this way. I can tell you this is the reason why the inodes are different.

Last edited by fsckd (2010-08-09 16:24:45)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#3 2010-08-09 22:32:31

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Quick Question on Hardlinks

In a nutshell:  There is not likely to be enough space to edit the file in-place without lots of fragmentation, so most editors and userspace programs save modified copies rather than making in-place edits.

On the other hand, when you edit a file with Emacs or by using the -i flag to Perl or sed or any of a number of other command line apps, the original file is renamed without copying.

% ls -i text 
132109 text
% sed -i~ -e 's/e//' text
% ls -i text*            
132108 text  132109 text~

In this example, the original file was renamed and the modified copy was saved with the original name to a different inode.

Offline

#4 2010-08-10 15:36:50

Proofrific
Member
Registered: 2008-01-05
Posts: 215

Re: Quick Question on Hardlinks

Trent wrote:
% ls -i text 
132109 text
% sed -i~ -e 's/e//' text
% ls -i text*            
132108 text  132109 text~

In this example, the original file was renamed and the modified copy was saved with the original name to a different inode.

Nice example and explanation.

Offline

Board footer

Powered by FluxBB