Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * newdir.c --- create a new directory block |
| 3 | * |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 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% |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 14 | #if HAVE_UNISTD_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 16 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 17 | |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 18 | #if EXT2_FLAT_INCLUDES |
| 19 | #include "ext2_fs.h" |
| 20 | #else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include <linux/ext2_fs.h> |
Theodore Ts'o | b5abe6f | 1998-01-19 14:47:53 +0000 | [diff] [blame] | 22 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | |
| 24 | #include "ext2fs.h" |
| 25 | |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame^] | 26 | #ifndef EXT2_FT_DIR |
| 27 | #define EXT2_FT_DIR 2 |
| 28 | #endif |
| 29 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 30 | /* |
| 31 | * Create new directory block |
| 32 | */ |
| 33 | errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino, ino_t parent_ino, |
| 34 | char **block) |
| 35 | { |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 36 | struct ext2_dir_entry *dir = NULL; |
| 37 | errcode_t retval; |
| 38 | char *buf; |
| 39 | int rec_len; |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame^] | 40 | int filetype = 0; |
| 41 | struct ext2fs_sb *sb; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 43 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 44 | |
Theodore Ts'o | 7b4e453 | 1997-10-26 03:41:24 +0000 | [diff] [blame] | 45 | retval = ext2fs_get_mem(fs->blocksize, (void **) &buf); |
| 46 | if (retval) |
| 47 | return retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 48 | memset(buf, 0, fs->blocksize); |
| 49 | dir = (struct ext2_dir_entry *) buf; |
| 50 | dir->rec_len = fs->blocksize; |
| 51 | |
| 52 | if (dir_ino) { |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame^] | 53 | sb = (struct ext2fs_sb *) fs->super; |
| 54 | if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE) |
| 55 | filetype = EXT2_FT_DIR << 8; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 56 | /* |
| 57 | * Set up entry for '.' |
| 58 | */ |
| 59 | dir->inode = dir_ino; |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame^] | 60 | dir->name_len = 1 | filetype; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 61 | dir->name[0] = '.'; |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame^] | 62 | rec_len = dir->rec_len - EXT2_DIR_REC_LEN(1); |
| 63 | dir->rec_len = EXT2_DIR_REC_LEN(1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Set up entry for '..' |
| 67 | */ |
| 68 | dir = (struct ext2_dir_entry *) (buf + dir->rec_len); |
| 69 | dir->rec_len = rec_len; |
| 70 | dir->inode = parent_ino; |
Theodore Ts'o | e6198e5 | 1999-10-23 00:58:54 +0000 | [diff] [blame^] | 71 | dir->name_len = 2 | filetype; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 72 | dir->name[0] = '.'; |
| 73 | dir->name[1] = '.'; |
| 74 | |
| 75 | } |
| 76 | *block = buf; |
| 77 | return 0; |
| 78 | } |