| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
File.h |
| 29 |
|
| 30 |
This class wraps standard C file I/O in a convenient way for the object store |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#ifndef _SOFTHSM_V2_FILE_H |
| 34 |
#define _SOFTHSM_V2_FILE_H |
| 35 |
|
| 36 |
#include "config.h" |
| 37 |
#include "OSAttribute.h" |
| 38 |
#include <stdio.h> |
| 39 |
#include <string> |
| 40 |
|
| 41 |
class File |
| 42 |
{ |
| 43 |
public: |
| 44 |
// Constructor |
| 45 |
File(std::string inPath, bool forRead = true, bool forWrite = false, bool create = false, bool truncate = true); |
| 46 |
|
| 47 |
// Destructor |
| 48 |
virtual ~File(); |
| 49 |
|
| 50 |
// Check if the file is valid |
| 51 |
bool isValid(); |
| 52 |
|
| 53 |
// Check if the file is readable |
| 54 |
bool isRead(); |
| 55 |
|
| 56 |
// Check if the file is writable |
| 57 |
bool isWrite(); |
| 58 |
|
| 59 |
// Check if the file is empty |
| 60 |
bool isEmpty(); |
| 61 |
|
| 62 |
// Check if the end-of-file was reached |
| 63 |
bool isEOF(); |
| 64 |
|
| 65 |
// Read an unsigned long value; warning: not thread safe without locking! |
| 66 |
bool readULong(unsigned long& value); |
| 67 |
|
| 68 |
// Read a ByteString value; warning: not thread safe without locking! |
| 69 |
bool readByteString(ByteString& value); |
| 70 |
|
| 71 |
// Read a string value; warning: not thread safe without locking! |
| 72 |
bool readString(std::string& value); |
| 73 |
|
| 74 |
// Read a boolean value; warning: not thread safe without locking! |
| 75 |
bool readBool(bool& value); |
| 76 |
|
| 77 |
// Read a mechanism type set value; warning: not thread safe without locking! |
| 78 |
bool readMechanismTypeSet(std::set<CK_MECHANISM_TYPE>& value); |
| 79 |
|
| 80 |
// Read an array value; warning: not thread safe without locking! |
| 81 |
bool readAttributeMap(std::map<CK_ATTRIBUTE_TYPE,OSAttribute>& value); |
| 82 |
|
| 83 |
// Write an unsigned long value; warning: not thread safe without locking! |
| 84 |
bool writeULong(const unsigned long value); |
| 85 |
|
| 86 |
// Write a ByteString value; warning: not thread safe without locking! |
| 87 |
bool writeByteString(const ByteString& value); |
| 88 |
|
| 89 |
// Write a string value; warning: not thread safe without locking! |
| 90 |
bool writeString(const std::string& value); |
| 91 |
|
| 92 |
// Write a boolean value; warning: not thread safe without locking! |
| 93 |
bool writeBool(const bool value); |
| 94 |
|
| 95 |
// Write a mechanism type set value; warning: not thread safe without locking! |
| 96 |
bool writeMechanismTypeSet(const std::set<CK_MECHANISM_TYPE>& value); |
| 97 |
|
| 98 |
// Write an attribute map value; warning: not thread safe without locking! |
| 99 |
bool writeAttributeMap(const std::map<CK_ATTRIBUTE_TYPE,OSAttribute>& value); |
| 100 |
|
| 101 |
// Rewind the file |
| 102 |
bool rewind(); |
| 103 |
|
| 104 |
// Truncate the file |
| 105 |
bool truncate(); |
| 106 |
|
| 107 |
// Seek to the specified position relative to the start of the file; if no |
| 108 |
// argument is specified this operation seeks to the end of the file |
| 109 |
bool seek(long offset = -1); |
| 110 |
|
| 111 |
// Lock the file |
| 112 |
bool lock(bool block = true); |
| 113 |
|
| 114 |
// Unlock the file |
| 115 |
bool unlock(); |
| 116 |
|
| 117 |
// Flush the buffered stream to background storage |
| 118 |
bool flush(); |
| 119 |
|
| 120 |
private: |
| 121 |
// The file path |
| 122 |
std::string path; |
| 123 |
|
| 124 |
// The status |
| 125 |
bool valid; |
| 126 |
bool locked; |
| 127 |
|
| 128 |
// Read, write or both? |
| 129 |
bool isReadable, isWritable; |
| 130 |
|
| 131 |
// The FILE stream |
| 132 |
FILE* stream; |
| 133 |
}; |
| 134 |
|
| 135 |
#endif // !_SOFTHSM_V2_FILE_H |
| 136 |
|