blob: 8043b2ef10d1b26b35e025baa700695028b0bc67 [file] [log] [blame]
Chris Mason62e27492007-03-15 12:56:47 -04001#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
9int 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 Mason1d4f6402007-03-15 15:18:43 -040021 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -040022 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);
35 name_ptr = (char *)(dir_item + 1);
36 memcpy(name_ptr, name, name_len);
37out:
38 btrfs_release_path(root, &path);
39 return ret;
40}
41
Chris Mason1d4f6402007-03-15 15:18:43 -040042int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
43 u64 dir, char *name, int name_len, int mod)
Chris Mason62e27492007-03-15 12:56:47 -040044{
Chris Mason1d4f6402007-03-15 15:18:43 -040045 int ret;
Chris Mason62e27492007-03-15 12:56:47 -040046 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -040047 int ins_len = mod < 0 ? -1 : 0;
48 int cow = mod != 0;
Chris Mason62e27492007-03-15 12:56:47 -040049
50 key.objectid = dir;
51 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040052 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -040053 ret = btrfs_name_hash(name, name_len, &key.offset);
54 BUG_ON(ret);
Chris Mason1d4f6402007-03-15 15:18:43 -040055 ret = btrfs_search_slot(root, &key, path, ins_len, cow);
Chris Mason62e27492007-03-15 12:56:47 -040056 return ret;
57}
58
Chris Mason1d4f6402007-03-15 15:18:43 -040059int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
60 char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -040061{
Chris Mason1d4f6402007-03-15 15:18:43 -040062 struct btrfs_item *item;
Chris Mason62e27492007-03-15 12:56:47 -040063 struct btrfs_dir_item *dir_item;
64 char *name_ptr;
Chris Mason62e27492007-03-15 12:56:47 -040065 u32 item_len;
Chris Mason1d4f6402007-03-15 15:18:43 -040066 item = path->nodes[0]->leaf.items + path->slots[0];
Chris Mason62e27492007-03-15 12:56:47 -040067 item_len = btrfs_item_size(item);
68 if (item_len != name_len + sizeof(struct btrfs_dir_item)) {
Chris Mason1d4f6402007-03-15 15:18:43 -040069 return 0;
Chris Mason62e27492007-03-15 12:56:47 -040070 }
Chris Mason1d4f6402007-03-15 15:18:43 -040071 dir_item = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
72 struct btrfs_dir_item);
Chris Mason62e27492007-03-15 12:56:47 -040073 name_ptr = (char *)(dir_item + 1);
74 if (memcmp(name_ptr, name, name_len)) {
Chris Mason1d4f6402007-03-15 15:18:43 -040075 return 0;
Chris Mason62e27492007-03-15 12:56:47 -040076 }
Chris Mason1d4f6402007-03-15 15:18:43 -040077 return 1;
Chris Mason62e27492007-03-15 12:56:47 -040078}