newdir.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * newdir.c --- create a new directory block
  3. *
  4. * Copyright (C) 1994, 1995 Theodore Ts'o.
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include "ext2_fs.h"
  17. #include "ext2fs.h"
  18. #ifndef EXT2_FT_DIR
  19. #define EXT2_FT_DIR 2
  20. #endif
  21. /*
  22. * Create new directory block
  23. */
  24. errcode_t ext2fs_new_dir_block(ext2_filsys fs, ext2_ino_t dir_ino,
  25. ext2_ino_t parent_ino, char **block)
  26. {
  27. struct ext2_dir_entry *dir = NULL;
  28. errcode_t retval;
  29. char *buf;
  30. int rec_len;
  31. int filetype = 0;
  32. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  33. retval = ext2fs_get_mem(fs->blocksize, &buf);
  34. if (retval)
  35. return retval;
  36. memset(buf, 0, fs->blocksize);
  37. dir = (struct ext2_dir_entry *) buf;
  38. dir->rec_len = fs->blocksize;
  39. if (dir_ino) {
  40. if (fs->super->s_feature_incompat &
  41. EXT2_FEATURE_INCOMPAT_FILETYPE)
  42. filetype = EXT2_FT_DIR << 8;
  43. /*
  44. * Set up entry for '.'
  45. */
  46. dir->inode = dir_ino;
  47. dir->name_len = 1 | filetype;
  48. dir->name[0] = '.';
  49. rec_len = dir->rec_len - EXT2_DIR_REC_LEN(1);
  50. dir->rec_len = EXT2_DIR_REC_LEN(1);
  51. /*
  52. * Set up entry for '..'
  53. */
  54. dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
  55. dir->rec_len = rec_len;
  56. dir->inode = parent_ino;
  57. dir->name_len = 2 | filetype;
  58. dir->name[0] = '.';
  59. dir->name[1] = '.';
  60. }
  61. *block = buf;
  62. return 0;
  63. }