xref: /haiku/headers/build/private/storage/storage_support.h (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //---------------------------------------------------------------------
5 /*!
6 	\file storage_support.h
7 	Interface declarations for miscellaneous internal
8 	Storage Kit support functions.
9 */
10 
11 #ifndef _STORAGE_SUPPORT_H
12 #define _STORAGE_SUPPORT_H
13 
14 #include <dirent.h>
15 #include <string>
16 
17 namespace BPrivate {
18 namespace Storage {
19 
20 // For convenience:
21 struct LongDirEntry : dirent { char _buffer[B_FILE_NAME_LENGTH]; };
22 
23 //! Returns whether the supplied path is absolute.
24 bool is_absolute_path(const char *path);
25 
26 status_t parse_path(const char *fullPath, int &dirEnd, int &leafStart,
27 	int &leafEnd);
28 status_t parse_path(const char *fullPath, char *dirPath, char *leaf);
29 
30 //!	splits a path name into directory path and leaf name
31 status_t split_path(const char *fullPath, char *&path, char *&leaf);
32 
33 //!	splits a path name into directory path and leaf name
34 status_t split_path(const char *fullPath, char **path, char **leaf);
35 
36 //! Parses the first component of a path name.
37 status_t parse_first_path_component(const char *path, int32& length,
38 									int32& nextComponent);
39 
40 //! Parses the first component of a path name.
41 status_t parse_first_path_component(const char *path, char *&component,
42 									int32& nextComponent);
43 
44 //! Checks whether an entry name is a valid entry name.
45 status_t check_entry_name(const char *entry);
46 
47 //! Checks whether a path name is a valid path name.
48 status_t check_path_name(const char *path);
49 
50 /*! \brief Returns a copy of \c str in which all alphabetic characters
51 	are lowercase.
52 
53 	Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer.
54 */
55 std::string to_lower(const char *str);
56 
57 /*! \brief Places a copy of \c str in \c result in which all alphabetic
58 	characters are lowercase.
59 
60 	Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer.
61 */
62 void to_lower(const char *str, std::string &result);
63 
64 /*! \brief Copies \c str into \c result, converting any uppercase alphabetics
65 	to lowercase.
66 
67 	\a str and \a result may point to the same string. \a result is
68 	assumed to be as long as or longer than \a str.
69 */
70 void to_lower(const char *str, char *result);
71 
72 //! Converts \c str to lowercase.
73 void to_lower(char *str);
74 
75 /*! \brief Escapes any whitespace or other special characters in the path
76 
77 	\a result must be large enough to accomodate the addition of
78 	escape sequences to \a str. \a str and \a result may *NOT* point to
79 	the same string.
80 
81 	Note that this function was designed for use with the registrar's
82 	RecentEntries class, and may not create escapes exactly like you're
83 	hoping.	Please double check the code for the function to see if this
84 	is the case.
85 */
86 void escape_path(const char *str, char *result);
87 
88 /*! \brief Escapes any whitespace or other special characters in the path
89 
90 	\a str must be large enough to accomodate the addition of
91 	escape sequences.
92 */
93 void escape_path(char *str);
94 
95 /*!	\brief Returns whether the supplied device ID refers to the root FS.
96 */
97 bool device_is_root_device(dev_t device);
98 
99 // FDCloser
100 class FDCloser {
101 public:
102 	FDCloser(int fd)
103 		: fFD(fd)
104 	{
105 	}
106 
107 	~FDCloser()
108 	{
109 		Close();
110 	}
111 
112 	void SetTo(int fd)
113 	{
114 		Close();
115 		fFD = fd;
116 	}
117 
118 // implemented in the source file to not expose syscalls to the unit tests
119 // which include this file too
120 	void Close();
121 //	void Close()
122 //	{
123 //		if (fFD >= 0)
124 //			_kern_close(fFD);
125 //		fFD = -1;
126 //	}
127 
128 	int Detach()
129 	{
130 		int fd = fFD;
131 		fFD = -1;
132 		return fd;
133 	}
134 
135 private:
136 	int	fFD;
137 };
138 
139 };	// namespace Storage
140 };	// namespace BPrivate
141 
142 using BPrivate::Storage::FDCloser;
143 
144 #endif	// _STORAGE_SUPPORT_H
145 
146 
147