# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# KTempDir Class Reference

The KTempDir class creates a unique directory for temporary use. More...

`#include <ktempdir.h>`

Collaboration diagram for KTempDir:

[legend]
List of all members.

## Public Member Functions

| KTempDir (QString directoryPrefix=QString::null, int mode=0700)
| ~KTempDir ()
void | setAutoDelete (bool autoDelete)
int | status () const
QString | name () const
QDir * | qDir ()
void | unlink ()
bool | existing () const

## Protected Member Functions

bool | create (const QString &directoryPrefix, int mode)
void | setError (int error)

## Detailed Description

The KTempDir class creates a unique directory for temporary use.

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

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

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

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

See also KStandardDirs

Since:
  3.2

Author:
  Joseph Wenninger <jowenn@kde.org>

Definition at line 53 of file ktempdir.h.

## Constructor & Destructor Documentation

KTempDir::KTempDir | ( | QString | directoryPrefix = `QString::null`,
|  | int | mode = `0700`
| ) |

Creates a temporary directory with the name: `<directoryPrefix><six` letters>.

The default `directoryPrefix` is "$KDEHOME/tmp-$HOST/appname"

Parameters:

  | directoryPrefix | the prefix of the file name, or QString::null for the default value
  | mode | the file permissions, almost always in octal. The first digit selects permissions for the user who owns the file: read (4), write (2), and execute (1); the second selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values.

Definition at line 57 of file ktempdir.cpp.

References create(), and QString::isEmpty().

KTempDir::~KTempDir | ( |  | ) |

| The destructor deletes the directory and it's contents if autoDelete is enabled. Definition at line 104 of file ktempdir.cpp. References unlink().

## Member Function Documentation

void KTempDir::setAutoDelete | ( | bool | autoDelete | ) | `[inline]`

Turn automatic deletion on or off.

Automatic deletion is off by default.

Parameters:

  | autoDelete | true to turn automatic deletion on

Definition at line 85 of file ktempdir.h.

int KTempDir::status | ( |  | ) | const

Returns the status of the directory creation based on errno.

(see errno.h) 0 means OK.

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

Returns:
  the errno status, 0 means ok

Definition at line 111 of file ktempdir.cpp.

QString KTempDir::name | ( |  | ) | const

Returns the full path and name of the directory, including a trailing '/'.

Returns:
  The name of the directory, or QString::null if creating the directory has failed or the directory has been unlinked

Definition at line 117 of file ktempdir.cpp.

QDir * KTempDir::qDir | ( |  | ) |

Returns the QDir* of the temporary directory.

Returns:
  QDir directory information of the directory or 0 if their is no managed directory The caller has to free the pointer open for writing to the

Definition at line 129 of file ktempdir.cpp.

void KTempDir::unlink | ( |  | ) |

| Deletes the directory recursively. Definition at line 136 of file ktempdir.cpp. References QFile::encodeName(), and KProcess::quote(). Referenced by ~KTempDir().

bool KTempDir::existing | ( |  | ) | const

Returns:
  true if a temporary directory has successfully been created and not been unlinked yet

Definition at line 123 of file ktempdir.cpp.

bool KTempDir::create | ( | const QString & | directoryPrefix,
|  | int | mode
| ) | `[protected]`

Creates a "random" directory with specified mode.

Parameters:

  | directoryPrefix | to use when creating temp directory (the rest is generated randomly)
  | mode | directory permissions

Returns:
  bool true upon sucess

Definition at line 70 of file ktempdir.cpp.

References QFile::decodeName(), QFile::encodeName(), endl(), and KApplication::random().

Referenced by KTempDir().

void KTempDir::setError | ( | int | error | ) | `[inline, protected]`

Sets the errno value.

Parameters:

  | error | the value to set the status to.

Definition at line 138 of file ktempdir.h.

The documentation for this class was generated from the following files:

- ktempdir.h
- ktempdir.cpp

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:19:08 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003