xref: /haiku/headers/private/file_systems/DeviceOpener.h (revision 0b5d48563d2c6a5e83dc271ffc55ce0da97bc506)
1 /*
2  * Copyright 2008-2010, Axel Dörfler, axeld@pinc-software.de.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef DEVICEOPENER_H
7 #define DEVICEOPENER_H
8 
9 #ifdef FS_SHELL
10 #	include "fssh_api_wrapper.h"
11 #else
12 #	include <fcntl.h>
13 #	include <sys/types.h>
14 #	include <SupportDefs.h>
15 #endif
16 
17 
18 class DeviceOpener {
19 	public:
20 								DeviceOpener(int fd, int mode);
21 								DeviceOpener(const char* device, int mode);
22 								~DeviceOpener();
23 
24 				int				Open(const char* device, int mode);
25 				int				Open(int fd, int mode);
26 				void*			InitCache(off_t numBlocks, uint32 blockSize);
27 				void			RemoveCache(bool allowWrites);
28 
29 				void			Keep();
30 
Device()31 				int				Device() const { return fDevice; }
Mode()32 				int				Mode() const { return fMode; }
IsReadOnly()33 				bool			IsReadOnly() const {
34 												return _IsReadOnly(fMode); }
35 
36 				status_t		GetSize(off_t* _size,
37 										uint32* _blockSize = NULL);
38 
39 	private:
_IsReadOnly(int mode)40 			static	bool		_IsReadOnly(int mode)
41 									{ return (mode & O_RWMASK) == O_RDONLY; }
_IsReadWrite(int mode)42 			static	bool		_IsReadWrite(int mode)
43 									{ return (mode & O_RWMASK) == O_RDWR; }
44 
45 					int			fDevice;
46 					int			fMode;
47 					void*		fBlockCache;
48 };
49 
50 #endif	// DEVICEOPENER_H
51