| 1 | /*************************************************************************** |
|---|
| 2 | * ioFileTemp.cc |
|---|
| 3 | * |
|---|
| 4 | * Wed Oct 8 2005 |
|---|
| 5 | * Copyright 2005 Dmytro Milinevskyy |
|---|
| 6 | * milinevskyy@gmail.com |
|---|
| 7 | ****************************************************************************/ |
|---|
| 8 | |
|---|
| 9 | /* |
|---|
| 10 | * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | * it under the terms of the GNU Lesser General Public License version 2.1 as published by |
|---|
| 12 | * the Free Software Foundation; |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU Library General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * vim indentation settings |
|---|
| 26 | * set tabstop=4 |
|---|
| 27 | * set shiftwidth=4 |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <libdodo/directives.h> |
|---|
| 31 | |
|---|
| 32 | #include <unistd.h> |
|---|
| 33 | #include <sys/types.h> |
|---|
| 34 | #include <sys/stat.h> |
|---|
| 35 | #include <errno.h> |
|---|
| 36 | #include <string.h> |
|---|
| 37 | |
|---|
| 38 | #include "ioFile.inline" |
|---|
| 39 | |
|---|
| 40 | #include <libdodo/ioFileTemp.h> |
|---|
| 41 | #include <libdodo/ioFileTempEx.h> |
|---|
| 42 | #include <libdodo/ioFileRegular.h> |
|---|
| 43 | #include <libdodo/types.h> |
|---|
| 44 | #include <libdodo/ioChannel.h> |
|---|
| 45 | #include <libdodo/ioBlockChannel.h> |
|---|
| 46 | #include <libdodo/pcSyncStack.h> |
|---|
| 47 | |
|---|
| 48 | using namespace dodo::io::file; |
|---|
| 49 | |
|---|
| 50 | temp::temp(bool open, |
|---|
| 51 | short protection) : block::channel(protection), |
|---|
| 52 | regular(protection) |
|---|
| 53 | { |
|---|
| 54 | #ifndef IO_WO_XEXEC |
|---|
| 55 | collectedData.setExecObject(xexec::OBJECT_IOFILETEMP); |
|---|
| 56 | #endif |
|---|
| 57 | |
|---|
| 58 | if (open) { |
|---|
| 59 | handle->file = tmpfile(); |
|---|
| 60 | if (handle->file == NULL) { |
|---|
| 61 | delete handle; |
|---|
| 62 | |
|---|
| 63 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_TEMP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | //------------------------------------------------------------------- |
|---|
| 69 | |
|---|
| 70 | temp::temp(const temp &fd) : block::channel(protection), |
|---|
| 71 | regular(protection) |
|---|
| 72 | { |
|---|
| 73 | #ifndef IO_WO_XEXEC |
|---|
| 74 | collectedData.setExecObject(xexec::OBJECT_IOFILETEMP); |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | handle = new io::__file__; |
|---|
| 78 | |
|---|
| 79 | append = fd.append; |
|---|
| 80 | pos = fd.pos; |
|---|
| 81 | bs = fd.bs; |
|---|
| 82 | |
|---|
| 83 | if (fd.handle->file != NULL) { |
|---|
| 84 | int oldDesc, newDesc; |
|---|
| 85 | |
|---|
| 86 | oldDesc = fileno(fd.handle->file); |
|---|
| 87 | if (oldDesc == -1) { |
|---|
| 88 | delete handle; |
|---|
| 89 | |
|---|
| 90 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_TEMP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | newDesc = dup(oldDesc); |
|---|
| 94 | if (newDesc == -1) { |
|---|
| 95 | delete handle; |
|---|
| 96 | |
|---|
| 97 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_TEMP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | handle->file = fdopen(newDesc, "r+"); |
|---|
| 101 | |
|---|
| 102 | if (handle->file == NULL) { |
|---|
| 103 | delete handle; |
|---|
| 104 | |
|---|
| 105 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_TEMP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | //------------------------------------------------------------------- |
|---|
| 111 | |
|---|
| 112 | temp::~temp() |
|---|
| 113 | { |
|---|
| 114 | if (handle->file != NULL) |
|---|
| 115 | fclose(handle->file); |
|---|
| 116 | |
|---|
| 117 | delete handle; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | //------------------------------------------------------------------- |
|---|
| 121 | |
|---|
| 122 | void |
|---|
| 123 | temp::clone(const temp &fd) |
|---|
| 124 | { |
|---|
| 125 | pc::sync::stack pg(keeper); |
|---|
| 126 | |
|---|
| 127 | if (handle->file != NULL) { |
|---|
| 128 | if (fclose(handle->file) != 0) |
|---|
| 129 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 130 | |
|---|
| 131 | handle->file = NULL; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | pos = fd.pos; |
|---|
| 135 | append = fd.append; |
|---|
| 136 | bs = fd.bs; |
|---|
| 137 | |
|---|
| 138 | if (fd.handle->file != NULL) { |
|---|
| 139 | int oldDesc, newDesc; |
|---|
| 140 | |
|---|
| 141 | oldDesc = fileno(fd.handle->file); |
|---|
| 142 | if (oldDesc == -1) |
|---|
| 143 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 144 | |
|---|
| 145 | newDesc = dup(oldDesc); |
|---|
| 146 | if (newDesc == -1) |
|---|
| 147 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 148 | |
|---|
| 149 | handle->file = fdopen(newDesc, "r+"); |
|---|
| 150 | |
|---|
| 151 | if (handle->file == NULL) |
|---|
| 152 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | //------------------------------------------------------------------- |
|---|
| 157 | |
|---|
| 158 | void |
|---|
| 159 | temp::open() |
|---|
| 160 | { |
|---|
| 161 | pc::sync::stack pg(keeper); |
|---|
| 162 | |
|---|
| 163 | #ifndef IO_WO_XEXEC |
|---|
| 164 | performPreExec(OPERATION_OPEN); |
|---|
| 165 | #endif |
|---|
| 166 | |
|---|
| 167 | if (handle->file != NULL) { |
|---|
| 168 | if (fclose(handle->file) != 0) |
|---|
| 169 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 170 | |
|---|
| 171 | handle->file = NULL; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | handle->file = tmpfile(); |
|---|
| 175 | if (handle->file == NULL) |
|---|
| 176 | dodo_throw exception::basic(exception::MODULE_IOFILETEMP, TEMPEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); |
|---|
| 177 | |
|---|
| 178 | #ifndef IO_WO_XEXEC |
|---|
| 179 | performPostExec(OPERATION_OPEN); |
|---|
| 180 | #endif |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | //------------------------------------------------------------------- |
|---|
| 184 | |
|---|