Debian Codegraph @

src/utilities/ntrace/os/posix/misc/FileObject.cpp

raw
1 /*
2 * PROGRAM: Client/Server Common Code
3 * MODULE: FileObject.cpp
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 #include "firebird.h"
29 #include "../FileObject.h"
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <fcntl.h>
37
38 using namespace Firebird;
39 Firebird::Mutex open_mutex;
40
41 void FileObject::open(int flags, int pflags)
42 {
43 open_mutex.enter();
44 int oflags = 0;
45
46 switch (flags & (fo_rdonly | fo_wronly | fo_rdwr))
47 {
48 case fo_rdonly:
49 oflags = O_RDONLY;
50 break;
51 case fo_wronly:
52 oflags = O_WRONLY;
53 break;
54 default:
55 oflags = O_RDWR;
56 break;
57 }
58
59 if (flags & fo_append)
60 oflags |= O_APPEND;
61
62 if (flags & fo_creat)
63 oflags |= O_CREAT;
64
65 if (flags & fo_trunc)
66 oflags |= O_TRUNC;
67
68 if (flags & fo_excl)
69 oflags |= O_EXCL;
70
71 file = os_utils::open(filename.c_str(), oflags, pflags);
72 open_mutex.leave();
73
74 if (file < 0)
75 fatal_exception::raiseFmt("Error (%d) opening file: %s", errno, filename.c_str());
76
77 if (flags & fo_temporary)
78 unlink(filename.c_str());
79 }
80
81 FileObject::~FileObject()
82 {
83 close(file);
84 }
85
86 //Size of file, given by descriptor
87 FB_UINT64 FileObject::size()
88 {
89 off_t nFileLen = 0;
90 struct STAT file_stat;
91 if (!os_utils::fstat(file, &file_stat))
92 nFileLen = file_stat.st_size;
93 else
94 fatal_exception::raiseFmt("IO error (%d) file stat: %s", errno, filename.c_str());
95
96 return nFileLen;
97 }
98
99 size_t FileObject::blockRead(void* buffer, size_t bytesToRead)
100 {
101 ssize_t bytesDone = read(file, buffer, bytesToRead);
102 if (bytesDone < 0)
103 {
104 fatal_exception::raiseFmt("IO error (%d) reading file: %s", errno, filename.c_str());
105 }
106
107 return bytesDone;
108 }
109
110 void FileObject::blockWrite(const void* buffer, size_t bytesToWrite)
111 {
112 ssize_t bytesDone = write(file, buffer, bytesToWrite);
113 if (bytesDone != static_cast<ssize_t>(bytesToWrite))
114 {
115 fatal_exception::raiseFmt("IO error (%d) writing file: %s", errno, filename.c_str());
116 }
117 }
118
119 // Write data to header only if file is empty
120 void FileObject::writeHeader(const void* buffer, size_t bytesToWrite)
121 {
122 if (seek(0, so_from_end) != 0)
123 return;
124
125 ssize_t bytesDone = write(file, buffer, bytesToWrite);
126 if (bytesDone != static_cast<ssize_t>(bytesToWrite))
127 {
128 fatal_exception::raiseFmt("IO error (%d) writing file: %s", errno, filename.c_str());
129 }
130 }
131
132 void FileObject::reopen()
133 {
134 if (file >= 0)
135 close(file);
136 open(fo_rdwr | fo_append | fo_creat, 0666);
137 //fchmod(file, PMASK);
138 }
139
140 bool FileObject::renameFile(const Firebird::PathName new_filename)
141 {
142 if (rename(filename.c_str(), new_filename.c_str()))
143 {
144 int rename_err = errno;
145 if (rename_err == ENOENT || rename_err == EEXIST) {
146 // Another process renames our file just now. Open it again.
147 reopen();
148 return false;
149 }
150 fatal_exception::raiseFmt("IO error (%d) renaming file: %s", rename_err, filename.c_str());
151 }
152 else
153 reopen();
154 return true;
155 }
156
157 SINT64 FileObject::seek(SINT64 newOffset, SeekOrigin origin)
158 {
159 if (newOffset != (SINT64) LSEEK_OFFSET_CAST newOffset)
160 {
161 fatal_exception::raiseFmt("Attempt to seek file %s past platform size limit", filename.c_str());
162 }
163
164 int moveMethod;
165
166 switch(origin)
167 {
168 case so_from_beginning:
169 moveMethod = SEEK_SET;
170 break;
171 case so_from_current:
172 moveMethod = SEEK_CUR;
173 break;
174 case so_from_end:
175 moveMethod = SEEK_END;
176 break;
177 }
178
179 off_t result = os_utils::lseek(file, newOffset, moveMethod);
180
181 if (result == (off_t) -1)
182 {
183 fatal_exception::raiseFmt("IO error (%d) seeking file: %s", errno, filename.c_str());
184 }
185
186 return result;
187 }