LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scenario for file's Inode update (https://www.linuxquestions.org/questions/linux-newbie-8/scenario-for-files-inode-update-4175600297/)

rushirg 02-21-2017 12:04 PM

Scenario for file's Inode update
 
while reading Maurice Bach's "The Design Of The Unix Operating System", i came across inode.
As file gets updated inode also changes but are there any scenarios like,
- file is same and new inode is assigned
- file is changed and previous(old) inode is still there

Are these situations may happen? if yes how? and why?

rknichols 02-21-2017 12:25 PM

Quote:

Originally Posted by rushirg (Post 5674158)
while reading Maurice Bach's "The Design Of The Unix Operating System", i came across inode.
As file gets updated inode also changes but are there any scenarios like,
- file is same and new inode is assigned
- file is changed and previous(old) inode is still there

Are these situations may happen? if yes how? and why?

If it's a new inode, it's a new file, perhaps created as a copy of the old file and then renamed to replace the old one, but it's still a new file.

A file can be read and rewritten in place (opened for reading and writing) and would keep the same inode. Note that most editors will, for safety, save the new version under a temporary name and then, once the new version has been written successfully, rename it over the old. The file would thus have a different inode. Cases where this can't be done, and the file is thus rewritten in-place, include (a) a directory that is not writable and thus no file creation or renaming can be done there, and (b) files with multiple hard links, since the new file would not share the hard links.

rushirg 02-21-2017 12:43 PM

Yes, i got the concept of how the new inode is assigned to same file.
But i am still not clear about 2nd scenario that, file is changed and old inode remains same. is this case possible?

szboardstretcher 02-21-2017 12:46 PM

Sure. Changing a file directly, the file keeps its inode number.

Code:

touch some_file
ls -alhi some_file
echo "some data" > some_file
ls -alhi some_file
mv some_file /tmp
ls -alhi /tmp/some_file

Code:

# touch some_file
# ls -alhi some_file
3362 -rw-r--r-- 1 root root 0 Feb 21 18:47 some_file
# echo "some data" > some_file
# ls -alhi some_file
3362 -rw-r--r-- 1 root root 10 Feb 21 18:47 some_file
# mv some_file /tmp
# ls -alhi /tmp/some_file
3362 -rw-r--r-- 1 root root 10 Feb 21 18:47 /tmp/some_file


rushirg 02-21-2017 12:56 PM

OK, it remains same. But as i read in the book, it says, as the file gets updated the inode also get changed. do they mean the content of the inode changes like access/modify time?

szboardstretcher 02-21-2017 01:57 PM

I see. Ok, this is the thing.

An "Inode" is an object with a numeric key. The term "Inode" is sometimes simplified and meant as the "inode number" when it really should refer to the record as a whole. For example, an Inode Record contains these fields:
  • Inode number
  • Access Control List (ACL)
  • Extended attribute
  • Direct/indirect disk blocks
  • Number of blocks
  • File access, change and modification time
  • File deletion time
  • File generation number
  • File size
  • File type
  • Group
  • Number of links
  • Owner
  • Permissions
  • Status flags

Notice that the "Inode Record" contains a field called "Inode Number"? That is the source of your confusion. The "Inode Number" does not change with every operation. But other fields in the record itself may.


All times are GMT -5. The time now is 08:22 PM.