1/*
2 * Copyright 2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Pawe�� Dziepak, pdziepak@quarnos.org
7 */
8
9
10#include "InodeIdMap.h"
11
12
13status_t
14InodeIdMap::AddName(FileInfo& fileInfo, InodeNames* parent,
15	const char* name, ino_t id)
16{
17	MutexLocker _(fLock);
18	AVLTreeMap<ino_t, FileInfo>::Iterator iterator = fMap.Find(id);
19	if (iterator.HasCurrent()) {
20		if (fileInfo.fHandle == iterator.Current().fHandle) {
21			return iterator.CurrentValuePointer()->fNames->AddName(parent,
22				name);
23		}
24	}
25
26	fMap.Remove(id);
27	fileInfo.fNames = new InodeNames;
28	if (fileInfo.fNames == NULL)
29		return B_NO_MEMORY;
30
31	fileInfo.fNames->fHandle = fileInfo.fHandle;
32	status_t result = fileInfo.fNames->AddName(parent, name);
33	if (result != B_OK) {
34		delete fileInfo.fNames;
35		return result;
36	}
37
38	return fMap.Insert(id, fileInfo);
39}
40
41
42bool
43InodeIdMap::RemoveName(ino_t id, InodeNames* parent, const char* name)
44{
45	ASSERT(name != NULL);
46
47	MutexLocker _(fLock);
48	AVLTreeMap<ino_t, FileInfo>::Iterator iterator = fMap.Find(id);
49	if (!iterator.HasCurrent())
50		return true;
51
52	FileInfo* fileInfo = iterator.CurrentValuePointer();
53
54	return fileInfo->fNames->RemoveName(parent, name);
55}
56
57
58status_t
59InodeIdMap::RemoveEntry(ino_t id)
60{
61	MutexLocker _(fLock);
62	return fMap.Remove(id);
63}
64
65
66status_t
67InodeIdMap::GetFileInfo(FileInfo* fileInfo, ino_t id)
68{
69	ASSERT(fileInfo != NULL);
70
71	MutexLocker _(fLock);
72	AVLTreeMap<ino_t, FileInfo>::Iterator iterator = fMap.Find(id);
73	if (!iterator.HasCurrent())
74		return B_ENTRY_NOT_FOUND;
75
76	*fileInfo = iterator.Current();
77	if (fileInfo->fNames->fNames.IsEmpty())
78		return B_ENTRY_NOT_FOUND;
79	return B_OK;
80}
81
82