newdir.c 1.5 KB

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