btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / storage / storage_support.h
blob309aab6225131e2efd62db0e1f63689f313781d0
1 /*
2 * Copyright 2002-2006, Haiku Inc.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Tyler Dauwalder
7 */
8 #ifndef _STORAGE_SUPPORT_H
9 #define _STORAGE_SUPPORT_H
11 /*!
12 \file storage_support.h
13 Interface declarations for miscellaneous internal
14 Storage Kit support functions.
17 #include <StorageDefs.h>
18 #include <SupportDefs.h>
20 #include <dirent.h>
21 #include <string>
24 namespace BPrivate {
25 namespace Storage {
27 // For convenience:
28 struct LongDirEntry : dirent { char _buffer[B_FILE_NAME_LENGTH]; };
30 //! Returns whether the supplied path is absolute.
31 bool is_absolute_path(const char *path);
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);
37 //! splits a path name into directory path and leaf name
38 status_t split_path(const char *fullPath, char *&path, char *&leaf);
40 //! splits a path name into directory path and leaf name
41 status_t split_path(const char *fullPath, char **path, char **leaf);
43 //! Parses the first component of a path name.
44 status_t parse_first_path_component(const char *path, int32& length,
45 int32& nextComponent);
47 //! Parses the first component of a path name.
48 status_t parse_first_path_component(const char *path, char *&component,
49 int32& nextComponent);
51 //! Checks whether an entry name is a valid entry name.
52 status_t check_entry_name(const char *entry);
54 //! Checks whether a path name is a valid path name.
55 status_t check_path_name(const char *path);
57 /*! \brief Returns a copy of \c str in which all alphabetic characters
58 are lowercase.
60 Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer.
62 std::string to_lower(const char *str);
64 /*! \brief Places a copy of \c str in \c result in which all alphabetic
65 characters are lowercase.
67 Returns \c "(null)" if you're a bonehead and pass in a \c NULL pointer.
69 void to_lower(const char *str, std::string &result);
71 /*! \brief Copies \c str into \c result, converting any uppercase alphabetics
72 to lowercase.
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.
77 void to_lower(const char *str, char *result);
79 //! Converts \c str to lowercase.
80 void to_lower(char *str);
82 /*! \brief Escapes any whitespace or other special characters in the path
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.
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.
93 void escape_path(const char *str, char *result);
95 /*! \brief Escapes any whitespace or other special characters in the path
97 \a str must be large enough to accomodate the addition of
98 escape sequences.
100 void escape_path(char *str);
102 /*! \brief Returns whether the supplied device ID refers to the root FS.
104 bool device_is_root_device(dev_t device);
106 // FDCloser
107 class FDCloser {
108 public:
109 FDCloser(int fd)
110 : fFD(fd)
114 ~FDCloser()
116 Close();
119 void SetTo(int fd)
121 Close();
122 fFD = fd;
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 // }
135 int Detach()
137 int fd = fFD;
138 fFD = -1;
139 return fd;
142 private:
143 int fFD;
146 }; // namespace Storage
147 }; // namespace BPrivate
149 using BPrivate::Storage::FDCloser;
151 #endif // _STORAGE_SUPPORT_H