1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef SYNCML_DM_FILEHANDLE_H 18#define SYNCML_DM_FILEHANDLE_H 19 20#ifndef __cplusplus 21#error "This is a C++ header file; it requires C++ to compile." 22#endif 23 24#include "syncml_dm_data_types.h" 25#include "xpl_File.h" 26#include "dmMemory.h" 27#include "dmstring.h" 28 29class DMFileHandler 30{ 31 public: 32 33 /* File name extensions for serialization */ 34 static const char TEMP_FILE_EXTENSION[]; 35 static const char BAK_FILE_EXTENSION[]; 36 static const char COMMIT_LOG_EXTENSION[]; 37 static const char LOG_FILE_EXTENSION[]; 38 static const char MIDDLE_FILE_EXTENSION[]; 39 enum { MAX_INTERNAL_BUFFER_LENGTH = 1024}; 40 41 42 /* Constructor for the class takes absolute path of the file */ 43 DMFileHandler(CPCHAR path); 44 45 DMFileHandler(CPCHAR path, BOOLEAN bIsCache); 46 47 /* Frees the internal buffer */ 48 ~DMFileHandler(); 49 50 /* Opens a file for I/O */ 51 SYNCML_DM_RET_STATUS_T open(INT32 mode); 52 53 /* Reads count bytes into buffer */ 54 inline SYNCML_DM_RET_STATUS_T read(UINT8* buffer, UINT16 count) {return read((char*)( buffer ), count);} 55 SYNCML_DM_RET_STATUS_T read(char* buffer, UINT16 count); 56 57 /* Writes count bytes into the buffer/file 58 * For the purposes of performance, this function 59 * adds small writes to a buffer before sending to the 60 * filesystem.*/ 61 inline SYNCML_DM_RET_STATUS_T write(const UINT8* buffer, UINT16 count) {return write((const char*)( buffer ), count);} 62 SYNCML_DM_RET_STATUS_T write(const char* buffer, UINT16 count); 63 64 /* Seek offset bytes relative to seekFrom */ 65 SYNCML_DM_RET_STATUS_T seek(XPL_FS_SEEK_MODE_T whence, INT32 offset); 66 67 /* Close the file handle */ 68 SYNCML_DM_RET_STATUS_T close(); 69 70 /* Rename the file to name */ 71 SYNCML_DM_RET_STATUS_T rename(CPCHAR newFileName); 72 73 /* Delete the file */ 74 SYNCML_DM_RET_STATUS_T deleteFile(); 75 76 /* Create an unique External Storage Node file name */ 77 static SYNCML_DM_RET_STATUS_T createTempESNFileName(DMString &fileName); 78 79 /* Returns file size */ 80 XPL_FS_SIZE_T size(); 81 82 // FILE* api functions 83 char* fgets( char* s, INT32 size ); 84 85 BOOLEAN iseof(); 86 87 /* Flushes the internal write buffer if it exists */ 88 SYNCML_DM_RET_STATUS_T flush(); 89 90 UINT8 * mmap(); 91 92 SYNCML_DM_RET_STATUS_T unmmap(); 93 94 XPL_FS_SEEK_OFFSET_T position(); 95 96 #ifndef DM_NO_LOCKING 97 SYNCML_DM_RET_STATUS_T lock(BOOLEAN bLockExclusive ); 98 99 SYNCML_DM_RET_STATUS_T unlock(); 100#endif 101 102 inline const char* getFullPath() const { return m_strPath.c_str(); } 103 104 inline void* operator new(size_t sz) 105 { 106 return (DmAllocMem(sz)); 107 } 108 109 inline void operator delete(void* buf) 110 { 111 DmFreeMem(buf); 112 } 113 114 private: 115 116 /* The file handle reference */ 117 XPL_FS_HANDLE_T m_nFileHandle; 118 119 /* Absolute path of the file pointed to by the file handle */ 120 DMString m_strPath; 121 122 /* Internal buffer data members */ 123 char * m_pInternalBuf; 124 UINT8 * m_pMemMap; 125 UINT16 m_nInternalBufLen; 126 UINT16 m_nBufPos; // reading operations required 2 int: buf size and pos 127 XPL_FS_SIZE_T m_nSize; 128 129 XPL_FS_SEEK_OFFSET_T m_nFilePos; 130 XPL_FS_SEEK_OFFSET_T m_nBufOffset; 131 BOOLEAN m_bWrite; 132 BOOLEAN m_bIsCache; 133 BOOLEAN m_bIsMap; 134 XPL_FS_OPEN_MODE_T m_nMode; 135 136 /* Writes count bytes into the file. 137 * This method does the actual writes to the filesystem */ 138 SYNCML_DM_RET_STATUS_T writeBuffer(const char* buffer, UINT16 count); 139 140 void init(CPCHAR path); 141}; 142 143#endif /* SYNCML_DM_FILEHANDLE_H */ 144