int rtems_rfs_dir_lookup_ino (rtems_rfs_file_system* fs, rtems_rfs_inode_handle* inode, const char* name, int length, rtems_rfs_ino* ino, uint32_t* offset) { rtems_rfs_block_map map; rtems_rfs_buffer_handle entries; int rc; if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) { int c; printf ("rtems-rfs: dir-lookup-ino: lookup ino: root=%" PRId32 ", path=", inode->ino); for (c = 0; c < length; c++) printf ("%c", name[c]); printf (", len=%d\n", length); } *ino = RTEMS_RFS_EMPTY_INO; *offset = 0; rc = rtems_rfs_block_map_open (fs, inode, &map); if (rc > 0) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: map open failed for ino %" PRIu32 ": %d: %s", rtems_rfs_inode_ino (inode), rc, strerror (rc)); return rc; } rc = rtems_rfs_buffer_handle_open (fs, &entries); if (rc > 0) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: handle open failed for ino %" PRIu32 ": %d: %s", rtems_rfs_inode_ino (inode), rc, strerror (rc)); rtems_rfs_block_map_close (fs, &map); return rc; } else { rtems_rfs_block_no block; uint32_t hash; /* * Calculate the hash of the look up string. */ hash = rtems_rfs_dir_hash (name, length); /* * Locate the first block. The map points to the start after open so just * seek 0. If an error the block will be 0. */ rc = rtems_rfs_block_map_seek (fs, &map, 0, &block); if (rc > 0) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: block map find failed: %d: %s\n", rc, strerror (rc)); if (rc == ENXIO) rc = ENOENT; rtems_rfs_buffer_handle_close (fs, &entries); rtems_rfs_block_map_close (fs, &map); return rc; } while ((rc == 0) && block) { uint8_t* entry; if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: block read, ino=%" PRIu32 " bno=%" PRId32 "\n", rtems_rfs_inode_ino (inode), map.bpos.bno); rc = rtems_rfs_buffer_handle_request (fs, &entries, block, true); if (rc > 0) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: block read, ino=%" PRIu32 " block=%" PRId32 ": %d: %s\n", rtems_rfs_inode_ino (inode), block, rc, strerror (rc)); break; } /* * Search the block to see if the name matches. A hash of 0xffff or 0x0 * means the entry is empty. */ entry = rtems_rfs_buffer_data (&entries); map.bpos.boff = 0; while (map.bpos.boff < (rtems_rfs_fs_block_size (fs) - RTEMS_RFS_DIR_ENTRY_SIZE)) { uint32_t ehash; int elength; ehash = rtems_rfs_dir_entry_hash (entry); elength = rtems_rfs_dir_entry_length (entry); *ino = rtems_rfs_dir_entry_ino (entry); if (elength == RTEMS_RFS_DIR_ENTRY_EMPTY) break; if (rtems_rfs_dir_entry_valid (fs, elength, *ino)) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: " "bad length or ino for ino %" PRIu32 ": %u/%" PRId32 " @ %04" PRIx32 "\n", rtems_rfs_inode_ino (inode), elength, *ino, map.bpos.boff); rc = EIO; break; } if (ehash == hash) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO_CHECK)) printf ("rtems-rfs: dir-lookup-ino: " "checking entry for ino %" PRId32 ": bno=%04" PRIx32 "/off=%04" PRIx32 " length:%d ino:%" PRId32 "\n", rtems_rfs_inode_ino (inode), map.bpos.bno, map.bpos.boff, elength, rtems_rfs_dir_entry_ino (entry)); if (memcmp (entry + RTEMS_RFS_DIR_ENTRY_SIZE, name, length) == 0) { *offset = rtems_rfs_block_map_pos (fs, &map); if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO_FOUND)) printf ("rtems-rfs: dir-lookup-ino: " "entry found in ino %" PRIu32 ", ino=%" PRIu32 " offset=%" PRIu32 "\n", rtems_rfs_inode_ino (inode), *ino, *offset); rtems_rfs_buffer_handle_close (fs, &entries); rtems_rfs_block_map_close (fs, &map); return 0; } } map.bpos.boff += elength; entry += elength; } if (rc == 0) { rc = rtems_rfs_block_map_next_block (fs, &map, &block); if ((rc > 0) && (rc != ENXIO)) { if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: " "block map next block failed in ino %" PRIu32 ": %d: %s\n", rtems_rfs_inode_ino (inode), rc, strerror (rc)); } if (rc == ENXIO) rc = ENOENT; } } if ((rc == 0) && (block == 0)) { rc = EIO; if (rtems_rfs_trace (RTEMS_RFS_TRACE_DIR_LOOKUP_INO)) printf ("rtems-rfs: dir-lookup-ino: block is 0 in ino %" PRIu32 ": %d: %s\n", rtems_rfs_inode_ino (inode), rc, strerror (rc)); } } rtems_rfs_buffer_handle_close (fs, &entries); rtems_rfs_block_map_close (fs, &map); return rc; }
static int rtems_rfs_shell_dir (rtems_rfs_file_system* fs, int argc, char *argv[]) { rtems_rfs_buffer_handle buffer; rtems_rfs_block_no block; uint8_t* data; bool state; int entry; int b; int rc; if (argc <= 1) { printf ("error: no block number provided\n"); return 1; } block = strtoul (argv[1], 0, 0); rtems_rfs_shell_lock_rfs (fs); rc = rtems_rfs_group_bitmap_test (fs, false, block, &state); if (rc > 0) { rtems_rfs_shell_unlock_rfs (fs); printf ("error: testing block state: block=%" PRIu32 ": (%d) %s\n", block, rc, strerror (rc)); return 1; } printf (" %5" PRIu32 ": block %s\n", block, state ? "allocated" : "free"); rc = rtems_rfs_buffer_handle_open (fs, &buffer); if (rc > 0) { rtems_rfs_shell_unlock_rfs (fs); printf ("error: opening buffer handle: block=%" PRIu32 ": (%d) %s\n", block, rc, strerror (rc)); return 1; } rc = rtems_rfs_buffer_handle_request (fs, &buffer, block, true); if (rc > 0) { rtems_rfs_buffer_handle_close (fs, &buffer); rtems_rfs_shell_unlock_rfs (fs); printf ("error: requesting buffer handle: block=%" PRIu32 ": (%d) %s\n", block, rc, strerror (rc)); return 1; } b = 0; entry = 1; data = rtems_rfs_buffer_data (&buffer); while (b < (rtems_rfs_fs_block_size (fs) - RTEMS_RFS_DIR_ENTRY_SIZE - 1)) { rtems_rfs_ino eino; int elength; int length; int c; eino = rtems_rfs_dir_entry_ino (data); elength = rtems_rfs_dir_entry_length (data); if (elength == RTEMS_RFS_DIR_ENTRY_EMPTY) break; if ((elength < RTEMS_RFS_DIR_ENTRY_SIZE) || (elength >= rtems_rfs_fs_max_name (fs))) { printf (" %5d: entry length appears corrupt: %d\n", entry, elength); break; } if ((eino < RTEMS_RFS_ROOT_INO) || (eino >= rtems_rfs_fs_inodes (fs))) { printf (" %5d: entry ino appears corrupt: ino=%" PRId32 "\n", entry, eino); break; } length = elength - RTEMS_RFS_DIR_ENTRY_SIZE; printf (" %5d: %04x inode=%-6" PRIu32 " hash=%08" PRIx32 " name[%03u]=", entry, b, rtems_rfs_dir_entry_ino (data), rtems_rfs_dir_entry_hash (data), length); if (length > 50) length = 50; for (c = 0; c < length; c++) printf ("%c", data[RTEMS_RFS_DIR_ENTRY_SIZE + c]); if (length < elength - RTEMS_RFS_DIR_ENTRY_SIZE) printf ("..."); printf ("\n"); b += elength; data += elength; entry++; } rc = rtems_rfs_buffer_handle_close (fs, &buffer); if (rc > 0) { rtems_rfs_shell_unlock_rfs (fs); printf ("error: closing buffer handle: block=%" PRIu32 ": (%d) %s\n", block, rc, strerror (rc)); return 1; } rtems_rfs_shell_unlock_rfs (fs); return 0; }