2

I am creating a program that takes a hexdump of a file, so that I can then transmit that somewhere, and have the file reconstructed, however the hexdump is only of the file contents, I also need to get the file properties, so where do I get the properties of that file, and then apply them to the new file that I create from that hexdump? I am using bash for this, but it's not really about the code, but more about where to find it, although you could it's about the code because obviously I am going to need some commands. I am running Ubuntu GNOME 15.04, with GNOME Shell, and GNOME 3.16.

  • by file properties you mean file attributes? – Ron Aug 23 '15 at 12:02
  • @Ron: Yes, I mean the file attributes. –  Aug 23 '15 at 12:02
  • In `inode` I guess. Like `stat /etc/passwd` will give you some details. Extended attributes will be filesystem depended as I understand – Ron Aug 23 '15 at 12:05
  • @Ron: But where are the metadata files actually stored on my file system? And how do I then reapply those attributes once I have transferred the hexdump somewhere else? –  Aug 23 '15 at 12:06

2 Answers2

3

File attributes are stored in inodes. The attributes that each inode stores are listed in POSIX Inode Specification. When we use ls -l or stat or any other program that get us the file attribute uses the stat(2) system call underneath.

Now inodes are filesystem dependent property, they are created as fixed numbers when the filesystem is created. There is a program dumpe2fs to read the superblock of a ext* filesystem so that we can get some idea e.g. :

sudo dumpe2fs -h /dev/sda1

Now let's check the inode related properties:

$ sudo dumpe2fs -h /dev/sda1 | grep -i 'inode.*:'

Inode count:              9379840
Free inodes:              9297243
Inodes per group:         8192
Inode blocks per group:   512
First inode:              11
Inode size:               256
Journal inode:            8

As you can see you get enough info like first inode, inode size, inode count etc. If you do a multiplication of Inode count and Inode size you get how much is reserved for the inodes.

File copying programs such as cp or rsync have the -a (archive) option to copy the attributes stored on the inode for a file, so you need to look for those or similar options available in the program you are likely to use.

For preserving attributes of files and then use it later you can check this answer from mighty Gilles.

heemayl
  • 90,425
  • 20
  • 200
  • 267
  • And when I get the file attributes, how do I then apply those attributes to the file on my other system where I put the hexdump? –  Aug 23 '15 at 18:42
  • @ParanoidPanda [Gilles has it](http://stackoverflow.com/a/3450585/3789550) for you :) – heemayl Aug 23 '15 at 18:51
  • I didn't answer that question?? :O Only @ParanoidPanda had a look at the SO question I linked in my post! :( – Ron Aug 24 '15 at 08:33
  • @Ron Actually there are quite a few differences in our answers, I have put up an answer because your answer lacks some more details IMHO..as for Gilles's answer, I actually get too lazy to do it myself seeing the same thing already answered by Gilles..so you probably get the idea.. – heemayl Aug 24 '15 at 09:17
  • @heemayl sure, there is. You mentioned Gilles's answer explicitly. I couldn't imaging the OP would like all those superblock info, I was looking more from a file perspective..so I suggested `stat` in my comments. No problems though ;) – Ron Aug 24 '15 at 09:29
2

This may not be a complete answer, but this is what I was able to find from my research.

File attributes are stored in inodes and the 'inode tables' are generally scattered throughout the file-system. In general, to find where you have the inode tables (at least in ext3 filesystem), you can run:

sudo dumpe2fs /dev/<device> | fgrep 'Inode table'

and you can do a hexdump of the raw data at the block where the inodes resides (Note I've not tried this command!):

sudo dd if=/dev/<device> ibs=4096 skip=<inode block> count=1 | hexdump

Replace the <device> and the <inode block> with the block offset to the inode table you want to look at.

source: LinuxQuestions, SO question

Ron
  • 20,518
  • 6
  • 57
  • 72
  • I have upvoted this answer, however as it does not fully answer my question, I cannot accept it. –  Aug 23 '15 at 13:55
  • This may help you to give a more complete answer: https://ext4.wiki.kernel.org/index.php/Main_Page –  Aug 23 '15 at 14:05
  • @ParanoidPanda Yeah, I understand :) Thanks for the link. let me see if I can get my head around it. Meanwhile, How good is something like `stat -c '%A %h %U %G %s %y %n' ` to you? There is also `e2image` that can save `dumpe2fs` readable metadata but guess it'll work only on entire filesystem – Ron Aug 23 '15 at 17:01
  • That is quite good, but I need all of the information my system has on it, and then I also need instructions for how to apply these attributes to the file on a new system, because I take the hexdump and put it on a new system, but as the hexdump is only the file contents, I don't also get the attributes, so if you can show me how to get them, it would be really good if you could show me how to associate them with the new file on the new system. You see this is because I am currently studying compression algorithms and need to be able to get the file attributes too, and then apply them. –  Aug 23 '15 at 17:08
  • what more information are you looking for? If you look `man stat`, you'll see that `stat` gives pretty much everything you'll need. – Ron Aug 23 '15 at 18:06