blob: a42a67b99753a55146eb01f8fa14c248db736d9a [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);
Chris Masona8a2ee02007-03-16 08:46:49 -040035 btrfs_set_dir_name_len(dir_item, name_len);
Chris Mason62e27492007-03-15 12:56:47 -040036 name_ptr = (char *)(dir_item + 1);
37 memcpy(name_ptr, name, name_len);
38out:
39 btrfs_release_path(root, &path);
40 return ret;
41}
42
Chris Mason1d4f6402007-03-15 15:18:43 -040043int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
44 u64 dir, char *name, int name_len, int mod)
Chris Mason62e27492007-03-15 12:56:47 -040045{
Chris Mason1d4f6402007-03-15 15:18:43 -040046 int ret;
Chris Mason62e27492007-03-15 12:56:47 -040047 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -040048 int ins_len = mod < 0 ? -1 : 0;
49 int cow = mod != 0;
Chris Mason62e27492007-03-15 12:56:47 -040050
51 key.objectid = dir;
52 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040053 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -040054 ret = btrfs_name_hash(name, name_len, &key.offset);
55 BUG_ON(ret);
Chris Mason1d4f6402007-03-15 15:18:43 -040056 ret = btrfs_search_slot(root, &key, path, ins_len, cow);
Chris Mason62e27492007-03-15 12:56:47 -040057 return ret;
58}
59
Chris Mason1d4f6402007-03-15 15:18:43 -040060int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
61 char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -040062{
Chris Mason62e27492007-03-15 12:56:47 -040063 struct btrfs_dir_item *dir_item;
64 char *name_ptr;
Chris Masona8a2ee02007-03-16 08:46:49 -040065
Chris Mason1d4f6402007-03-15 15:18:43 -040066 dir_item = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
67 struct btrfs_dir_item);
Chris Masona8a2ee02007-03-16 08:46:49 -040068 if (btrfs_dir_name_len(dir_item) != name_len)
Chris Mason1d4f6402007-03-15 15:18:43 -040069 return 0;
Chris Masona8a2ee02007-03-16 08:46:49 -040070 name_ptr = (char *)(dir_item + 1);
71 if (memcmp(name_ptr, name, name_len))
72 return 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040073 return 1;
Chris Mason62e27492007-03-15 12:56:47 -040074}