35#include <tqdatetime.h>
39#include "tdeapplication.h"
41#include "tdestandarddirs.h"
52 if (!checkAccess(real_filename, W_OK))
54 mTempFile.setError(EACCES);
58 if (mTempFile.create(real_filename, TQString::fromLatin1(
".new"), mode))
60 mFileName = real_filename;
65 KDE_struct_stat stat_buf;
66 if (KDE_stat(TQFile::encodeName(real_filename), &stat_buf)==0)
69 if (stat_buf.st_uid == getuid())
71 bool changePermission =
true;
72 if (stat_buf.st_gid != getgid())
74 if (fchown(mTempFile.
handle(), (uid_t) -1, stat_buf.st_gid) != 0)
77 changePermission =
false;
81 fchmod(mTempFile.
handle(), stat_buf.st_mode);
109 if (mTempFile.
name().isEmpty() || mTempFile.
handle()==-1)
111 if (!mTempFile.
sync())
116 if (mTempFile.
close())
118 if (0==KDE_rename(TQFile::encodeName(mTempFile.
name()), TQFile::encodeName(mFileName)))
120 mTempFile.setError(errno);
128write_all(
int fd,
const char *buf,
size_t len)
132 int written = write(fd, buf, len);
146 const TQString& backupExtension)
148 TQCString cFilename = TQFile::encodeName(qFilename);
149 const char *filename = cFilename.data();
151 int fd = KDE_open( filename, O_RDONLY );
155 KDE_struct_stat buff;
156 if ( KDE_fstat( fd, &buff) < 0 )
163 if ( backupDir.isEmpty() )
168 int slash = cFilename.findRev(
'/');
170 nameOnly = cFilename;
172 nameOnly = cFilename.mid(slash + 1);
173 cBackup = TQFile::encodeName(backupDir);
174 if ( backupDir[backupDir.length()-1] != (TQChar)
'/' )
178 cBackup += TQFile::encodeName(backupExtension);
179 const char *backup = cBackup.data();
180 int permissions = buff.st_mode & 07777;
182 if ( KDE_stat( backup, &buff) == 0)
184 if ( unlink( backup ) != 0 )
191 mode_t old_umask = umask(0);
192 int fd2 = KDE_open( backup, O_WRONLY | O_CREAT | O_EXCL, permissions | S_IWUSR);
201 char buffer[ 32*1024 ];
205 int n = ::read( fd, buffer, 32*1024 );
217 if (write_all( fd2, buffer, n))
TQString name() const
The name of the file as passed to the constructor.
~KSaveFile()
The destructor closes the file.
KSaveFile(const TQString &filename, int mode=0666)
Creates a new KSaveFile with the given file name.
bool close()
Closes the file and makes the changes definitive.
void abort()
Aborts the write operation and removes any intermediate files This implies a close.
static bool backupFile(const TQString &filename, const TQString &backupDir=TQString::null, const TQString &backupExtension=TQString::fromLatin1("~"))
Static method to create a backup file before saving.
int handle() const
An integer file descriptor open for writing to the file.
bool close()
Closes the file.
bool sync()
Flushes file to disk (fsync).
void unlink()
Unlinks the file from the directory.
TQString name() const
Returns the full path and name of the file.
static TQString realFilePath(const TQString &filename)
Expands all symbolic links and resolves references to '/.