• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
tdetempfile.cpp
1/*
2 *
3 * This file is part of the KDE libraries
4 * Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
5 *
6 * $Id$
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License version 2 as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 **/
22
23#include <config.h>
24
25#include <sys/types.h>
26
27#ifdef HAVE_SYS_STAT_H
28#include <sys/stat.h>
29#endif
30
31#include <fcntl.h>
32#include <stdlib.h>
33#include <unistd.h>
34
35#ifdef HAVE_TEST
36#include <test.h>
37#endif
38#ifdef HAVE_PATHS_H
39#include <paths.h>
40#endif
41
42#ifndef _PATH_TMP
43#define _PATH_TMP "/tmp"
44#endif
45
46#include <tqdatetime.h>
47#include <tqfile.h>
48#include <tqdatastream.h>
49#include <tqtextstream.h>
50
51#include "tdeglobal.h"
52#include "tdeapplication.h"
53#include "kinstance.h"
54#include "tdetempfile.h"
55#include "tdestandarddirs.h"
56#include "kde_file.h"
57#include "kdebug.h"
58
59/* antlarr: KDE 4: make the parameters const TQString & */
60KTempFile::KTempFile(TQString filePrefix, TQString fileExtension, int mode)
61{
62 bAutoDelete = false;
63 mFd = -1;
64 mStream = 0;
65 mFile = 0;
66 mTextStream = 0;
67 mDataStream = 0;
68 mError = 0;
69 bOpen = false;
70 if (fileExtension.isEmpty())
71 fileExtension = ".tmp";
72 if (filePrefix.isEmpty())
73 {
74 filePrefix = locateLocal("tmp", TDEGlobal::instance()->instanceName());
75 }
76 (void) create(filePrefix, fileExtension, mode);
77}
78
79KTempFile::KTempFile(bool)
80{
81 bAutoDelete = false;
82 mFd = -1;
83 mStream = 0;
84 mFile = 0;
85 mTextStream = 0;
86 mDataStream = 0;
87 mError = 0;
88 bOpen = false;
89}
90
91bool
92KTempFile::create(const TQString &filePrefix, const TQString &fileExtension,
93 int mode)
94{
95 // make sure the random seed is randomized
96 (void) TDEApplication::random();
97
98 TQCString ext = TQFile::encodeName(fileExtension);
99 TQCString nme = TQFile::encodeName(filePrefix) + "XXXXXX" + ext;
100 if((mFd = mkstemps(nme.data(), ext.length())) < 0)
101 {
102 // Recreate it for the warning, mkstemps emptied it
103 TQCString nme = TQFile::encodeName(filePrefix) + "XXXXXX" + ext;
104 kdWarning() << "KTempFile: Error trying to create " << nme << ": " << strerror(errno) << endl;
105 mError = errno;
106 mTmpName = TQString::null;
107 return false;
108 }
109
110 // got a file descriptor. nme contains the name
111 mTmpName = TQFile::decodeName(nme);
112 mode_t tmp = 0;
113 mode_t umsk = umask(tmp);
114 umask(umsk);
115 fchmod(mFd, mode&(~umsk));
116
117 // Success!
118 bOpen = true;
119
120 uid_t uid = getuid();
121 uid_t euid = geteuid();
122 if (uid != euid) {
123 // Set uid/gid (necessary for SUID programs)
124 fchown(mFd, getuid(), getgid());
125 }
126
127 // Set close on exec
128 fcntl(mFd, F_SETFD, FD_CLOEXEC);
129
130 return true;
131}
132
133KTempFile::~KTempFile()
134{
135 close();
136 if (bAutoDelete)
137 unlink();
138}
139
140int
141KTempFile::status() const
142{
143 return mError;
144}
145
146TQString
147KTempFile::name() const
148{
149 return mTmpName;
150}
151
152int
153KTempFile::handle() const
154{
155 return mFd;
156}
157
158FILE *
159KTempFile::fstream()
160{
161 if (mStream) return mStream;
162 if (mFd < 0) return 0;
163
164 // Create a stream
165 mStream = KDE_fdopen(mFd, "r+");
166 if (!mStream) {
167 kdWarning() << "KTempFile: Error trying to open " << mTmpName << ": " << strerror(errno) << endl;
168 mError = errno;
169 }
170 return mStream;
171}
172
173TQFile *
174KTempFile::file()
175{
176 if (mFile) return mFile;
177 if ( !fstream() ) return 0;
178
179 mFile = new TQFile();
180 mFile->setName( name() );
181 mFile->open(IO_ReadWrite, mStream);
182 return mFile;
183}
184
185TQTextStream *
186KTempFile::textStream()
187{
188 if (mTextStream) return mTextStream;
189 if ( !file() ) return 0; // Initialize mFile
190
191 mTextStream = new TQTextStream( mFile );
192 return mTextStream;
193}
194
195TQDataStream *
196KTempFile::dataStream()
197{
198 if (mDataStream) return mDataStream;
199 if ( !file() ) return 0; // Initialize mFile
200
201 mDataStream = new TQDataStream( mFile );
202 return mDataStream;
203}
204
205void
206KTempFile::unlink()
207{
208 if (!mTmpName.isEmpty())
209 TQFile::remove( mTmpName );
210 mTmpName = TQString::null;
211}
212
213#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
214#define FDATASYNC fdatasync
215#else
216#define FDATASYNC fsync
217#endif
218
219bool
220KTempFile::sync()
221{
222 int result = 0;
223
224 if (mStream)
225 {
226 do {
227 result = fflush(mStream); // We need to flush first otherwise fsync may not have our data
228 }
229 while ((result == -1) && (errno == EINTR));
230
231 if (result)
232 {
233 kdWarning() << "KTempFile: Error trying to flush " << mTmpName << ": " << strerror(errno) << endl;
234 mError = errno;
235 }
236 }
237
238 if (mFd >= 0)
239 {
240 if( qstrcmp( getenv( "TDE_EXTRA_FSYNC" ), "1" ) == 0 )
241 {
242 result = FDATASYNC(mFd);
243 if (result)
244 {
245 kdWarning() << "KTempFile: Error trying to sync " << mTmpName << ": " << strerror(errno) << endl;
246 mError = errno;
247 }
248 }
249 }
250
251 return (mError == 0);
252}
253
254#undef FDATASYNC
255
256bool
257KTempFile::close()
258{
259 int result = 0;
260 delete mTextStream; mTextStream = 0;
261 delete mDataStream; mDataStream = 0;
262 delete mFile; mFile = 0;
263
264 if (mStream)
265 {
266 result = ferror(mStream);
267 if (result)
268 mError = ENOSPC; // Assume disk full.
269
270 result = fclose(mStream);
271 mStream = 0;
272 mFd = -1;
273 if (result != 0) {
274 kdWarning() << "KTempFile: Error trying to close " << mTmpName << ": " << strerror(errno) << endl;
275 mError = errno;
276 }
277 }
278
279
280 if (mFd >= 0)
281 {
282 result = ::close(mFd);
283 mFd = -1;
284 if (result != 0) {
285 kdWarning() << "KTempFile: Error trying to close " << mTmpName << ": " << strerror(errno) << endl;
286 mError = errno;
287 }
288 }
289
290 bOpen = false;
291 return (mError == 0);
292}
293
KTempFile::fstream
FILE * fstream()
Returns the FILE* of the temporary file.
Definition: tdetempfile.cpp:159
KTempFile::dataStream
TQDataStream * dataStream()
Returns a TQDataStream for writing.
Definition: tdetempfile.cpp:196
KTempFile::file
TQFile * file()
Returns a TQFile.
Definition: tdetempfile.cpp:174
KTempFile::~KTempFile
~KTempFile()
The destructor closes the file.
Definition: tdetempfile.cpp:133
KTempFile::status
int status() const
Returns the status of the file based on errno.
Definition: tdetempfile.cpp:141
KTempFile::KTempFile
KTempFile(TQString filePrefix=TQString::null, TQString fileExtension=TQString::null, int mode=0600)
Creates a temporary file with the name: <filePrefix><six letters><fileExtension>
Definition: tdetempfile.cpp:60
KTempFile::handle
int handle() const
An integer file descriptor open for writing to the file.
Definition: tdetempfile.cpp:153
KTempFile::textStream
TQTextStream * textStream()
Returns the TQTextStream for writing.
Definition: tdetempfile.cpp:186
KTempFile::close
bool close()
Closes the file.
Definition: tdetempfile.cpp:257
KTempFile::sync
bool sync()
Flushes file to disk (fsync).
Definition: tdetempfile.cpp:220
KTempFile::unlink
void unlink()
Unlinks the file from the directory.
Definition: tdetempfile.cpp:206
KTempFile::name
TQString name() const
Returns the full path and name of the file.
Definition: tdetempfile.cpp:147
TDEApplication::random
static int random()
Generates a uniform random number.
Definition: tdeapplication.cpp:3393
TDEGlobal::instance
static TDEInstance * instance()
Returns the global instance.
Definition: tdeglobal.cpp:102
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.