xref: /haiku/src/add-ons/kernel/file_systems/nfs4/FileInfo.h (revision 71452e98334eaac603bf542d159e24788a46bebb)
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 
22 struct 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 
37 class InodeNames;
38 
39 struct InodeName : public SinglyLinkedListLinkImpl<InodeName> {
40 				InodeName(InodeNames* parent, const char* name);
41 				~InodeName();
42 
43 	InodeNames*	fParent;
44 	const char*	fName;
45 };
46 
47 struct 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 
60 class FileSystem;
61 class 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).
66 struct 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 
82 struct 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 
91 inline
92 FileHandle::FileHandle()
93 	:
94 	fSize(0)
95 {
96 }
97 
98 
99 inline
100 FileHandle::FileHandle(const FileHandle& fh)
101 	:
102 	fSize(fh.fSize)
103 {
104 	memcpy(fData, fh.fData, fSize);
105 }
106 
107 
108 inline FileHandle&
109 FileHandle::operator=(const FileHandle& fh)
110 {
111 	fSize = fh.fSize;
112 	memcpy(fData, fh.fData, fSize);
113 	return *this;
114 }
115 
116 
117 inline bool
118 FileHandle::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 
126 inline bool
127 FileHandle::operator!=(const FileHandle& handle) const
128 {
129 	return !operator==(handle);
130 }
131 
132 
133 inline bool
134 FileHandle::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 
142 inline bool
143 FileHandle::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 
151 inline bool
152 FileSystemId::operator==(const FileSystemId& fsid) const
153 {
154 	return fMajor == fsid.fMajor && fMinor == fsid.fMinor;
155 }
156 
157 
158 inline bool
159 FileSystemId::operator!=(const FileSystemId& fsid) const
160 {
161 	return !operator==(fsid);
162 }
163 
164 
165 #endif	// FILEHINFO_H
166 
167