| Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * newdir.c --- create a new directory block |
| 3 | * |
| 4 | * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/ext2_fs.h> |
| 14 | |
| 15 | #include "ext2fs.h" |
| 16 | |
| 17 | /* |
| 18 | * Create new directory block |
| 19 | */ |
| 20 | errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino, ino_t parent_ino, |
| 21 | char **block) |
| 22 | { |
| 23 | char *buf; |
| 24 | struct ext2_dir_entry *dir = NULL; |
| 25 | int rec_len; |
| 26 | |
| 27 | buf = malloc(fs->blocksize); |
| 28 | if (!buf) |
| 29 | return ENOMEM; |
| 30 | memset(buf, 0, fs->blocksize); |
| 31 | dir = (struct ext2_dir_entry *) buf; |
| 32 | dir->rec_len = fs->blocksize; |
| 33 | |
| 34 | if (dir_ino) { |
| 35 | /* |
| 36 | * Set up entry for '.' |
| 37 | */ |
| 38 | dir->inode = dir_ino; |
| 39 | dir->name_len = 1; |
| 40 | dir->name[0] = '.'; |
| 41 | rec_len = dir->rec_len - EXT2_DIR_REC_LEN(dir->name_len); |
| 42 | dir->rec_len = EXT2_DIR_REC_LEN(dir->name_len); |
| 43 | |
| 44 | /* |
| 45 | * Set up entry for '..' |
| 46 | */ |
| 47 | dir = (struct ext2_dir_entry *) (buf + dir->rec_len); |
| 48 | dir->rec_len = rec_len; |
| 49 | dir->inode = parent_ino; |
| 50 | dir->name_len = 2; |
| 51 | dir->name[0] = '.'; |
| 52 | dir->name[1] = '.'; |
| 53 | |
| 54 | } |
| 55 | *block = buf; |
| 56 | return 0; |
| 57 | } |