xref: /haiku/headers/private/kernel/fs/KPath.h (revision a127b88ecbfab58f64944c98aa47722a18e363b2)
1 /*
2  * Copyright 2004-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2008-2017, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _K_PATH_H
7 #define _K_PATH_H
8 
9 
10 #include <KernelExport.h>
11 
12 
13 namespace BPrivate {
14 namespace DiskDevice {
15 
16 
17 class KPath {
18 public:
19 	enum {
20 		DEFAULT = 0,
21 		NORMALIZE = 0x01,
22 		TRAVERSE_LEAF_LINK = 0x02,
23 		LAZY_ALLOC = 0x04
24 	};
25 public:
26 								KPath(size_t bufferSize = B_PATH_NAME_LENGTH + 1);
27 								KPath(const char* path, int32 flags = DEFAULT,
28 									size_t bufferSize = B_PATH_NAME_LENGTH + 1);
29 								KPath(const KPath& other);
30 								~KPath();
31 
32 			status_t			SetTo(const char* path, int32 flags = DEFAULT,
33 									size_t bufferSize = B_PATH_NAME_LENGTH + 1);
34 			void				Adopt(KPath& other);
35 
36 			status_t			InitCheck() const;
37 
38 			status_t			SetPath(const char* path,
39 									int32 flags = DEFAULT);
40 			const char*			Path() const;
41 			size_t				Length() const
42 									{ return fPathLength; }
43 
44 			size_t				BufferSize() const
45 									{ return fBufferSize; }
46 			char*				LockBuffer(bool force = false);
47 			void				UnlockBuffer();
48 			char*				DetachBuffer();
49 
50 			const char*			Leaf() const;
51 			status_t			ReplaceLeaf(const char* newLeaf);
52 			bool				RemoveLeaf();
53 				// returns false, if nothing could be removed anymore
54 
55 			status_t			Append(const char* toAppend,
56 									bool isComponent = true);
57 
58 			status_t			Normalize(bool traverseLeafLink);
59 
60 			KPath&				operator=(const KPath& other);
61 			KPath&				operator=(const char* path);
62 
63 			bool				operator==(const KPath& other) const;
64 			bool				operator==(const char* path) const;
65 			bool				operator!=(const KPath& other) const;
66 			bool				operator!=(const char* path) const;
67 
68 private:
69 			status_t			_AllocateBuffer();
70 			void				_FreeBuffer();
71 			status_t			_Normalize(const char* path,
72 									bool traverseLeafLink);
73 			void				_ChopTrailingSlashes();
74 
75 private:
76 			char*				fBuffer;
77 			size_t				fBufferSize;
78 			size_t				fPathLength;
79 			bool				fLocked;
80 			bool				fLazy;
81 			bool				fFailed;
82 			bool				fIsNull;
83 };
84 
85 
86 } // namespace DiskDevice
87 } // namespace BPrivate
88 
89 
90 using BPrivate::DiskDevice::KPath;
91 
92 
93 #endif	/* _K_PATH_H */
94