\section{KTemp\-File Class Reference}
\label{classKTempFile}\index{KTempFile@{KTempFile}}
The KTemp\-File class creates and opens a unique file for temporary use.  


{\tt \#include $<$ktempfile.h$>$}

Collaboration diagram for KTemp\-File:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=190pt]{classKTempFile__coll__graph}
\end{center}
\end{figure}
\subsection*{Public Member Functions}
\begin{CompactItemize}
\item 
{\bf KTemp\-File} ({\bf QString} file\-Prefix=QString::null, {\bf QString} file\-Extension=QString::null, int mode=0600)
\item 
{\bf $\sim$KTemp\-File} ()
\item 
void {\bf set\-Auto\-Delete} (bool auto\-Delete)
\item 
int {\bf status} () const 
\item 
{\bf QString} {\bf name} () const 
\item 
int {\bf handle} () const 
\item 
FILE $\ast$ {\bf fstream} ()
\item 
{\bf QText\-Stream} $\ast$ {\bf text\-Stream} ()
\item 
{\bf QData\-Stream} $\ast$ {\bf data\-Stream} ()
\item 
{\bf QFile} $\ast$ {\bf file} ()
\item 
void {\bf unlink} ()
\item 
bool {\bf sync} ()
\item 
bool {\bf close} ()
\end{CompactItemize}
\subsection*{Protected Member Functions}
\begin{CompactItemize}
\item 
{\bf KTemp\-File} (bool)
\item 
bool {\bf create} (const {\bf QString} \&file\-Prefix, const {\bf QString} \&file\-Extension, int mode)\label{classKTempFile_b1}

\item 
void {\bf set\-Error} (int error)\label{classKTempFile_b2}

\end{CompactItemize}
\subsection*{Friends}
\begin{CompactItemize}
\item 
class {\bf KSave\-File}\label{classKTempFile_n0}

\end{CompactItemize}


\subsection{Detailed Description}
The KTemp\-File 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.

{\bf KDE}{\rm (p.\,\pageref{namespaceKDE})} applications, however, shouldn't create files in /tmp in the first place but use the \char`\"{}tmp\char`\"{} resource instead. The standard KTemp\-File constructor will do that by default.

To create a temporary file that starts with a certain name in the \char`\"{}tmp\char`\"{} resource, one should use: KTemp\-File(locate\-Local(\char`\"{}tmp\char`\"{}, prefix), extension);

KTemp\-File does not create any missing directories, but locate\-Local() does.

See also {\bf KStandard\-Dirs}{\rm (p.\,\pageref{classKStandardDirs})}

\begin{Desc}
\item[Author:]Waldo Bastian $<${\tt bastian@kde.org}$>$ \end{Desc}




Definition at line 55 of file ktempfile.h.

\subsection{Constructor \& Destructor Documentation}
\index{KTempFile@{KTemp\-File}!KTempFile@{KTempFile}}
\index{KTempFile@{KTempFile}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KTemp\-File::KTemp\-File ({\bf QString} {\em file\-Prefix} = {\tt QString::null}, {\bf QString} {\em file\-Extension} = {\tt QString::null}, int {\em mode} = {\tt 0600})}\label{classKTempFile_a0}


Creates a temporary file with the name: $<$file\-Prefix$>$$<$six letters$>$$<$file\-Extension$>$. 

The default {\tt file\-Prefix} is \char`\"{}\$KDEHOME/tmp-\$HOST/appname/\char`\"{} The default {\tt file\-Extension} is \char`\"{}.tmp\char`\"{} \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em file\-Prefix}]the prefix of the file name, or QString::null for the default value \item[{\em file\-Extension}]the extension of the prefix, or QString::null for the default value \item[{\em mode}]the file permissions \end{description}
\end{Desc}


Definition at line 60 of file ktempfile.cpp.

References QString::is\-Empty().\index{KTempFile@{KTemp\-File}!~KTempFile@{$\sim$KTempFile}}
\index{~KTempFile@{$\sim$KTempFile}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KTemp\-File::$\sim${\bf KTemp\-File} ()}\label{classKTempFile_a1}


The destructor closes the file. 

If auto\-Delete is enabled the file gets unlinked as well. 

Definition at line 129 of file ktempfile.cpp.

References close(), and unlink().\index{KTempFile@{KTemp\-File}!KTempFile@{KTempFile}}
\index{KTempFile@{KTempFile}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KTemp\-File::KTemp\-File (bool)\hspace{0.3cm}{\tt  [protected]}}\label{classKTempFile_b0}


Constructor used by {\bf KSave\-File}{\rm (p.\,\pageref{classKSaveFile})}. 



Definition at line 79 of file ktempfile.cpp.

\subsection{Member Function Documentation}
\index{KTempFile@{KTemp\-File}!setAutoDelete@{setAutoDelete}}
\index{setAutoDelete@{setAutoDelete}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KTemp\-File::set\-Auto\-Delete (bool {\em auto\-Delete})\hspace{0.3cm}{\tt  [inline]}}\label{classKTempFile_a2}


Turn automatic deletion on or off. 

Automatic deletion is off by default. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em auto\-Delete}]true to turn automatic deletion on \end{description}
\end{Desc}


Definition at line 87 of file ktempfile.h.\index{KTempFile@{KTemp\-File}!status@{status}}
\index{status@{status}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KTemp\-File::status () const}\label{classKTempFile_a3}


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. \begin{Desc}
\item[Returns:]the errno status, 0 means ok \end{Desc}


Definition at line 137 of file ktempfile.cpp.\index{KTempFile@{KTemp\-File}!name@{name}}
\index{name@{name}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf QString} KTemp\-File::name () const}\label{classKTempFile_a4}


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 KTemp\-File design attempts to prevent.

\begin{Desc}
\item[Returns:]The name of the file, or QString::null if opening the file has failed or the file has been unlinked already. \end{Desc}


Definition at line 143 of file ktempfile.cpp.

Referenced by KSave\-File::close(), and file().\index{KTempFile@{KTemp\-File}!handle@{handle}}
\index{handle@{handle}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KTemp\-File::handle () const}\label{classKTempFile_a5}


An integer file descriptor open for writing to the file. 

\begin{Desc}
\item[Returns:]The file descriptor, or a negative number if opening the file failed \end{Desc}


Definition at line 149 of file ktempfile.cpp.

Referenced by KSave\-File::close(), and KSave\-File::KSave\-File().\index{KTempFile@{KTemp\-File}!fstream@{fstream}}
\index{fstream@{fstream}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}FILE $\ast$ KTemp\-File::fstream ()}\label{classKTempFile_a6}


Returns the FILE$\ast$ of the temporary file. 

\begin{Desc}
\item[Returns:]FILE$\ast$ stream open for writing to the file, or 0 if opening the file failed \end{Desc}


Definition at line 155 of file ktempfile.cpp.

References QString::latin1().

Referenced by file().\index{KTempFile@{KTemp\-File}!textStream@{textStream}}
\index{textStream@{textStream}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf QText\-Stream} $\ast$ KTemp\-File::text\-Stream ()}\label{classKTempFile_a7}


Returns the {\bf QText\-Stream} for writing. 

\begin{Desc}
\item[Returns:]{\bf QText\-Stream} open for writing to the file, or 0 if opening the file failed \end{Desc}


Definition at line 182 of file ktempfile.cpp.

References file().\index{KTempFile@{KTemp\-File}!dataStream@{dataStream}}
\index{dataStream@{dataStream}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf QData\-Stream} $\ast$ KTemp\-File::data\-Stream ()}\label{classKTempFile_a8}


Returns a {\bf QData\-Stream} for writing. 

\begin{Desc}
\item[Returns:]{\bf QData\-Stream} open for writing to the file, or 0 if opening the file failed \end{Desc}


Definition at line 192 of file ktempfile.cpp.

References file().\index{KTempFile@{KTemp\-File}!file@{file}}
\index{file@{file}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf QFile} $\ast$ KTemp\-File::file ()}\label{classKTempFile_a9}


Returns a {\bf QFile}. 

\begin{Desc}
\item[Returns:]A {\bf QFile} open for writing to the file, or 0 if opening the file failed. \end{Desc}


Definition at line 170 of file ktempfile.cpp.

References fstream(), name(), QFile::open(), and QFile::set\-Name().

Referenced by data\-Stream(), and text\-Stream().\index{KTempFile@{KTemp\-File}!unlink@{unlink}}
\index{unlink@{unlink}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KTemp\-File::unlink ()}\label{classKTempFile_a10}


Unlinks the file from the directory. 

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

Definition at line 202 of file ktempfile.cpp.

References QString::is\-Empty(), and QFile::remove().

Referenced by KSave\-File::abort(), KSave\-File::close(), and $\sim$KTemp\-File().\index{KTempFile@{KTemp\-File}!sync@{sync}}
\index{sync@{sync}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}bool KTemp\-File::sync ()}\label{classKTempFile_a11}


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 {\bf sync()}{\rm (p.\,\pageref{classKTempFile_a11})}.

See {\bf status()}{\rm (p.\,\pageref{classKTempFile_a3})} for details about errors. \begin{Desc}
\item[Returns:]true if successful, or false if an error has occurred. \end{Desc}
\begin{Desc}
\item[Since:]3.3 \end{Desc}


Definition at line 216 of file ktempfile.cpp.

References QString::latin1().

Referenced by KSave\-File::close().\index{KTempFile@{KTemp\-File}!close@{close}}
\index{close@{close}!KTempFile@{KTemp\-File}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}bool KTemp\-File::close ()}\label{classKTempFile_a12}


Closes the file. 

See {\bf status()}{\rm (p.\,\pageref{classKTempFile_a3})} for details about errors. \begin{Desc}
\item[Returns:]true if successful, or false if an error has occurred. \end{Desc}


Definition at line 250 of file ktempfile.cpp.

References QString::latin1().

Referenced by KSave\-File::abort(), KSave\-File::close(), and $\sim$KTemp\-File().

The documentation for this class was generated from the following files:\begin{CompactItemize}
\item 
ktempfile.h\item 
ktempfile.cpp\end{CompactItemize}
