ISO9660FileSystem.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ISO9660FileSystem.h"
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/Endian.h>
  9. #include <AK/HashFunctions.h>
  10. #include <AK/NonnullRefPtr.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/RefPtr.h>
  13. #include <AK/StringHash.h>
  14. #include <AK/StringView.h>
  15. #include <Kernel/Debug.h>
  16. #include <Kernel/FileSystem/BlockBasedFileSystem.h>
  17. #include <Kernel/Forward.h>
  18. #include <Kernel/KBuffer.h>
  19. #include <Kernel/UnixTypes.h>
  20. #include <Kernel/UserOrKernelBuffer.h>
  21. namespace Kernel {
  22. // NOTE: According to the spec, logical blocks 0 to 15 are system use.
  23. constexpr u32 first_data_area_block = 16;
  24. constexpr u32 logical_sector_size = 2048;
  25. constexpr u32 max_cached_directory_entries = 128;
  26. struct DirectoryState {
  27. RefPtr<ISO9660FS::DirectoryEntry> entry;
  28. u32 offset { 0 };
  29. };
  30. class ISO9660DirectoryIterator {
  31. public:
  32. ISO9660DirectoryIterator(ISO9660FS& fs, ISO::DirectoryRecordHeader const& header)
  33. : m_fs(fs)
  34. , m_current_header(&header)
  35. {
  36. // FIXME: Panic or alternative method?
  37. (void)read_directory_contents();
  38. get_header();
  39. }
  40. ISO::DirectoryRecordHeader const* operator*() { return m_current_header; }
  41. // Recurses into subdirectories. May fail.
  42. KResultOr<bool> next()
  43. {
  44. if (done())
  45. return false;
  46. dbgln_if(ISO9660_VERY_DEBUG, "next(): Called");
  47. if (has_flag(m_current_header->file_flags, ISO::FileFlags::Directory)) {
  48. dbgln_if(ISO9660_VERY_DEBUG, "next(): Recursing");
  49. {
  50. bool result = m_directory_stack.try_append(move(m_current_directory));
  51. if (!result) {
  52. return ENOMEM;
  53. }
  54. }
  55. dbgln_if(ISO9660_VERY_DEBUG, "next(): Pushed into directory stack");
  56. {
  57. auto result = read_directory_contents();
  58. if (result.is_error()) {
  59. return result;
  60. }
  61. }
  62. dbgln_if(ISO9660_VERY_DEBUG, "next(): Read directory contents");
  63. m_current_directory.offset = 0;
  64. get_header();
  65. if (m_current_header->length == 0) {
  66. // We have found an empty directory, let's continue with the
  67. // next one.
  68. if (!go_up())
  69. return false;
  70. } else {
  71. // We cannot skip here, as this is the first record in this
  72. // extent.
  73. return true;
  74. }
  75. }
  76. return skip();
  77. }
  78. // Skips to the directory in the list, returns whether there was a next one.
  79. // No allocation here, cannot fail.
  80. bool skip()
  81. {
  82. VERIFY(m_current_directory.entry);
  83. if (m_current_directory.offset >= m_current_directory.entry->length) {
  84. dbgln_if(ISO9660_VERY_DEBUG, "skip(): Was at last item already");
  85. return false;
  86. }
  87. m_current_directory.offset += m_current_header->length;
  88. get_header();
  89. if (m_current_header->length == 0) {
  90. // According to ECMA 119, if a logical block contains directory
  91. // records, then the leftover bytes in the logical block are
  92. // all zeros. So if our directory header has a length of 0,
  93. // we're probably looking at padding.
  94. //
  95. // Of course, this doesn't mean we're done; it only means that there
  96. // are no more directory entries in *this* logical block. If we
  97. // have at least one more logical block of data length to go, we
  98. // need to snap to the next logical block, because directory records
  99. // cannot span multiple logical blocks.
  100. u32 remaining_bytes = m_current_directory.entry->length - m_current_directory.offset;
  101. if (remaining_bytes > m_fs.logical_block_size()) {
  102. m_current_directory.offset += remaining_bytes % m_fs.logical_block_size();
  103. get_header();
  104. dbgln_if(ISO9660_VERY_DEBUG, "skip(): Snapped to next logical block (succeeded)");
  105. return true;
  106. }
  107. dbgln_if(ISO9660_VERY_DEBUG, "skip(): Was at the last logical block, at padding now (offset {}, data length {})", m_current_directory.entry->length, m_current_directory.offset);
  108. return false;
  109. }
  110. dbgln_if(ISO9660_VERY_DEBUG, "skip(): Skipped to next item");
  111. return true;
  112. }
  113. bool go_up()
  114. {
  115. if (m_directory_stack.is_empty()) {
  116. dbgln_if(ISO9660_VERY_DEBUG, "go_up(): Empty directory stack");
  117. return false;
  118. }
  119. m_current_directory = m_directory_stack.take_last();
  120. get_header();
  121. dbgln_if(ISO9660_VERY_DEBUG, "go_up(): Went up a directory");
  122. return true;
  123. }
  124. bool done() const
  125. {
  126. VERIFY(m_current_directory.entry);
  127. auto result = m_directory_stack.is_empty() && m_current_directory.offset >= m_current_directory.entry->length;
  128. dbgln_if(ISO9660_VERY_DEBUG, "done(): {}", result);
  129. return result;
  130. }
  131. private:
  132. KResult read_directory_contents()
  133. {
  134. auto result = m_fs.directory_entry_for_record({}, m_current_header);
  135. if (result.is_error()) {
  136. return result.error();
  137. }
  138. m_current_directory.entry = result.value();
  139. return KSuccess;
  140. }
  141. void get_header()
  142. {
  143. VERIFY(m_current_directory.entry);
  144. if (!m_current_directory.entry->blocks)
  145. return;
  146. m_current_header = reinterpret_cast<ISO::DirectoryRecordHeader const*>(m_current_directory.entry->blocks->data() + m_current_directory.offset);
  147. }
  148. ISO9660FS& m_fs;
  149. DirectoryState m_current_directory;
  150. ISO::DirectoryRecordHeader const* m_current_header { nullptr };
  151. Vector<DirectoryState> m_directory_stack;
  152. };
  153. KResultOr<NonnullRefPtr<ISO9660FS>> ISO9660FS::try_create(FileDescription& description)
  154. {
  155. return adopt_nonnull_ref_or_enomem(new (nothrow) ISO9660FS(description));
  156. }
  157. ISO9660FS::ISO9660FS(FileDescription& description)
  158. : BlockBasedFileSystem(description)
  159. {
  160. set_block_size(logical_sector_size);
  161. m_logical_block_size = logical_sector_size;
  162. }
  163. ISO9660FS::~ISO9660FS()
  164. {
  165. }
  166. KResult ISO9660FS::initialize()
  167. {
  168. if (auto result = BlockBasedFileSystem::initialize(); result.is_error())
  169. return result;
  170. if (auto result = parse_volume_set(); result.is_error())
  171. return result;
  172. if (auto result = create_root_inode(); result.is_error())
  173. return result;
  174. return KSuccess;
  175. }
  176. Inode& ISO9660FS::root_inode()
  177. {
  178. VERIFY(!m_root_inode.is_null());
  179. return *m_root_inode;
  180. }
  181. unsigned ISO9660FS::total_block_count() const
  182. {
  183. return LittleEndian { m_primary_volume->volume_space_size.little };
  184. }
  185. unsigned ISO9660FS::total_inode_count() const
  186. {
  187. if (!m_cached_inode_count) {
  188. auto result = calculate_inode_count();
  189. if (result.is_error()) {
  190. // FIXME: This should be able to return a KResult.
  191. return 0;
  192. }
  193. }
  194. return m_cached_inode_count;
  195. }
  196. u8 ISO9660FS::internal_file_type_to_directory_entry_type(const DirectoryEntryView& entry) const
  197. {
  198. if (has_flag(static_cast<ISO::FileFlags>(entry.file_type), ISO::FileFlags::Directory)) {
  199. return DT_DIR;
  200. }
  201. return DT_REG;
  202. }
  203. KResult ISO9660FS::parse_volume_set()
  204. {
  205. VERIFY(!m_primary_volume);
  206. auto block = KBuffer::try_create_with_size(m_logical_block_size, Memory::Region::Access::Read | Memory::Region::Access::Write, "ISO9660FS: Temporary volume descriptor storage");
  207. if (!block) {
  208. return ENOMEM;
  209. }
  210. auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
  211. auto current_block_index = first_data_area_block;
  212. while (true) {
  213. bool result = raw_read(BlockIndex { current_block_index }, block_buffer);
  214. if (!result) {
  215. dbgln_if(ISO9660_DEBUG, "Failed to read volume descriptor from ISO file");
  216. return EIO;
  217. }
  218. auto header = reinterpret_cast<ISO::VolumeDescriptorHeader const*>(block->data());
  219. if (StringView { header->identifier, 5 } != "CD001") {
  220. dbgln_if(ISO9660_DEBUG, "Header magic at volume descriptor {} is not valid", current_block_index - first_data_area_block);
  221. return EIO;
  222. }
  223. switch (header->type) {
  224. case ISO::VolumeDescriptorType::PrimaryVolumeDescriptor: {
  225. auto primary_volume = reinterpret_cast<ISO::PrimaryVolumeDescriptor const*>(header);
  226. m_primary_volume = adopt_own_if_nonnull(new ISO::PrimaryVolumeDescriptor(*primary_volume));
  227. break;
  228. }
  229. case ISO::VolumeDescriptorType::BootRecord:
  230. case ISO::VolumeDescriptorType::SupplementaryOrEnhancedVolumeDescriptor:
  231. case ISO::VolumeDescriptorType::VolumePartitionDescriptor: {
  232. break;
  233. }
  234. case ISO::VolumeDescriptorType::VolumeDescriptorSetTerminator: {
  235. goto all_headers_read;
  236. }
  237. default:
  238. dbgln_if(ISO9660_DEBUG, "Unexpected volume descriptor type {} in volume set", static_cast<u8>(header->type));
  239. return EIO;
  240. }
  241. current_block_index++;
  242. }
  243. all_headers_read:
  244. if (!m_primary_volume) {
  245. dbgln_if(ISO9660_DEBUG, "Could not find primary volume");
  246. return EIO;
  247. }
  248. m_logical_block_size = LittleEndian { m_primary_volume->logical_block_size.little };
  249. return KSuccess;
  250. }
  251. KResult ISO9660FS::create_root_inode()
  252. {
  253. if (!m_primary_volume) {
  254. dbgln_if(ISO9660_DEBUG, "Primary volume doesn't exist, can't create root inode");
  255. return EIO;
  256. }
  257. auto maybe_inode = ISO9660Inode::try_create_from_directory_record(*this, m_primary_volume->root_directory_record_header, {});
  258. if (maybe_inode.is_error()) {
  259. return ENOMEM;
  260. }
  261. m_root_inode = maybe_inode.release_value();
  262. return KSuccess;
  263. }
  264. KResult ISO9660FS::calculate_inode_count() const
  265. {
  266. if (!m_primary_volume) {
  267. dbgln_if(ISO9660_DEBUG, "Primary volume doesn't exist, can't calculate inode count");
  268. return EIO;
  269. }
  270. size_t inode_count = 1;
  271. auto traversal_result = visit_directory_record(m_primary_volume->root_directory_record_header, [&](ISO::DirectoryRecordHeader const* header) {
  272. if (header == nullptr) {
  273. return RecursionDecision::Continue;
  274. }
  275. inode_count += 1;
  276. if (has_flag(header->file_flags, ISO::FileFlags::Directory)) {
  277. if (header->file_identifier_length == 1) {
  278. auto file_identifier = reinterpret_cast<u8 const*>(header + 1);
  279. if (file_identifier[0] == '\0' || file_identifier[0] == '\1') {
  280. return RecursionDecision::Continue;
  281. }
  282. }
  283. return RecursionDecision::Recurse;
  284. }
  285. return RecursionDecision::Continue;
  286. });
  287. if (traversal_result.is_error()) {
  288. dbgln_if(ISO9660_DEBUG, "Failed to traverse for caching inode count!");
  289. return traversal_result;
  290. }
  291. m_cached_inode_count = inode_count;
  292. return KSuccess;
  293. }
  294. KResult ISO9660FS::visit_directory_record(ISO::DirectoryRecordHeader const& record, Function<KResultOr<RecursionDecision>(ISO::DirectoryRecordHeader const*)> const& visitor) const
  295. {
  296. if (!has_flag(record.file_flags, ISO::FileFlags::Directory)) {
  297. return KSuccess;
  298. }
  299. ISO9660DirectoryIterator iterator { const_cast<ISO9660FS&>(*this), record };
  300. while (!iterator.done()) {
  301. auto maybe_result = visitor(*iterator);
  302. if (maybe_result.is_error()) {
  303. return maybe_result.error();
  304. }
  305. switch (maybe_result.value()) {
  306. case RecursionDecision::Recurse: {
  307. auto maybe_has_moved = iterator.next();
  308. if (maybe_has_moved.is_error()) {
  309. return maybe_has_moved.error();
  310. }
  311. if (!maybe_has_moved.value()) {
  312. // If next() hasn't moved then we have read through all the
  313. // directories, and can exit.
  314. return KSuccess;
  315. }
  316. continue;
  317. }
  318. case RecursionDecision::Continue: {
  319. while (!iterator.done()) {
  320. if (iterator.skip())
  321. break;
  322. if (!iterator.go_up())
  323. return KSuccess;
  324. }
  325. continue;
  326. }
  327. case RecursionDecision::Break:
  328. return KSuccess;
  329. }
  330. }
  331. return KSuccess;
  332. }
  333. KResultOr<NonnullRefPtr<ISO9660FS::DirectoryEntry>> ISO9660FS::directory_entry_for_record(Badge<ISO9660DirectoryIterator>, ISO::DirectoryRecordHeader const* record)
  334. {
  335. u32 extent_location = LittleEndian { record->extent_location.little };
  336. u32 data_length = LittleEndian { record->data_length.little };
  337. auto key = calculate_directory_entry_cache_key(*record);
  338. auto it = m_directory_entry_cache.find(key);
  339. if (it != m_directory_entry_cache.end()) {
  340. dbgln_if(ISO9660_DEBUG, "Cache hit for dirent @ {}", extent_location);
  341. return it->value;
  342. }
  343. dbgln_if(ISO9660_DEBUG, "Cache miss for dirent @ {} :^(", extent_location);
  344. if (m_directory_entry_cache.size() == max_cached_directory_entries) {
  345. // FIXME: A smarter algorithm would probably be nicer.
  346. m_directory_entry_cache.remove(m_directory_entry_cache.begin());
  347. }
  348. if (!(data_length % logical_block_size() == 0)) {
  349. dbgln_if(ISO9660_DEBUG, "Found a directory with non-logical block size aligned data length!");
  350. return EIO;
  351. }
  352. auto blocks = KBuffer::try_create_with_size(data_length, Memory::Region::Access::Read | Memory::Region::Access::Write, "ISO9660FS: Directory traversal buffer");
  353. if (!blocks) {
  354. return ENOMEM;
  355. }
  356. auto blocks_buffer = UserOrKernelBuffer::for_kernel_buffer(blocks->data());
  357. auto did_read = raw_read_blocks(BlockBasedFileSystem::BlockIndex { extent_location }, data_length / logical_block_size(), blocks_buffer);
  358. if (!did_read) {
  359. return EIO;
  360. }
  361. auto maybe_entry = DirectoryEntry::try_create(extent_location, data_length, move(blocks));
  362. if (maybe_entry.is_error()) {
  363. return maybe_entry.error();
  364. }
  365. m_directory_entry_cache.set(key, maybe_entry.value());
  366. dbgln_if(ISO9660_DEBUG, "Cached dirent @ {}", extent_location);
  367. return maybe_entry.release_value();
  368. }
  369. u32 ISO9660FS::calculate_directory_entry_cache_key(ISO::DirectoryRecordHeader const& record)
  370. {
  371. return LittleEndian { record.extent_location.little };
  372. }
  373. KResultOr<size_t> ISO9660Inode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, FileDescription*) const
  374. {
  375. MutexLocker inode_locker(m_inode_lock);
  376. u32 data_length = LittleEndian { m_record.data_length.little };
  377. u32 extent_location = LittleEndian { m_record.extent_location.little };
  378. if (static_cast<u64>(offset) >= data_length)
  379. return 0;
  380. auto block = KBuffer::try_create_with_size(fs().m_logical_block_size);
  381. if (!block) {
  382. return ENOMEM;
  383. }
  384. auto block_buffer = UserOrKernelBuffer::for_kernel_buffer(block->data());
  385. size_t total_bytes = min(size, data_length - offset);
  386. size_t nread = 0;
  387. size_t blocks_already_read = offset / fs().m_logical_block_size;
  388. size_t initial_offset = offset % fs().m_logical_block_size;
  389. auto current_block_index = BlockBasedFileSystem::BlockIndex { extent_location + blocks_already_read };
  390. while (nread != total_bytes) {
  391. size_t bytes_to_read = min(total_bytes - nread, fs().logical_block_size() - initial_offset);
  392. auto buffer_offset = buffer.offset(nread);
  393. dbgln_if(ISO9660_VERY_DEBUG, "ISO9660Inode::read_bytes: Reading {} bytes into buffer offset {}/{}, logical block index: {}", bytes_to_read, nread, total_bytes, current_block_index.value());
  394. if (bool result = const_cast<ISO9660FS&>(fs()).raw_read(current_block_index, block_buffer); !result) {
  395. return EIO;
  396. }
  397. bool result = buffer_offset.write(block->data() + initial_offset, bytes_to_read);
  398. if (!result) {
  399. return EFAULT;
  400. }
  401. nread += bytes_to_read;
  402. initial_offset = 0;
  403. current_block_index = BlockBasedFileSystem::BlockIndex { current_block_index.value() + 1 };
  404. }
  405. return nread;
  406. }
  407. InodeMetadata ISO9660Inode::metadata() const
  408. {
  409. return m_metadata;
  410. }
  411. KResult ISO9660Inode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> visitor) const
  412. {
  413. Array<u8, max_file_identifier_length> file_identifier_buffer;
  414. auto traversal_result = fs().visit_directory_record(m_record, [&](ISO::DirectoryRecordHeader const* record) {
  415. StringView filename = get_normalized_filename(*record, file_identifier_buffer);
  416. dbgln_if(ISO9660_VERY_DEBUG, "traverse_as_directory(): Found {}", filename);
  417. InodeIdentifier id { fsid(), get_inode_index(*record, filename) };
  418. auto entry = FileSystem::DirectoryEntryView(filename, id, static_cast<u8>(record->file_flags));
  419. if (!visitor(entry)) {
  420. return RecursionDecision::Break;
  421. }
  422. return RecursionDecision::Continue;
  423. });
  424. if (traversal_result.is_error())
  425. return traversal_result;
  426. return KSuccess;
  427. }
  428. KResultOr<NonnullRefPtr<Inode>> ISO9660Inode::lookup(StringView name)
  429. {
  430. RefPtr<Inode> inode;
  431. Array<u8, max_file_identifier_length> file_identifier_buffer;
  432. auto traversal_result = fs().visit_directory_record(m_record, [&](ISO::DirectoryRecordHeader const* record) {
  433. StringView filename = get_normalized_filename(*record, file_identifier_buffer);
  434. if (filename == name) {
  435. auto maybe_inode = ISO9660Inode::try_create_from_directory_record(fs(), *record, filename);
  436. if (maybe_inode.is_error()) {
  437. // FIXME: The Inode API does not handle allocation failures very
  438. // well... we can't return a KResultOr from here. It
  439. // would be nice if we could return a KResult(Or) from
  440. // any place where allocation may happen.
  441. dbgln("Could not allocate inode for lookup!");
  442. } else {
  443. inode = maybe_inode.release_value();
  444. }
  445. return RecursionDecision::Break;
  446. }
  447. return RecursionDecision::Continue;
  448. });
  449. if (traversal_result.is_error())
  450. return traversal_result;
  451. if (!inode)
  452. return ENOENT;
  453. return inode.release_nonnull();
  454. }
  455. void ISO9660Inode::flush_metadata()
  456. {
  457. }
  458. KResultOr<size_t> ISO9660Inode::write_bytes(off_t, size_t, const UserOrKernelBuffer&, FileDescription*)
  459. {
  460. return EROFS;
  461. }
  462. KResultOr<NonnullRefPtr<Inode>> ISO9660Inode::create_child(StringView, mode_t, dev_t, UserID, GroupID)
  463. {
  464. return EROFS;
  465. }
  466. KResult ISO9660Inode::add_child(Inode&, const StringView&, mode_t)
  467. {
  468. return EROFS;
  469. }
  470. KResult ISO9660Inode::remove_child(const StringView&)
  471. {
  472. return EROFS;
  473. }
  474. KResult ISO9660Inode::chmod(mode_t)
  475. {
  476. return EROFS;
  477. }
  478. KResult ISO9660Inode::chown(UserID, GroupID)
  479. {
  480. return EROFS;
  481. }
  482. KResult ISO9660Inode::truncate(u64)
  483. {
  484. return EROFS;
  485. }
  486. KResult ISO9660Inode::set_atime(time_t)
  487. {
  488. return EROFS;
  489. }
  490. KResult ISO9660Inode::set_ctime(time_t)
  491. {
  492. return EROFS;
  493. }
  494. KResult ISO9660Inode::set_mtime(time_t)
  495. {
  496. return EROFS;
  497. }
  498. void ISO9660Inode::one_ref_left()
  499. {
  500. }
  501. ISO9660Inode::ISO9660Inode(ISO9660FS& fs, ISO::DirectoryRecordHeader const& record, StringView const& name)
  502. : Inode(fs, get_inode_index(record, name))
  503. , m_record(record)
  504. {
  505. dbgln_if(ISO9660_VERY_DEBUG, "Creating inode #{}", index());
  506. create_metadata();
  507. }
  508. ISO9660Inode::~ISO9660Inode()
  509. {
  510. }
  511. KResultOr<NonnullRefPtr<ISO9660Inode>> ISO9660Inode::try_create_from_directory_record(ISO9660FS& fs, ISO::DirectoryRecordHeader const& record, StringView const& name)
  512. {
  513. return adopt_nonnull_ref_or_enomem(new (nothrow) ISO9660Inode(fs, record, name));
  514. }
  515. void ISO9660Inode::create_metadata()
  516. {
  517. u32 data_length = LittleEndian { m_record.data_length.little };
  518. bool is_directory = has_flag(m_record.file_flags, ISO::FileFlags::Directory);
  519. time_t recorded_at = parse_numerical_date_time(m_record.recording_date_and_time);
  520. m_metadata = {
  521. .inode = identifier(),
  522. .size = data_length,
  523. .mode = static_cast<mode_t>((is_directory ? S_IFDIR : S_IFREG) | (is_directory ? 0555 : 0444)),
  524. .uid = 0,
  525. .gid = 0,
  526. .link_count = 1,
  527. .atime = recorded_at,
  528. .ctime = recorded_at,
  529. .mtime = recorded_at,
  530. .dtime = 0,
  531. .block_count = 0,
  532. .block_size = 0,
  533. .major_device = 0,
  534. .minor_device = 0,
  535. };
  536. }
  537. time_t ISO9660Inode::parse_numerical_date_time(ISO::NumericalDateAndTime const& date)
  538. {
  539. i32 year_offset = date.years_since_1900 - 70;
  540. return (year_offset * 60 * 60 * 24 * 30 * 12)
  541. + (date.month * 60 * 60 * 24 * 30)
  542. + (date.day * 60 * 60 * 24)
  543. + (date.hour * 60 * 60)
  544. + (date.minute * 60)
  545. + date.second;
  546. }
  547. StringView ISO9660Inode::get_normalized_filename(ISO::DirectoryRecordHeader const& record, Bytes buffer)
  548. {
  549. auto file_identifier = reinterpret_cast<u8 const*>(&record + 1);
  550. auto filename = StringView { file_identifier, record.file_identifier_length };
  551. if (filename.length() == 1) {
  552. if (filename[0] == '\0') {
  553. filename = "."sv;
  554. }
  555. if (filename[0] == '\1') {
  556. filename = ".."sv;
  557. }
  558. }
  559. if (!has_flag(record.file_flags, ISO::FileFlags::Directory)) {
  560. // FIXME: We currently strip the file version from the filename,
  561. // but that may be used later down the line if the file actually
  562. // has multiple versions on the disk.
  563. Optional<size_t> semicolon = filename.find(';');
  564. if (semicolon.has_value()) {
  565. filename = filename.substring_view(0, semicolon.value());
  566. }
  567. if (filename[filename.length() - 1] == '.') {
  568. filename = filename.substring_view(0, filename.length() - 1);
  569. }
  570. }
  571. if (filename.length() > buffer.size()) {
  572. // FIXME: Rock Ridge allows filenames up to 255 characters, so we should
  573. // probably support that instead of truncating.
  574. filename = filename.substring_view(0, buffer.size());
  575. }
  576. for (size_t i = 0; i < filename.length(); i++) {
  577. buffer[i] = to_ascii_lowercase(filename[i]);
  578. }
  579. return { buffer.data(), filename.length() };
  580. }
  581. InodeIndex ISO9660Inode::get_inode_index(ISO::DirectoryRecordHeader const& record, StringView const& name)
  582. {
  583. if (name.is_null()) {
  584. // NOTE: This is the index of the root inode.
  585. return 1;
  586. }
  587. return { pair_int_hash(LittleEndian { record.extent_location.little }, string_hash(name.characters_without_null_termination(), name.length())) };
  588. }
  589. }