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

tdecore

Public Member Functions | Protected Member Functions | Friends | List of all members
KTempFile Class Reference

#include <tdetempfile.h>

Public Member Functions

 KTempFile (TQString filePrefix=TQString::null, TQString fileExtension=TQString::null, int mode=0600)
 
 ~KTempFile ()
 
void setAutoDelete (bool autoDelete)
 
int status () const
 
TQString name () const
 
int handle () const
 
FILE * fstream ()
 
TQTextStream * textStream ()
 
TQDataStream * dataStream ()
 
TQFile * file ()
 
void unlink ()
 
bool sync ()
 
bool close ()
 

Protected Member Functions

 KTempFile (bool)
 
bool create (const TQString &filePrefix, const TQString &fileExtension, int mode)
 
void setError (int error)
 

Friends

class KSaveFile
 

Detailed Description

The KTempFile class creates and opens a unique file for temporary use.

This is especially useful if you need to create a file in a world writable directory like /tmp without being vulnerable to so called symlink attacks.

KDE applications, however, shouldn't create files in /tmp in the first place but use the "tmp" resource instead. The standard KTempFile constructor will do that by default.

To create a temporary file that starts with a certain name in the "tmp" resource, one should use: KTempFile(locateLocal("tmp", prefix), extension);

KTempFile does not create any missing directories, but locateLocal() does.

See also TDEStandardDirs

Author
Waldo Bastian basti.nosp@m.an@k.nosp@m.de.or.nosp@m.g

Definition at line 55 of file tdetempfile.h.

Constructor & Destructor Documentation

◆ KTempFile() [1/2]

KTempFile::KTempFile ( TQString  filePrefix = TQString::null,
TQString  fileExtension = TQString::null,
int  mode = 0600 
)

Creates a temporary file with the name: <filePrefix><six letters><fileExtension>

The default filePrefix is "$TDEHOME/tmp-$HOST/appname/" The default fileExtension is ".tmp"

Parameters
filePrefixthe prefix of the file name, or TQString::null for the default value
fileExtensionthe extension of the prefix, or TQString::null for the default value
modethe file permissions

Definition at line 60 of file tdetempfile.cpp.

◆ ~KTempFile()

KTempFile::~KTempFile ( )

The destructor closes the file.

If autoDelete is enabled the file gets unlinked as well.

Definition at line 133 of file tdetempfile.cpp.

◆ KTempFile() [2/2]

KTempFile::KTempFile ( bool  )
protected

Constructor used by KSaveFile.

Definition at line 79 of file tdetempfile.cpp.

Member Function Documentation

◆ close()

bool KTempFile::close ( )

Closes the file.

See status() for details about errors.

Returns
true if successful, or false if an error has occurred.

Definition at line 257 of file tdetempfile.cpp.

◆ create()

bool KTempFile::create ( const TQString &  filePrefix,
const TQString &  fileExtension,
int  mode 
)
protected

Definition at line 92 of file tdetempfile.cpp.

◆ dataStream()

TQDataStream * KTempFile::dataStream ( )

Returns a TQDataStream for writing.

Returns
TQDataStream open for writing to the file, or 0 if opening the file failed

Definition at line 196 of file tdetempfile.cpp.

◆ file()

TQFile * KTempFile::file ( )

Returns a TQFile.

Returns
A TQFile open for writing to the file, or 0 if opening the file failed.

Definition at line 174 of file tdetempfile.cpp.

◆ fstream()

FILE * KTempFile::fstream ( )

Returns the FILE* of the temporary file.

Returns
FILE* stream open for writing to the file, or 0 if opening the file failed

Definition at line 159 of file tdetempfile.cpp.

◆ handle()

int KTempFile::handle ( ) const

An integer file descriptor open for writing to the file.

Returns
The file descriptor, or a negative number if opening the file failed

Definition at line 153 of file tdetempfile.cpp.

◆ name()

TQString KTempFile::name ( ) const

Returns the full path and name of the file.

Note that in most circumstances the file needs to be closed before you use it by name.

In particular, if another process or software part needs to write data to the file based on the filename, the file should be closed before doing so. Otherwise the act of closing the file later on may cause the file to get truncated to a zero-size, resulting in an unexpected loss of the data.

In some cases there is only interest in the filename itself but where the actual presence of a file with such name is a problem. In that case the file should first be both closed and unlinked. Such usage is not recommended since it may lead to the kind of symlink vulnerabilities that the KTempFile design attempts to prevent.

Returns
The name of the file, or TQString::null if opening the file has failed or the file has been unlinked already.

Definition at line 147 of file tdetempfile.cpp.

◆ setAutoDelete()

void KTempFile::setAutoDelete ( bool  autoDelete)
inline

Turn automatic deletion on or off.

Automatic deletion is off by default.

Parameters
autoDeletetrue to turn automatic deletion on

Definition at line 87 of file tdetempfile.h.

◆ setError()

void KTempFile::setError ( int  error)
inlineprotected

Definition at line 198 of file tdetempfile.h.

◆ status()

int KTempFile::status ( ) const

Returns the status of the file based on errno.

(see errno.h) 0 means OK.

You should check the status after object creation to check whether a file could be created in the first place.

You may check the status after closing the file to verify that the file has indeed been written correctly.

Returns
the errno status, 0 means ok

Definition at line 141 of file tdetempfile.cpp.

◆ sync()

bool KTempFile::sync ( )

Flushes file to disk (fsync).

If you want to be as sure as possible that the file data has actually been physically stored on disk you need to call sync().

See status() for details about errors.

Returns
true if successful, or false if an error has occurred.
Since
3.3

Definition at line 220 of file tdetempfile.cpp.

◆ textStream()

TQTextStream * KTempFile::textStream ( )

Returns the TQTextStream for writing.

Returns
TQTextStream open for writing to the file, or 0 if opening the file failed

Definition at line 186 of file tdetempfile.cpp.

◆ unlink()

void KTempFile::unlink ( )

Unlinks the file from the directory.

The file is deleted once the last reader/writer closes it.

Definition at line 206 of file tdetempfile.cpp.

Friends And Related Function Documentation

◆ KSaveFile

friend class KSaveFile
friend

Definition at line 57 of file tdetempfile.h.


The documentation for this class was generated from the following files:
  • tdetempfile.h
  • tdetempfile.cpp

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.