sqfs_dir.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. int j, offset;
  34. switch (get_unaligned_le16(&base->inode_type)) {
  35. case SQFS_DIR_TYPE:
  36. dir = (struct squashfs_dir_inode *)base;
  37. start_block = get_unaligned_le32(&dir->start_block);
  38. offset = get_unaligned_le16(&dir->offset);
  39. break;
  40. case SQFS_LDIR_TYPE:
  41. ldir = (struct squashfs_ldir_inode *)base;
  42. start_block = get_unaligned_le32(&ldir->start_block);
  43. offset = get_unaligned_le16(&ldir->offset);
  44. break;
  45. default:
  46. printf("Error: this is not a directory.\n");
  47. return -EINVAL;
  48. }
  49. if (offset < 0)
  50. return -EINVAL;
  51. for (j = 0; j < m_count; j++) {
  52. if (m_list[j] == start_block)
  53. return (++j * SQFS_METADATA_BLOCK_SIZE) + offset;
  54. }
  55. if (start_block == 0)
  56. return offset;
  57. printf("Error: invalid inode reference to directory table.\n");
  58. return -EINVAL;
  59. }
  60. bool sqfs_is_empty_dir(void *dir_i)
  61. {
  62. struct squashfs_base_inode *base = dir_i;
  63. struct squashfs_ldir_inode *ldir;
  64. struct squashfs_dir_inode *dir;
  65. u32 file_size;
  66. switch (get_unaligned_le16(&base->inode_type)) {
  67. case SQFS_DIR_TYPE:
  68. dir = (struct squashfs_dir_inode *)base;
  69. file_size = get_unaligned_le16(&dir->file_size);
  70. break;
  71. case SQFS_LDIR_TYPE:
  72. ldir = (struct squashfs_ldir_inode *)base;
  73. file_size = get_unaligned_le16(&ldir->file_size);
  74. break;
  75. default:
  76. printf("Error: this is not a directory.\n");
  77. return false;
  78. }
  79. return file_size == SQFS_EMPTY_FILE_SIZE;
  80. }