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