2

I am working with the EXT2 File System and spent the last 2 days trying to figure out how to create a symbolic link. From http://www.nongnu.org/ext2-doc/ext2.html#DEF-SYMBOLIC-LINKS, "For all symlink shorter than 60 bytes long, the data is stored within the inode itself; it uses the fields which would normally be used to store the pointers to data blocks. This is a worthwhile optimization as it we avoid allocating a full block for the symlink, and most symlinks are less than 60 characters long"

To create a sym link at /link1 to /source I create a new inode and say it gets index 24. Since it's <60 characters, I placed the string "/source" starting at the i_block[0] field (so printing new_inode->i_block[0] in gdb shows "/dir2/source") and set i_links_count to 1, i_size and i_blocks to 0. I then created a directory entry at the inode 2 (root inode) with the properties 24, "link1", and file type EXT2_FT_SYMLINK.

A link called "link1" gets created but its a directory and when I click it it goes to "/". I'm wondering what I'm doing wrong...

samuel
  • 43
  • 4
  • 1
    Why are you reinventing the wheel? There is a `symlink()` function for creating symbolic links programmatically, and it has the advantage of not being externally dependent on filesystem details. – John Bollinger Mar 31 '17 at 21:24
  • Hi John, I'm doing this for educational purpose and understanding how symbolic link gets created in the background. – samuel Mar 31 '17 at 21:30

1 Answers1

1

A (very) late response, but just because the symlink's data is in the block pointers that doesn't mean the file size is 0! You need to set the i_size field in the symlink's inode equal to the length of the path

Marco Cutecchia
  • 1,335
  • 2
  • 10
  • 14