sqfs_dir.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <asm/byteorder.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "sqfs_filesystem.h"
  14. #include "sqfs_utils.h"
  15. bool sqfs_is_dir(u16 type)
  16. {
  17. return type == SQFS_DIR_TYPE || type == SQFS_LDIR_TYPE;
  18. }
  19. /*
  20. * Receives a pointer (void *) to a position in the inode table containing the
  21. * directory's inode. Returns directory inode offset into the directory table.
  22. * m_list contains each metadata block's position, and m_count is the number of
  23. * elements of m_list. Those metadata blocks come from the compressed directory
  24. * table.
  25. */
  26. int sqfs_dir_offset(void *dir_i, u32 *m_list, int m_count)
  27. {
  28. struct squashfs_base_inode *base = dir_i;
  29. struct squashfs_ldir_inode *ldir;
  30. struct squashfs_dir_inode *dir;
  31. u32 start_block;
  32. int j, offset;
  33. switch (get_unaligned_le16(&base->inode_type)) {
  34. case SQFS_DIR_TYPE:
  35. dir = (struct squashfs_dir_inode *)base;
  36. start_block = get_unaligned_le32(&dir->start_block);
  37. offset = get_unaligned_le16(&dir->offset);
  38. break;
  39. case SQFS_LDIR_TYPE:
  40. ldir = (struct squashfs_ldir_inode *)base;
  41. start_block = get_unaligned_le32(&ldir->start_block);
  42. offset = get_unaligned_le16(&ldir->offset);
  43. break;
  44. default:
  45. printf("Error: this is not a directory.\n");
  46. return -EINVAL;
  47. }
  48. if (offset < 0)
  49. return -EINVAL;
  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. }