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