Directory

Ext2 implements directories as a special kind of file whose data blocks store filenames together with the corresponding inode numbers. In particular, such data blocks contain structures of type ext2_dir_entry_2. The fields of that structure are shown in Table 17-5. The structure has a variable length, since the last name field is a variable length array of up to ext2_name_len characters (usually 255). Moreover, for reasons of efficiency, the length of a directory entry is always a multiple of 4 and, therefore, null characters (\0) are added for padding at the end of the filename, if necessary. The name_len field stores the actual file name length (see Figure 17-2).

Table 17-5. The fields of an Ext2 directory entry

Type

Field

Description

u32

inode

Inode number

u16

rec len

Directory entry length

_u8

name len

Filename length

_u8

file type

File type

char [EXT2 NAME LEN]

name

Filename

The file_type field stores a value that specifies the file type (see Table 17-4). The rec_len field may be interpreted as a pointer to the next valid directory entry: it is the offset to be added to the starting address of the directory entry to get the starting address of the next valid directory entry. To delete a directory entry, it is sufficient to set its inode field to 0 and suitably increment the value of the rec_len field of the previous valid entry. Read the rec_len field of Figure 17-2 carefully; you'll see that the oldfile entry was deleted because the rec_len field of usr is set to 12+16 (the lengths of the usr and oldfile entries).

Figure 17-2. An example of the EXT2 directory

Figure 17-2. An example of the EXT2 directory

Was this article helpful?

0 0

Post a comment