diff --git a/include/tebako-memfs-table.h b/include/tebako-memfs-table.h index 3cc84457..96be0bf0 100644 --- a/include/tebako-memfs-table.h +++ b/include/tebako-memfs-table.h @@ -31,6 +31,11 @@ namespace tebako { +#ifndef _INO_T_DEFINED +typedef ino_t _ino_t; +#define _INO_T_DEFINED +#endif + typedef std::map> tebako_memfs_table; class sync_tebako_memfs_table { @@ -40,6 +45,16 @@ class sync_tebako_memfs_table { public: static sync_tebako_memfs_table& get_tebako_memfs_table(void); + static constexpr int inoBits = std::min(sizeof(_ino_t), sizeof(uint32_t)) * 8; + static int getFsIndex(_ino_t ino) { return (ino >> (inoBits - 3)) & 0x7; } + + static _ino_t getFsIno(_ino_t ino) { return ino & ((static_cast<_ino_t>(1) << (inoBits - 3)) - 1); } + + static _ino_t fsInoFromFsAndIno(int index, _ino_t ino) + { + return (static_cast<_ino_t>(index) << (inoBits - 3)) | getFsIno(ino); + } + bool check(uint32_t index); void clear(void); void erase(uint32_t index); @@ -73,7 +88,7 @@ template int inode_memfs_call(Functor&& fn, uint32_t inode, Args&&... args) { int ret = DWARFS_IO_ERROR; - uint32_t fs_index = (inode >> 29) & 0x7; // Higher three bits is memfs index + uint32_t fs_index = sync_tebako_memfs_table::getFsIndex(inode); auto fs = sync_tebako_memfs_table::get_tebako_memfs_table().get(fs_index); if (fs == nullptr) { diff --git a/src/tebako-memfs-table.cpp b/src/tebako-memfs-table.cpp index 6af1cd43..751b2c58 100644 --- a/src/tebako-memfs-table.cpp +++ b/src/tebako-memfs-table.cpp @@ -100,7 +100,7 @@ uint32_t sync_tebako_memfs_table::insert_auto(std::shared_ptr fs) if (index > 7) { // Only three bits to store memfs index return 0; } - fs->set_root_inode(index << 29); + fs->set_root_inode(sync_tebako_memfs_table::fsInoFromFsAndIno(index, 0)); p_memfs_table->emplace(index, fs); return index; } diff --git a/tests/tests-memfs-table.cpp b/tests/tests-memfs-table.cpp index a69d48dc..3595821e 100644 --- a/tests/tests-memfs-table.cpp +++ b/tests/tests-memfs-table.cpp @@ -32,6 +32,30 @@ struct tebako_dirent; #include + +#ifdef _WIN32 +#undef lseek +#undef close +#undef read +#undef pread + +#undef chdir +#undef mkdir +#undef rmdir +#undef unlink +#undef access +#undef fstat +#undef stat +#undef lstat +#undef getcwd +#undef opendir +#undef readdir +#undef telldir +#undef seekdir +#undef rewinddir +#undef closedir +#endif + #include #include @@ -124,4 +148,11 @@ TEST_F(MemfsTableTests, test_concurrent_access) EXPECT_TRUE(memfs_table.check(2)); } +TEST_F(MemfsTableTests, fs_ino_and_index) +{ + _ino_t t = sync_tebako_memfs_table::fsInoFromFsAndIno(0x1, 0x234); + EXPECT_EQ(sync_tebako_memfs_table::getFsIndex(t), 0x1); + EXPECT_EQ(sync_tebako_memfs_table::getFsIno(t), 0x234); +} + } // namespace tebako diff --git a/tests/tests-mount-dwarfs.cpp b/tests/tests-mount-dwarfs.cpp index 3f0acc73..7d9a2957 100644 --- a/tests/tests-mount-dwarfs.cpp +++ b/tests/tests-mount-dwarfs.cpp @@ -28,6 +28,30 @@ */ #include "tests.h" + +#ifdef _WIN32 +#undef lseek +#undef close +#undef read +#undef pread + +#undef chdir +#undef mkdir +#undef rmdir +#undef unlink +#undef access +#undef fstat +#undef stat +#undef lstat +#undef getcwd +#undef opendir +#undef readdir +#undef telldir +#undef seekdir +#undef rewinddir +#undef closedir +#endif + #include namespace tebako {