blob: 949c4e52679807b2a6cedfbd1100c9ce9a261b13 [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"
Chris Masone089f052007-03-16 16:20:31 -04008#include "transaction.h"
Chris Mason62e27492007-03-15 12:56:47 -04009
Chris Masone089f052007-03-16 16:20:31 -040010int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
11 *root, char *name, int name_len, u64 dir, u64
12 objectid, u8 type)
Chris Mason62e27492007-03-15 12:56:47 -040013{
14 int ret = 0;
15 struct btrfs_path path;
16 struct btrfs_dir_item *dir_item;
17 char *name_ptr;
18 struct btrfs_key key;
19 u32 data_size;
20
21 key.objectid = dir;
22 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040023 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -040024 ret = btrfs_name_hash(name, name_len, &key.offset);
25 BUG_ON(ret);
26 btrfs_init_path(&path);
27 data_size = sizeof(*dir_item) + name_len;
Chris Masone089f052007-03-16 16:20:31 -040028 ret = btrfs_insert_empty_item(trans, root, &path, &key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -040029 if (ret)
30 goto out;
31
32 dir_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
33 struct btrfs_dir_item);
34 btrfs_set_dir_objectid(dir_item, objectid);
35 btrfs_set_dir_type(dir_item, type);
36 btrfs_set_dir_flags(dir_item, 0);
Chris Masona8a2ee02007-03-16 08:46:49 -040037 btrfs_set_dir_name_len(dir_item, name_len);
Chris Mason62e27492007-03-15 12:56:47 -040038 name_ptr = (char *)(dir_item + 1);
39 memcpy(name_ptr, name, name_len);
40out:
41 btrfs_release_path(root, &path);
42 return ret;
43}
44
Chris Masone089f052007-03-16 16:20:31 -040045int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
46 *root, struct btrfs_path *path, u64 dir, char *name,
47 int name_len, int mod)
Chris Mason62e27492007-03-15 12:56:47 -040048{
Chris Mason1d4f6402007-03-15 15:18:43 -040049 int ret;
Chris Mason62e27492007-03-15 12:56:47 -040050 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -040051 int ins_len = mod < 0 ? -1 : 0;
52 int cow = mod != 0;
Chris Mason62e27492007-03-15 12:56:47 -040053
54 key.objectid = dir;
55 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040056 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -040057 ret = btrfs_name_hash(name, name_len, &key.offset);
58 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -040059 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
Chris Mason62e27492007-03-15 12:56:47 -040060 return ret;
61}
62
Chris Masone089f052007-03-16 16:20:31 -040063int btrfs_match_dir_item_name(struct btrfs_root *root,
64 struct btrfs_path *path, char
65 *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -040066{
Chris Mason62e27492007-03-15 12:56:47 -040067 struct btrfs_dir_item *dir_item;
68 char *name_ptr;
Chris Masona8a2ee02007-03-16 08:46:49 -040069
Chris Mason1d4f6402007-03-15 15:18:43 -040070 dir_item = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
71 struct btrfs_dir_item);
Chris Masona8a2ee02007-03-16 08:46:49 -040072 if (btrfs_dir_name_len(dir_item) != name_len)
Chris Mason1d4f6402007-03-15 15:18:43 -040073 return 0;
Chris Masona8a2ee02007-03-16 08:46:49 -040074 name_ptr = (char *)(dir_item + 1);
75 if (memcmp(name_ptr, name, name_len))
76 return 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040077 return 1;
Chris Mason62e27492007-03-15 12:56:47 -040078}