• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDE3Support

Public Member Functions | Protected Member Functions | List of all members
K3TempFile Class Reference

#include <k3tempfile.h>

Public Member Functions

 K3TempFile (const QString &filePrefix=QString(), const QString &fileExtension=QString(), int mode=0600)
 
 ~K3TempFile ()
 
bool close ()
 
QDataStream * dataStream ()
 
QFile * file ()
 
FILE * fstream ()
 
int handle () const
 
QString name () const
 
void setAutoDelete (bool autoDelete)
 
int status () const
 
bool sync ()
 
QTextStream * textStream ()
 
void unlink ()
 

Protected Member Functions

 K3TempFile (bool)
 
bool create (const QString &filePrefix, const QString &fileExtension, int mode)
 
bool isOpen () const
 
void setError (int error)
 

Detailed Description

The K3TempFile 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 K3TempFile constructor will do that by default.

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

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

See also KStandardDirs

Deprecated:
Use KTemporaryFile instead.
Author
Waldo Bastian basti.nosp@m.an@k.nosp@m.de.or.nosp@m.g

Definition at line 55 of file k3tempfile.h.

Constructor & Destructor Documentation

K3TempFile::K3TempFile ( const QString &  filePrefix = QString(),
const QString &  fileExtension = QString(),
int  mode = 0600 
)
explicit

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

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

Parameters
filePrefixthe prefix of the file name, or QString() for the default value
fileExtensionthe extension of the prefix, or QString() for the default value
modethe file permissions

Definition at line 79 of file k3tempfile.cpp.

K3TempFile::~K3TempFile ( )

The destructor closes the file.

If autoDelete is enabled the file gets unlinked as well.

Definition at line 153 of file k3tempfile.cpp.

K3TempFile::K3TempFile ( bool  )
explicitprotected

Constructor used by KSaveFile.

Definition at line 102 of file k3tempfile.cpp.

Member Function Documentation

bool K3TempFile::close ( )

Closes the file.

See status() for details about errors.

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

Definition at line 278 of file k3tempfile.cpp.

bool K3TempFile::create ( const QString &  filePrefix,
const QString &  fileExtension,
int  mode 
)
protected

Create function used internally by K3TempFile and KSaveFile.

The file is opened by this function (and no other).

See also
K3TempFile()
isOpen()

Definition at line 115 of file k3tempfile.cpp.

QDataStream * K3TempFile::dataStream ( )

Returns a QDataStream for writing.

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

Definition at line 217 of file k3tempfile.cpp.

QFile * K3TempFile::file ( )

Returns a QFile.

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

Definition at line 195 of file k3tempfile.cpp.

FILE * K3TempFile::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 180 of file k3tempfile.cpp.

int K3TempFile::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 174 of file k3tempfile.cpp.

bool K3TempFile::isOpen ( ) const
protected
Returns
Whether the temporary file is open (for writing, or whatever.
See also
create()

Definition at line 328 of file k3tempfile.cpp.

QString K3TempFile::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 K3TempFile design attempts to prevent.

Returns
The name of the file, or QString() if opening the file has failed or the file has been unlinked already.

Definition at line 168 of file k3tempfile.cpp.

void K3TempFile::setAutoDelete ( bool  autoDelete)

Turn automatic deletion on or off.

Automatic deletion is off by default.

Parameters
autoDeletetrue to turn automatic deletion on

Definition at line 316 of file k3tempfile.cpp.

void K3TempFile::setError ( int  error)
protected

Set the error value (for subclasses).

Parameters
errorAn error number. Error numbers should come from the standard list of error numbers in errno.h .
See also
status()

Definition at line 322 of file k3tempfile.cpp.

int K3TempFile::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 162 of file k3tempfile.cpp.

bool K3TempFile::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.

Definition at line 241 of file k3tempfile.cpp.

QTextStream * K3TempFile::textStream ( )

Returns the QTextStream for writing.

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

Definition at line 207 of file k3tempfile.cpp.

void K3TempFile::unlink ( )

Unlinks the file from the directory.

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

Definition at line 227 of file k3tempfile.cpp.


The documentation for this class was generated from the following files:
  • k3tempfile.h
  • k3tempfile.cpp
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:26:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDE3Support

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal