LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 10-25-2006, 11:01 AM   #1
claytonjohnroby
LQ Newbie
 
Registered: Oct 2006
Posts: 19

Rep: Reputation: 0
Convert a hardlink to a file with its own inode


A file has an inode.
The inode is a data structure that contains the information that the filesystem needs to find the raw data of the file in the filesystem media.
The inode has a count of all the files that connect to it.
When you use 'ln' to create a hard link to a file you increase the ref count on the inode and you have a new file with that same inode as the original file.

eg:

filename inode
file1 1

inode 1 now has a refcount of 1

ln file1 file2

filename inode
file1 1
file2 1

inode 1 now has refcount of 2

file1 and file2 are just 2 names that refer to the same inode which is to say the same data in the memory storage medium.
edits to file1 will show up in file2
If I now copy

cp file1 file3

filename inode
file1 1
file2 1
file3 2

edits to file1 will show up in file 2 but not in file3 because file3 links to a different inode and hence to a different set of data within the data storage medium.

I am looking for a command that allows me to change a filenames inode to a new inode that refers to a copy of the data referred to by its original inode.

With reference to the above example

filename inode
file1 1
file2 1
file3 2

command file2

would result in

filename inode
file1 1
file2 3
file3 2

Where inode 3 refers to a copy of the data currently referred to by inode 1

This would be the same result as

cp file1 tempfile
rm file2
mv tmpfile file2

Although this is a solution that could work quite well and a script could be devised that could do this, and even handle situations where the tempfile name might clash with something already there and could also handle the situation where the process of making the new copy of the data might over fill the disk. I was wondering if there is a switch on either cp mv ln or any other command that perfoms this function.
 
Old 10-25-2006, 12:27 PM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 1,950

Rep: Reputation: 279Reputation: 279Reputation: 279
Boy, that was a long post. I *think* I understand what you are asking, but I'm not sure!

If you have two copies of the data (no matter how many links point to these copies), and you now want THREE copies of the data, you're just going to have to cough up more disk space to store that third copy. No if's, and's, or but's about it. There is no magic way to store data on a disk without using disk space that I'm aware of.
 
Old 10-25-2006, 12:33 PM   #3
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 1,950

Rep: Reputation: 279Reputation: 279Reputation: 279
Oh wait, I reread your post and you're not asking about running out of disk space.

You can try:
Code:
$ cp --remove-destination file1 file2
 
Old 10-25-2006, 01:17 PM   #4
claytonjohnroby
LQ Newbie
 
Registered: Oct 2006
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by haertig
You can try:
Code:
$ cp --remove-destination file1 file2
That works
Still it would be nice to have something that only needs a reference to the subject file.

I would like a switch to mv that tells it to perform as if it is moving across filesystems. Normally if you mv within a filesystem the filename referring to the inode is all that changes. If you mv across filesystem boundries a copy is created on the destination filesystem with its own destination inode and a destination file is created to link to the new destination inode and then the source file is unlinked from the source inode and if the source inode's refcount drops to 0 then the source inode is released and can be reused by the filesystem.

Last edited by claytonjohnroby; 10-25-2006 at 01:18 PM.
 
Old 10-25-2006, 01:57 PM   #5
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 1,950

Rep: Reputation: 279Reputation: 279Reputation: 279
Quote:
Originally Posted by claytonjohnroby
I would like a switch to mv that tells it to perform as if it is moving across filesystems. Normally if you mv within a filesystem the filename referring to the inode is all that changes. If you mv across filesystem boundries a copy is created on the destination ...
So you prefer the inefficient over the efficient? For the life of me, I can't figure out why. But if you really want to wipe out program shortcuts and speedups, just write your own mv command.

Here it is in pseudo-code. Implement it in whatever language you like.
Code:
open(A, ro)
open(B, rw)
while read(A) { write(B) }
close(A)
close(B)
remove(A)

Last edited by haertig; 10-25-2006 at 01:59 PM.
 
Old 10-25-2006, 08:20 PM   #6
claytonjohnroby
LQ Newbie
 
Registered: Oct 2006
Posts: 19

Original Poster
Rep: Reputation: 0
The reason that I want this is that I am currently developing a website on my linux box and uploading it to the server when I have completed updates.

In each directory I have some files that are the same for each directory of the project. I would like to edit these files as if they were all one file so hardlinking them seems the way to go. This results in a single file to be edited in my development environment and separate copies of this single file in each directory of my production environment because copying of several files linked to the same inode from one system to another results in each destination file having its own inode.

Once I set this up I asked myself the question of how to then customise these constant files in these directories when I need to. As I pointed out in my original post I can always copy to a temp file, remove the hardlinked file and then move the temp file to the original filename or I could copy with a remove destination option from another file that shares the inode as was demonstrated earlier. The first option is a bit inelegant and the second has the requirement that you need to find another file that shares the inode of the file you want to convert.

A more complete description of what I am looking for

Code:
$command file1
Assigns a new inode and copies to it the data from the original inode like cp would do.
Delink file1 from its current inode and decrement the inode's refcount by 1 like rm would do.
Link file1 to the new inode and increment the inode's refcount by 1 like ln would do.

This would eliminate the need for a temp filename and eliminate the need to find another file with the same inode.

I think if such a facility does not already exist that it would be a good feature to add to bash and to the stand alone versions of either ln, mv or cp.
 
Old 10-25-2006, 11:56 PM   #7
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 1,950

Rep: Reputation: 279Reputation: 279Reputation: 279
Try putting your common files is one place and using symlinks, not hardlinks. A symlink will copy from system to system. Do "man ln" and look at the -s option.
 
Old 10-27-2006, 01:55 AM   #8
claytonjohnroby
LQ Newbie
 
Registered: Oct 2006
Posts: 19

Original Poster
Rep: Reputation: 0
I have found something relating to what I am looking for. It is called "copy-on-write link breaking" and it is used by VServer. It allows a collection of files that are common to several guest operating systems to be hardlinked to a single file in a central repository. If one of the guest operating systems then writes to the file through their link to the shared file the link is broken and a copy that belongs only to the guest operating system with the modifications the guest operating system made is created.

Further research shows that this is a concern all throughout "kernel level isolation" virtualisation where there is a desire to share files across multiple guests.

Now I just need to find out how to do this for myself.
 
  


Reply

Tags
cp, file, inode, link, ln, mv


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
access file using inode number? allasso Linux - General 1 09-26-2006 09:14 PM
getting hardlink for Thunderbird to work gravity-1 Linux - Newbie 3 03-07-2005 04:11 PM
Is hardlink support on Jffs FS? alice95089 Linux - Software 0 02-17-2005 10:11 PM
How to repair File System Error - unattached inode 37138 ch.keller Linux - Software 1 07-21-2004 07:42 PM
Hardlink shivanandbg Linux - General 1 04-27-2004 10:14 AM


All times are GMT -5. The time now is 07:24 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration