3
0

mkdir.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * mkdir.c --- make a directory in the filesystem
  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 <fcntl.h>
  17. #include <time.h>
  18. #if HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #if HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #include "ext2_fs.h"
  25. #include "ext2fs.h"
  26. #ifndef EXT2_FT_DIR
  27. #define EXT2_FT_DIR 2
  28. #endif
  29. errcode_t ext2fs_mkdir(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t inum,
  30. const char *name)
  31. {
  32. errcode_t retval;
  33. struct ext2_inode parent_inode, inode;
  34. ext2_ino_t ino = inum;
  35. ext2_ino_t scratch_ino;
  36. blk_t blk;
  37. char *block = 0;
  38. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  39. /*
  40. * Allocate an inode, if necessary
  41. */
  42. if (!ino) {
  43. retval = ext2fs_new_inode(fs, parent, LINUX_S_IFDIR | 0755,
  44. 0, &ino);
  45. if (retval)
  46. goto cleanup;
  47. }
  48. /*
  49. * Allocate a data block for the directory
  50. */
  51. retval = ext2fs_new_block(fs, 0, 0, &blk);
  52. if (retval)
  53. goto cleanup;
  54. /*
  55. * Create a scratch template for the directory
  56. */
  57. retval = ext2fs_new_dir_block(fs, ino, parent, &block);
  58. if (retval)
  59. goto cleanup;
  60. /*
  61. * Get the parent's inode, if necessary
  62. */
  63. if (parent != ino) {
  64. retval = ext2fs_read_inode(fs, parent, &parent_inode);
  65. if (retval)
  66. goto cleanup;
  67. } else
  68. memset(&parent_inode, 0, sizeof(parent_inode));
  69. /*
  70. * Create the inode structure....
  71. */
  72. memset(&inode, 0, sizeof(struct ext2_inode));
  73. inode.i_mode = LINUX_S_IFDIR | (0777 & ~fs->umask);
  74. inode.i_uid = inode.i_gid = 0;
  75. inode.i_blocks = fs->blocksize / 512;
  76. inode.i_block[0] = blk;
  77. inode.i_links_count = 2;
  78. inode.i_ctime = inode.i_atime = inode.i_mtime = time(NULL);
  79. inode.i_size = fs->blocksize;
  80. /*
  81. * Write out the inode and inode data block
  82. */
  83. retval = ext2fs_write_dir_block(fs, blk, block);
  84. if (retval)
  85. goto cleanup;
  86. retval = ext2fs_write_new_inode(fs, ino, &inode);
  87. if (retval)
  88. goto cleanup;
  89. /*
  90. * Link the directory into the filesystem hierarchy
  91. */
  92. if (name) {
  93. retval = ext2fs_lookup(fs, parent, name, strlen(name), 0,
  94. &scratch_ino);
  95. if (!retval) {
  96. retval = EXT2_ET_DIR_EXISTS;
  97. name = 0;
  98. goto cleanup;
  99. }
  100. if (retval != EXT2_ET_FILE_NOT_FOUND)
  101. goto cleanup;
  102. retval = ext2fs_link(fs, parent, name, ino, EXT2_FT_DIR);
  103. if (retval)
  104. goto cleanup;
  105. }
  106. /*
  107. * Update parent inode's counts
  108. */
  109. if (parent != ino) {
  110. parent_inode.i_links_count++;
  111. retval = ext2fs_write_inode(fs, parent, &parent_inode);
  112. if (retval)
  113. goto cleanup;
  114. }
  115. /*
  116. * Update accounting....
  117. */
  118. ext2fs_block_alloc_stats(fs, blk, +1);
  119. ext2fs_inode_alloc_stats2(fs, ino, +1, 1);
  120. cleanup:
  121. if (block)
  122. ext2fs_free_mem(&block);
  123. return retval;
  124. }