xref: /haiku/headers/private/storage/storage_support.h (revision 268f99dd7dc4bd7474a8bd2742d3f1ec1de6752a)
13e54c13aSAxel Dörfler /*
23e54c13aSAxel Dörfler  * Copyright 2002-2006, Haiku Inc.
33e54c13aSAxel Dörfler  * Distributed under the terms of the MIT License.
43e54c13aSAxel Dörfler  *
53e54c13aSAxel Dörfler  * Authors:
63e54c13aSAxel Dörfler  *		Tyler Dauwalder
73e54c13aSAxel Dörfler  */
83e54c13aSAxel Dörfler #ifndef _STORAGE_SUPPORT_H
93e54c13aSAxel Dörfler #define _STORAGE_SUPPORT_H
103e54c13aSAxel Dörfler 
1152a38012Sejakowatz /*!
1252a38012Sejakowatz 	\file storage_support.h
1352a38012Sejakowatz 	Interface declarations for miscellaneous internal
1452a38012Sejakowatz 	Storage Kit support functions.
1552a38012Sejakowatz */
1652a38012Sejakowatz 
173e54c13aSAxel Dörfler #include <StorageDefs.h>
183e54c13aSAxel Dörfler #include <SupportDefs.h>
1952a38012Sejakowatz 
20db10640dSIngo Weinhold #include <dirent.h>
211c4b4100STyler Dauwalder #include <string>
2252a38012Sejakowatz 
233e54c13aSAxel Dörfler 
2409d84e61STyler Dauwalder namespace BPrivate {
2509d84e61STyler Dauwalder namespace Storage {
2652a38012Sejakowatz 
27db10640dSIngo Weinhold // For convenience:
28*2532a287SAugustin Cavalier struct LongDirEntry {
29*2532a287SAugustin Cavalier 	char _[sizeof(struct dirent) + B_FILE_NAME_LENGTH + 1];
direntLongDirEntry30*2532a287SAugustin Cavalier 	struct dirent* dirent() { return (struct dirent*)_; }
318f03af00SAugustin Cavalier };
32db10640dSIngo Weinhold 
3352a38012Sejakowatz //! Returns whether the supplied path is absolute.
3452a38012Sejakowatz bool is_absolute_path(const char *path);
3552a38012Sejakowatz 
36db10640dSIngo Weinhold status_t parse_path(const char *fullPath, int &dirEnd, int &leafStart,
37db10640dSIngo Weinhold 	int &leafEnd);
38db10640dSIngo Weinhold status_t parse_path(const char *fullPath, char *dirPath, char *leaf);
39db10640dSIngo Weinhold 
4052a38012Sejakowatz //!	splits a path name into directory path and leaf name
4152a38012Sejakowatz status_t split_path(const char *fullPath, char *&path, char *&leaf);
4252a38012Sejakowatz 
4352a38012Sejakowatz //!	splits a path name into directory path and leaf name
4452a38012Sejakowatz status_t split_path(const char *fullPath, char **path, char **leaf);
4552a38012Sejakowatz 
4652a38012Sejakowatz //! Parses the first component of a path name.
4752a38012Sejakowatz status_t parse_first_path_component(const char *path, int32& length,
4852a38012Sejakowatz 									int32& nextComponent);
4952a38012Sejakowatz 
5052a38012Sejakowatz //! Parses the first component of a path name.
5152a38012Sejakowatz status_t parse_first_path_component(const char *path, char *&component,
5252a38012Sejakowatz 									int32& nextComponent);
5352a38012Sejakowatz 
5452a38012Sejakowatz //! Checks whether an entry name is a valid entry name.
5552a38012Sejakowatz status_t check_entry_name(const char *entry);
5652a38012Sejakowatz 
5752a38012Sejakowatz //! Checks whether a path name is a valid path name.
5852a38012Sejakowatz status_t check_path_name(const char *path);
5952a38012Sejakowatz 
60e5f9569dSTyler Dauwalder /*! \brief Returns a copy of \c str in which all alphabetic characters
61e5f9569dSTyler Dauwalder 	are lowercase.
62e5f9569dSTyler Dauwalder 
63e5f9569dSTyler Dauwalder 	Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer.
64e5f9569dSTyler Dauwalder */
651c4b4100STyler Dauwalder std::string to_lower(const char *str);
661c4b4100STyler Dauwalder 
67e5f9569dSTyler Dauwalder /*! \brief Places a copy of \c str in \c result in which all alphabetic
68e5f9569dSTyler Dauwalder 	characters are lowercase.
69e5f9569dSTyler Dauwalder 
70e5f9569dSTyler Dauwalder 	Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer.
71e5f9569dSTyler Dauwalder */
721c4b4100STyler Dauwalder void to_lower(const char *str, std::string &result);
731c4b4100STyler Dauwalder 
74e5f9569dSTyler Dauwalder /*! \brief Copies \c str into \c result, converting any uppercase alphabetics
75e5f9569dSTyler Dauwalder 	to lowercase.
76e5f9569dSTyler Dauwalder 
77e5f9569dSTyler Dauwalder 	\a str and \a result may point to the same string. \a result is
78e5f9569dSTyler Dauwalder 	assumed to be as long as or longer than \a str.
79e5f9569dSTyler Dauwalder */
801c4b4100STyler Dauwalder void to_lower(const char *str, char *result);
811c4b4100STyler Dauwalder 
821c4b4100STyler Dauwalder //! Converts \c str to lowercase.
831c4b4100STyler Dauwalder void to_lower(char *str);
841c4b4100STyler Dauwalder 
85e5f9569dSTyler Dauwalder /*! \brief Escapes any whitespace or other special characters in the path
86e5f9569dSTyler Dauwalder 
87e5f9569dSTyler Dauwalder 	\a result must be large enough to accomodate the addition of
88e5f9569dSTyler Dauwalder 	escape sequences to \a str. \a str and \a result may *NOT* point to
89e5f9569dSTyler Dauwalder 	the same string.
90e5f9569dSTyler Dauwalder 
91e5f9569dSTyler Dauwalder 	Note that this function was designed for use with the registrar's
92e5f9569dSTyler Dauwalder 	RecentEntries class, and may not create escapes exactly like you're
93e5f9569dSTyler Dauwalder 	hoping.	Please double check the code for the function to see if this
94e5f9569dSTyler Dauwalder 	is the case.
95e5f9569dSTyler Dauwalder */
96e5f9569dSTyler Dauwalder void escape_path(const char *str, char *result);
97e5f9569dSTyler Dauwalder 
98e5f9569dSTyler Dauwalder /*! \brief Escapes any whitespace or other special characters in the path
99e5f9569dSTyler Dauwalder 
100e5f9569dSTyler Dauwalder 	\a str must be large enough to accomodate the addition of
101e5f9569dSTyler Dauwalder 	escape sequences.
102e5f9569dSTyler Dauwalder */
103e5f9569dSTyler Dauwalder void escape_path(char *str);
104e5f9569dSTyler Dauwalder 
105db10640dSIngo Weinhold /*!	\brief Returns whether the supplied device ID refers to the root FS.
106db10640dSIngo Weinhold */
107db10640dSIngo Weinhold bool device_is_root_device(dev_t device);
108db10640dSIngo Weinhold 
109db10640dSIngo Weinhold // FDCloser
110db10640dSIngo Weinhold class FDCloser {
111db10640dSIngo Weinhold public:
FDCloser(int fd)112db10640dSIngo Weinhold 	FDCloser(int fd)
113db10640dSIngo Weinhold 		: fFD(fd)
114db10640dSIngo Weinhold 	{
115db10640dSIngo Weinhold 	}
116db10640dSIngo Weinhold 
~FDCloser()117db10640dSIngo Weinhold 	~FDCloser()
118db10640dSIngo Weinhold 	{
119db10640dSIngo Weinhold 		Close();
120db10640dSIngo Weinhold 	}
121db10640dSIngo Weinhold 
SetTo(int fd)122db10640dSIngo Weinhold 	void SetTo(int fd)
123db10640dSIngo Weinhold 	{
124db10640dSIngo Weinhold 		Close();
125db10640dSIngo Weinhold 		fFD = fd;
126db10640dSIngo Weinhold 	}
127db10640dSIngo Weinhold 
128db10640dSIngo Weinhold // implemented in the source file to not expose syscalls to the unit tests
129db10640dSIngo Weinhold // which include this file too
130db10640dSIngo Weinhold 	void Close();
131db10640dSIngo Weinhold //	void Close()
132db10640dSIngo Weinhold //	{
133db10640dSIngo Weinhold //		if (fFD >= 0)
134db10640dSIngo Weinhold //			_kern_close(fFD);
135db10640dSIngo Weinhold //		fFD = -1;
136db10640dSIngo Weinhold //	}
137db10640dSIngo Weinhold 
Detach()138db10640dSIngo Weinhold 	int Detach()
139db10640dSIngo Weinhold 	{
140db10640dSIngo Weinhold 		int fd = fFD;
141db10640dSIngo Weinhold 		fFD = -1;
142db10640dSIngo Weinhold 		return fd;
143db10640dSIngo Weinhold 	}
144db10640dSIngo Weinhold 
145db10640dSIngo Weinhold private:
146db10640dSIngo Weinhold 	int	fFD;
147db10640dSIngo Weinhold };
148db10640dSIngo Weinhold 
14909d84e61STyler Dauwalder };	// namespace Storage
15009d84e61STyler Dauwalder };	// namespace BPrivate
15152a38012Sejakowatz 
152db10640dSIngo Weinhold using BPrivate::Storage::FDCloser;
153db10640dSIngo Weinhold 
15482b75665STyler Dauwalder #endif	// _STORAGE_SUPPORT_H
155