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