1/*
2 * Copyright 2012 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#ifndef FILEINFO_H
9#define FILEINFO_H
10
11
12#include <stdlib.h>
13#include <string.h>
14
15#include <lock.h>
16#include <SupportDefs.h>
17#include <util/KernelReferenceable.h>
18
19
20#define NFS4_FHSIZE	128
21
22struct FileHandle {
23			uint8		fSize;
24			uint8		fData[NFS4_FHSIZE];
25
26	inline				FileHandle();
27	inline				FileHandle(const FileHandle& fh);
28	inline	FileHandle&	operator=(const FileHandle& fh);
29
30
31	inline	bool		operator==(const FileHandle& handle) const;
32	inline	bool		operator!=(const FileHandle& handle) const;
33	inline	bool		operator>(const FileHandle& handle) const;
34	inline	bool		operator<(const FileHandle& handle) const;
35};
36
37struct InodeNames;
38
39struct InodeName : public SinglyLinkedListLinkImpl<InodeName> {
40				InodeName(InodeNames* parent, const char* name);
41				~InodeName();
42
43	InodeNames*	fParent;
44	const char*	fName;
45};
46
47struct InodeNames : public KernelReferenceable {
48								InodeNames();
49								~InodeNames();
50
51	status_t					AddName(InodeNames* parent, const char* name);
52	bool						RemoveName(InodeNames* parent,
53									const char* name);
54
55	mutex						fLock;
56	SinglyLinkedList<InodeName>	fNames;
57	FileHandle					fHandle;
58};
59
60class FileSystem;
61class RequestBuilder;
62
63// Complete information needed to identify a file in any situation.
64// Unfortunately just a FileHandle is not enough even when they are persistent
65// since OPEN requires both parent FileHandle and file name (just like LOOKUP).
66struct FileInfo {
67			uint64		fFileId;
68			FileHandle	fHandle;
69
70			InodeNames*	fNames;
71
72			FileHandle	fAttrDir;
73
74						FileInfo();
75						~FileInfo();
76						FileInfo(const FileInfo& fi);
77			FileInfo&	operator=(const FileInfo& fi);
78
79			status_t	UpdateFileHandles(FileSystem* fs);
80};
81
82struct FileSystemId {
83			uint64		fMajor;
84			uint64		fMinor;
85
86	inline	bool		operator==(const FileSystemId& fsid) const;
87	inline	bool		operator!=(const FileSystemId& fsid) const;
88};
89
90
91inline
92FileHandle::FileHandle()
93	:
94	fSize(0)
95{
96}
97
98
99inline
100FileHandle::FileHandle(const FileHandle& fh)
101	:
102	fSize(fh.fSize)
103{
104	memcpy(fData, fh.fData, fSize);
105}
106
107
108inline FileHandle&
109FileHandle::operator=(const FileHandle& fh)
110{
111	fSize = fh.fSize;
112	memcpy(fData, fh.fData, fSize);
113	return *this;
114}
115
116
117inline bool
118FileHandle::operator==(const FileHandle& handle) const
119{
120	if (fSize != handle.fSize)
121		return false;
122	return memcmp(fData, handle.fData, fSize) == 0;
123}
124
125
126inline bool
127FileHandle::operator!=(const FileHandle& handle) const
128{
129	return !operator==(handle);
130}
131
132
133inline bool
134FileHandle::operator>(const FileHandle& handle) const
135{
136	if (fSize > handle.fSize)
137		return true;
138	return memcmp(fData, handle.fData, fSize) > 0;
139}
140
141
142inline bool
143FileHandle::operator<(const FileHandle& handle) const
144{
145	if (fSize < handle.fSize)
146		return true;
147	return memcmp(fData, handle.fData, fSize) < 0;
148}
149
150
151inline bool
152FileSystemId::operator==(const FileSystemId& fsid) const
153{
154	return fMajor == fsid.fMajor && fMinor == fsid.fMinor;
155}
156
157
158inline bool
159FileSystemId::operator!=(const FileSystemId& fsid) const
160{
161	return !operator==(fsid);
162}
163
164
165#endif	// FILEHINFO_H
166
167