home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ktempdir.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-17  |  5.2 KB  |  173 lines

  1. /*
  2.    This file is part of the KDE libraries
  3.    Copyright (c) 2003 Joseph Wenninger <jowenn@kde.org>
  4.  
  5.    This library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License version 2 as published by the Free Software Foundation.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.    Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. #ifndef _KTEMPDIR_H_
  21. #define _KTEMPDIR_H_
  22.  
  23. #include <qstring.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include "kdelibs_export.h"
  27.  
  28. class QDir;
  29. class KTempDirPrivate;
  30.  
  31. /**
  32.  * The KTempDir class creates a unique directory for temporary use.
  33.  *
  34.  * This is especially useful if you need to create a directory in a world
  35.  * writable directory like /tmp without being vulnerable to so called
  36.  * symlink attacks.
  37.  *
  38.  * KDE applications, however, shouldn't create files or directories in /tmp in the first
  39.  * place but use the "tmp" resource instead. The standard KTempDir
  40.  * constructor will do that by default.
  41.  *
  42.  * To create a temporary directory that starts with a certain name
  43.  * in the "tmp" resource, one should use:
  44.  * KTempDir(locateLocal("tmp", prefix));
  45.  *
  46.  * KTempFile does not create any missing directories, but locateLocal() does.
  47.  *
  48.  * See also KStandardDirs
  49.  *
  50.  * @since 3.2
  51.  * @author Joseph Wenninger <jowenn@kde.org>
  52.  */
  53. class KDECORE_EXPORT KTempDir
  54. {
  55. public:
  56.    /**
  57.     * Creates a temporary directory with the name:
  58.     *  \p \<directoryPrefix\>\<six letters\>
  59.     *
  60.     * The default \p directoryPrefix is "$KDEHOME/tmp-$HOST/appname"
  61.     * @param directoryPrefix the prefix of the file name, or
  62.     *        QString::null for the default value
  63.     * @param mode the file permissions,
  64.     * almost always in octal. The first digit selects permissions for
  65.     * the user who owns the file: read (4), write (2), and execute
  66.     * (1); the second selects permissions for other users in the
  67.     * file's group, with the same values; and the fourth for other
  68.     * users not in the file's group, with the same values.
  69.     *
  70.     **/
  71.    KTempDir(QString directoryPrefix=QString::null,
  72.              int mode = 0700 );
  73.  
  74.  
  75.    /**
  76.     * The destructor deletes the directory and it's contents if autoDelete is enabled
  77.     **/
  78.    ~KTempDir();
  79.  
  80.    /**
  81.     * Turn automatic deletion on or off.
  82.     * Automatic deletion is off by default.
  83.     * @param autoDelete true to turn automatic deletion on
  84.     **/
  85.    void setAutoDelete(bool autoDelete) { bAutoDelete = autoDelete; }
  86.  
  87.    /**
  88.     * Returns the status of the directory creation  based on errno. (see errno.h)
  89.     * 0 means OK.
  90.     *
  91.     * You should check the status after object creation to check
  92.     * whether a directory could be created in the first place.
  93.     *
  94.     * @return the errno status, 0 means ok
  95.     **/
  96.    int status() const;
  97.  
  98.    /**
  99.     * Returns the full path and name of the directory, including a trailing '/'.
  100.     * @return The name of the directory, or QString::null if creating the
  101.     *         directory has failed or the directory has been unlinked
  102.     **/
  103.    QString name() const;
  104.  
  105.  
  106.    /**
  107.     * Returns the QDir* of the temporary directory.
  108.     * @return QDir directory information of the directory or 0 if their is no managed directory
  109.     * The caller has to free the pointer open for writing to the
  110.     **/
  111.    QDir *qDir();
  112.  
  113.    /**
  114.     * Deletes the directory recursively
  115.     **/
  116.    void unlink();
  117.  
  118.    /**
  119.     * @return true if a temporary directory has successfully been created and not been unlinked yet
  120.     */
  121.    bool existing() const;
  122.  
  123.    /**
  124.     * @brief Remove a directory and all its contents
  125.     *
  126.     * Remove recursively a directory, even if it is not empty
  127.     * or contains other directories.
  128.     *
  129.     * However the function works too when the @p path given
  130.     * is a non-directory file. In that case it simply remove that file.
  131.     *
  132.     * The function stops on the first error.
  133.     *
  134.     * @note This function is more meant for removing a directory
  135.     * not created by the user. For user-created directories,
  136.     * using KIO::NetAccess::del is recommended instead,
  137.     * especially as it has user feedback for long operations.
  138.     *
  139.     * @param path Path of the directory to delete
  140.     * @return true if successful, otherwise false 
  141.     * (Use errno for more details about the error.)
  142.     * @since 3.5.2
  143.     */
  144.     static bool removeDir( const QString& path );
  145.  
  146. protected:
  147.  
  148.    /**
  149.     * Creates a "random" directory with specified mode
  150.     * @param directoryPrefix to use when creating temp directory
  151.     *       (the rest is generated randomly)
  152.     * @param mode directory permissions
  153.     * @return bool true upon sucess
  154.     */
  155.    bool create(const QString &directoryPrefix,  int mode);
  156.  
  157.    /**
  158.     * Sets the errno value
  159.     * @param error the value to set the status to.
  160.     */
  161.    void setError(int error) { mError = error; }
  162.  
  163. private:
  164.    int mError;
  165.    QString mTmpName;
  166.    bool bExisting;
  167.    bool bAutoDelete;
  168.  
  169.    KTempDirPrivate *d;
  170. };
  171.  
  172. #endif
  173.