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 31 int Device() const { return fDevice; } 32 int Mode() const { return fMode; } 33 bool IsReadOnly() const { 34 return _IsReadOnly(fMode); } 35 36 status_t GetSize(off_t* _size, 37 uint32* _blockSize = NULL); 38 39 private: 40 static bool _IsReadOnly(int mode) 41 { return (mode & O_RWMASK) == O_RDONLY; } 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