Debian Codegraph @

src/utilities/ntrace/os/FileObject.h

raw
1 /*
2 * PROGRAM: Client/Server Common Code
3 * MODULE: FileObject.h
4 * DESCRIPTION: Wrapper class for platform IO
5 *
6 * The contents of this file are subject to the Initial
7 * Developer's Public License Version 1.0 (the "License");
8 * you may not use this file except in compliance with the
9 * License. You may obtain a copy of the License at
10 * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
11 *
12 * Software distributed under the License is distributed AS IS,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
14 * See the License for the specific language governing rights
15 * and limitations under the License.
16 *
17 * The Original Code was created by Nickolay Samofatov
18 * for the Firebird Open Source RDBMS project.
19 *
20 * Copyright (c) 2004 Nickolay Samofatov <[email protected]>
21 * and all contributors signed below.
22 *
23 * All Rights Reserved.
24 * Contributor(s): ______________________________________.
25 *
26 */
27
28 #ifndef OS_FILEOBJECT_H
29 #define OS_FILEOBJECT_H
30
31 #include "../../../common/classes/fb_string.h"
32 #include "../../../common/classes/locks.h"
33
34 #ifdef WIN_NT
35 #define NEWLINE "\r\n"
36 #else
37 #define NEWLINE "\n"
38 #endif
39
40 enum FileOpenFlags
41 {
42 fo_rdonly = 0x0000, // open for reading only
43 fo_wronly = 0x0001, // open for writing only
44 fo_rdwr = 0x0002, // open for reading and writing
45 fo_append = 0x0008, // writes done at eof
46
47 fo_creat = 0x0100, // create and open file
48 fo_trunc = 0x0200, // open and truncate
49 fo_excl = 0x0400, // open only if file doesn't already exist
50
51 // Temporary file bit - file is deleted when last handle is closed
52 fo_temporary = 0x0040, // temporary file bit
53
54 // temporary access hint
55 fo_short_lived = 0x1000, // temporary storage file, try not to flush
56
57 // sequential/random access hints
58 fo_sequential = 0x0020, // file access is primarily sequential
59 fo_random = 0x0010 // file access is primarily random
60 };
61
62 enum SeekOrigin
63 {
64 so_from_beginning,
65 so_from_current,
66 so_from_end
67 };
68
69 class FileObject : public Firebird::AutoStorage
70 {
71 public:
72 FileObject(const Firebird::PathName& afilename, int flags, int pflags = 0666) :
73 filename(getPool(), afilename),
74 #ifdef WIN_NT
75 file(INVALID_HANDLE_VALUE),
76 append_mutex(INVALID_HANDLE_VALUE)
77 #else
78 file(-1)
79 #endif
80 {
81 open(flags, pflags);
82 }
83
84 FileObject(Firebird::MemoryPool& pool, const Firebird::PathName& afilename, int flags, int pflags = 0666) :
85 Firebird::AutoStorage(pool), filename(getPool(), afilename),
86 #ifdef WIN_NT
87 file(INVALID_HANDLE_VALUE),
88 append_mutex(INVALID_HANDLE_VALUE)
89 #else
90 file(-1)
91 #endif
92 {
93 open(flags, pflags);
94 }
95
96 ~FileObject();
97
98 // Platform-specific stuff
99 size_t blockRead(void* buffer, size_t bytesToRead);
100 void blockWrite(const void* buffer, size_t bytesToWrite);
101 void writeHeader(const void* buffer, size_t bytesToWrite);
102 SINT64 seek(SINT64 offset, SeekOrigin origin);
103 FB_UINT64 size();
104
105 //This method used when log file was renamed by another process
106 void reopen();
107 bool renameFile(const Firebird::PathName new_filename);
108
109 // Generic stuff. Let it be inline for the moment.
110 // If there will be a more than a few such methods we need to use inheritance
111 bool readLine(Firebird::string& dest)
112 {
113 // This method is not very efficient, but is still much better than
114 // reading characters one-by-one. Plus it can handle line breaks in
115 // Windows, Linux and Macintosh format nicely on all platforms
116 char buffer[100];
117 size_t bytesRead;
118 dest.resize(0);
119 bool prevCR = false;
120
121 do
122 {
123 bytesRead = blockRead(buffer, sizeof(buffer));
124 for (int pos = 0; pos < bytesRead; pos++)
125 {
126 switch (buffer[pos])
127 {
128 case '\n':
129 dest.append(buffer, pos);
130 // Adjust file pointer
131 seek(pos - bytesRead + 1, so_from_current);
132 // Kill trailing CR if present (Windows)
133 if (prevCR)
134 dest.resize(dest.length() - 1);
135 return true;
136 case '\r':
137 prevCR = true;
138 break;
139 default:
140 if (prevCR)
141 {
142 dest.append(buffer, pos);
143 // Adjust file pointer
144 seek(pos - bytesRead, so_from_current);
145 return true;
146 }
147 }
148 }
149 dest.append(buffer, bytesRead);
150 } while (bytesRead == sizeof(buffer));
151
152 // Kill trailing CR if present
153 if (prevCR)
154 dest.resize(dest.length() - 1);
155
156 return dest.length() || bytesRead;
157 }
158
159 void writeLine(const Firebird::string& from)
160 {
161 // Line should be written in a single BlockWrite call to handle file append properly
162 Firebird::string temp = from.substr(0, from.max_length() - 2) + NEWLINE;
163 blockWrite(temp.c_str(), temp.length());
164 }
165
166 private:
167 // Forbid copy and assignment operators
168 FileObject(const FileObject&);
169 FileObject& operator =(const FileObject&);
170
171 void open(int flags, int pflags);
172
173 Firebird::PathName filename;
174 #ifdef WIN_NT
175 HANDLE file;
176 HANDLE append_mutex;
177 #else
178 int file;
179 #endif
180 };
181
182 #endif // OS_FILEOBJECT_H