Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include "kerncompat.h" |
| 4 | #include "radix-tree.h" |
| 5 | #include "ctree.h" |
| 6 | #include "disk-io.h" |
| 7 | #include "hash.h" |
| 8 | |
| 9 | int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len, |
| 10 | u64 dir, u64 objectid, u8 type) |
| 11 | { |
| 12 | int ret = 0; |
| 13 | struct btrfs_path path; |
| 14 | struct btrfs_dir_item *dir_item; |
| 15 | char *name_ptr; |
| 16 | struct btrfs_key key; |
| 17 | u32 data_size; |
| 18 | |
| 19 | key.objectid = dir; |
| 20 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 21 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 22 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 23 | BUG_ON(ret); |
| 24 | btrfs_init_path(&path); |
| 25 | data_size = sizeof(*dir_item) + name_len; |
| 26 | ret = btrfs_insert_empty_item(root, &path, &key, data_size); |
| 27 | if (ret) |
| 28 | goto out; |
| 29 | |
| 30 | dir_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], |
| 31 | struct btrfs_dir_item); |
| 32 | btrfs_set_dir_objectid(dir_item, objectid); |
| 33 | btrfs_set_dir_type(dir_item, type); |
| 34 | btrfs_set_dir_flags(dir_item, 0); |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame^] | 35 | btrfs_set_dir_name_len(dir_item, name_len); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 36 | name_ptr = (char *)(dir_item + 1); |
| 37 | memcpy(name_ptr, name, name_len); |
| 38 | out: |
| 39 | btrfs_release_path(root, &path); |
| 40 | return ret; |
| 41 | } |
| 42 | |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 43 | int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path, |
| 44 | u64 dir, char *name, int name_len, int mod) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 45 | { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 46 | int ret; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 47 | struct btrfs_key key; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 48 | int ins_len = mod < 0 ? -1 : 0; |
| 49 | int cow = mod != 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 50 | |
| 51 | key.objectid = dir; |
| 52 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 53 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 54 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 55 | BUG_ON(ret); |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 56 | ret = btrfs_search_slot(root, &key, path, ins_len, cow); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 57 | return ret; |
| 58 | } |
| 59 | |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 60 | int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path, |
| 61 | char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 62 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 63 | struct btrfs_dir_item *dir_item; |
| 64 | char *name_ptr; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame^] | 65 | |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 66 | dir_item = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0], |
| 67 | struct btrfs_dir_item); |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame^] | 68 | if (btrfs_dir_name_len(dir_item) != name_len) |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 69 | return 0; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame^] | 70 | name_ptr = (char *)(dir_item + 1); |
| 71 | if (memcmp(name_ptr, name, name_len)) |
| 72 | return 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 73 | return 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 74 | } |