sqfs_dir.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Bootlin
  4. *
  5. * Author: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
  6. */
  7. #include <errno.h>
  8. #include <linux/types.h>
  9. #include <linux/byteorder/little_endian.h>
  10. #include <linux/byteorder/generic.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "sqfs_filesystem.h"
  15. #include "sqfs_utils.h"
  16. bool sqfs_is_dir(u16 type)
  17. {
  18. return type == SQFS_DIR_TYPE || type == SQFS_LDIR_TYPE;
  19. }
  20. /*
  21. * Receives a pointer (void *) to a position in the inode table containing the
  22. * directory's inode. Returns directory inode offset into the directory table.
  23. * m_list contains each metadata block's position, and m_count is the number of
  24. * elements of m_list. Those metadata blocks come from the compressed directory
  25. * table.
  26. */
  27. int sqfs_dir_offset(void *dir_i, u32 *m_list, int m_count)
  28. {
  29. struct squashfs_base_inode *base = dir_i;
  30. struct squashfs_ldir_inode *ldir;
  31. struct squashfs_dir_inode *dir;
  32. u32 start_block;
  33. u16 offset;
  34. int j;
  35. switch (get_unaligned_le16(&base->inode_type)) {
  36. case SQFS_DIR_TYPE:
  37. dir = (struct squashfs_dir_inode *)base;
  38. start_block = get_unaligned_le32(&dir->start_block);
  39. offset = get_unaligned_le16(&dir->offset);
  40. break;
  41. case SQFS_LDIR_TYPE:
  42. ldir = (struct squashfs_ldir_inode *)base;
  43. start_block = get_unaligned_le32(&ldir->start_block);
  44. offset = get_unaligned_le16(&ldir->offset);
  45. break;
  46. default:
  47. printf("Error: this is not a directory.\n");
  48. return -EINVAL;
  49. }
  50. for (j = 0; j < m_count; j++) {
  51. if (m_list[j] == start_block)
  52. return (++j * SQFS_METADATA_BLOCK_SIZE) + offset;
  53. }
  54. if (start_block == 0)
  55. return offset;
  56. printf("Error: invalid inode reference to directory table.\n");
  57. return -EINVAL;
  58. }
  59. bool sqfs_is_empty_dir(void *dir_i)
  60. {
  61. struct squashfs_base_inode *base = dir_i;
  62. struct squashfs_ldir_inode *ldir;
  63. struct squashfs_dir_inode *dir;
  64. u32 file_size;
  65. switch (get_unaligned_le16(&base->inode_type)) {
  66. case SQFS_DIR_TYPE:
  67. dir = (struct squashfs_dir_inode *)base;
  68. file_size = get_unaligned_le16(&dir->file_size);
  69. break;
  70. case SQFS_LDIR_TYPE:
  71. ldir = (struct squashfs_ldir_inode *)base;
  72. file_size = get_unaligned_le16(&ldir->file_size);
  73. break;
  74. default:
  75. printf("Error: this is not a directory.\n");
  76. return false;
  77. }
  78. return file_size == SQFS_EMPTY_FILE_SIZE;
  79. }