You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
391 lines
11 KiB
391 lines
11 KiB
12 years ago
|
// tdefileio.cpp
|
||
15 years ago
|
// Author: Stefan Taferner <taferner@kde.org>
|
||
|
// License: GPL
|
||
|
|
||
|
#ifdef HAVE_CONFIG_H
|
||
|
#include <config.h>
|
||
|
#endif
|
||
|
|
||
|
#include <kmessagebox.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include <assert.h>
|
||
15 years ago
|
#include <tqdir.h>
|
||
15 years ago
|
|
||
|
#include <klocale.h>
|
||
|
#include <kstdguiitem.h>
|
||
|
|
||
15 years ago
|
#include <tqwidget.h>
|
||
|
#include <tqfile.h>
|
||
|
#include <tqfileinfo.h>
|
||
15 years ago
|
#include <sys/stat.h>
|
||
|
#include <sys/types.h>
|
||
|
|
||
13 years ago
|
#include <tdepimmacros.h>
|
||
15 years ago
|
|
||
|
namespace KPIM {
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
15 years ago
|
static void msgDialog(const TQString &msg)
|
||
15 years ago
|
{
|
||
|
KMessageBox::sorry(0, msg, i18n("File I/O Error"));
|
||
|
}
|
||
|
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
15 years ago
|
KDE_EXPORT TQCString kFileToString(const TQString &aFileName, bool aEnsureNL, bool aVerbose)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQCString result;
|
||
|
TQFileInfo info(aFileName);
|
||
15 years ago
|
unsigned int readLen;
|
||
|
unsigned int len = info.size();
|
||
15 years ago
|
TQFile file(aFileName);
|
||
15 years ago
|
|
||
|
//assert(aFileName!=0);
|
||
|
if( aFileName.isEmpty() )
|
||
|
return "";
|
||
|
|
||
|
if (!info.exists())
|
||
|
{
|
||
|
if (aVerbose)
|
||
13 years ago
|
msgDialog(i18n("The specified file does not exist:\n%1").arg(aFileName));
|
||
15 years ago
|
return TQCString();
|
||
15 years ago
|
}
|
||
|
if (info.isDir())
|
||
|
{
|
||
|
if (aVerbose)
|
||
13 years ago
|
msgDialog(i18n("This is a folder and not a file:\n%1").arg(aFileName));
|
||
15 years ago
|
return TQCString();
|
||
15 years ago
|
}
|
||
|
if (!info.isReadable())
|
||
|
{
|
||
|
if (aVerbose)
|
||
|
msgDialog(i18n("You do not have read permissions "
|
||
13 years ago
|
"to the file:\n%1").arg(aFileName));
|
||
15 years ago
|
return TQCString();
|
||
15 years ago
|
}
|
||
15 years ago
|
if (len <= 0) return TQCString();
|
||
15 years ago
|
|
||
|
if (!file.open(IO_Raw|IO_ReadOnly))
|
||
|
{
|
||
|
if (aVerbose) switch(file.status())
|
||
|
{
|
||
|
case IO_ReadError:
|
||
13 years ago
|
msgDialog(i18n("Could not read file:\n%1").arg(aFileName));
|
||
15 years ago
|
break;
|
||
|
case IO_OpenError:
|
||
13 years ago
|
msgDialog(i18n("Could not open file:\n%1").arg(aFileName));
|
||
15 years ago
|
break;
|
||
|
default:
|
||
13 years ago
|
msgDialog(i18n("Error while reading file:\n%1").arg(aFileName));
|
||
15 years ago
|
}
|
||
15 years ago
|
return TQCString();
|
||
15 years ago
|
}
|
||
|
|
||
|
result.resize(len + (int)aEnsureNL + 1);
|
||
|
readLen = file.readBlock(result.data(), len);
|
||
|
if (aEnsureNL && result[len-1]!='\n')
|
||
|
{
|
||
|
result[len++] = '\n';
|
||
|
readLen++;
|
||
|
}
|
||
|
result[len] = '\0';
|
||
|
|
||
|
if (readLen < len)
|
||
|
{
|
||
15 years ago
|
TQString msg = i18n("Could only read %1 bytes of %2.")
|
||
13 years ago
|
.arg(readLen).arg(len);
|
||
15 years ago
|
msgDialog(msg);
|
||
15 years ago
|
return TQCString();
|
||
15 years ago
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
#if 0 // unused
|
||
15 years ago
|
TQByteArray kFileToBytes(const TQString &aFileName, bool aVerbose)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQByteArray result;
|
||
|
TQFileInfo info(aFileName);
|
||
15 years ago
|
unsigned int readLen;
|
||
|
unsigned int len = info.size();
|
||
15 years ago
|
TQFile file(aFileName);
|
||
15 years ago
|
|
||
|
//assert(aFileName!=0);
|
||
|
if( aFileName.isEmpty() )
|
||
|
return result;
|
||
|
|
||
|
if (!info.exists())
|
||
|
{
|
||
|
if (aVerbose)
|
||
|
msgDialog(i18n("The specified file does not exist:\n%1")
|
||
13 years ago
|
.arg(aFileName));
|
||
15 years ago
|
return result;
|
||
|
}
|
||
|
if (info.isDir())
|
||
|
{
|
||
|
if (aVerbose)
|
||
|
msgDialog(i18n("This is a folder and not a file:\n%1")
|
||
13 years ago
|
.arg(aFileName));
|
||
15 years ago
|
return result;
|
||
|
}
|
||
|
if (!info.isReadable())
|
||
|
{
|
||
|
if (aVerbose)
|
||
|
msgDialog(i18n("You do not have read permissions "
|
||
13 years ago
|
"to the file:\n%1").arg(aFileName));
|
||
15 years ago
|
return result;
|
||
|
}
|
||
|
if (len <= 0) return result;
|
||
|
|
||
|
if (!file.open(IO_Raw|IO_ReadOnly))
|
||
|
{
|
||
|
if (aVerbose) switch(file.status())
|
||
|
{
|
||
|
case IO_ReadError:
|
||
13 years ago
|
msgDialog(i18n("Could not read file:\n%1").arg(aFileName));
|
||
15 years ago
|
break;
|
||
|
case IO_OpenError:
|
||
13 years ago
|
msgDialog(i18n("Could not open file:\n%1").arg(aFileName));
|
||
15 years ago
|
break;
|
||
|
default:
|
||
13 years ago
|
msgDialog(i18n("Error while reading file:\n%1").arg(aFileName));
|
||
15 years ago
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
result.resize(len);
|
||
|
readLen = file.readBlock(result.data(), len);
|
||
13 years ago
|
kdDebug(5300) << TQString( "len %1" ).arg(len) << endl;
|
||
15 years ago
|
|
||
|
if (readLen < len)
|
||
|
{
|
||
15 years ago
|
TQString msg;
|
||
15 years ago
|
msg = i18n("Could only read %1 bytes of %2.")
|
||
13 years ago
|
.arg(readLen).arg(len);
|
||
15 years ago
|
msgDialog(msg);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
//-----------------------------------------------------------------------------
|
||
|
KDE_EXPORT bool kBytesToFile(const char* aBuffer, int len,
|
||
15 years ago
|
const TQString &aFileName,
|
||
15 years ago
|
bool aAskIfExists, bool aBackup, bool aVerbose)
|
||
|
{
|
||
|
// TODO: use KSaveFile
|
||
15 years ago
|
TQFile file(aFileName);
|
||
15 years ago
|
int writeLen, rc;
|
||
|
|
||
|
//assert(aFileName!=0);
|
||
|
if(aFileName.isEmpty())
|
||
|
return FALSE;
|
||
|
|
||
|
if (file.exists())
|
||
|
{
|
||
|
if (aAskIfExists)
|
||
|
{
|
||
15 years ago
|
TQString str;
|
||
14 years ago
|
str = i18n("File %1 exists.\nDo you want to replace it?")
|
||
13 years ago
|
.arg(aFileName);
|
||
15 years ago
|
rc = KMessageBox::warningContinueCancel(0,
|
||
|
str, i18n("Save to File"), i18n("&Replace"));
|
||
|
if (rc != KMessageBox::Continue) return FALSE;
|
||
|
}
|
||
|
if (aBackup)
|
||
|
{
|
||
|
// make a backup copy
|
||
|
// TODO: use KSaveFile::backupFile()
|
||
15 years ago
|
TQString bakName = aFileName;
|
||
15 years ago
|
bakName += '~';
|
||
15 years ago
|
TQFile::remove(bakName);
|
||
|
if( !TQDir::current().rename(aFileName, bakName) )
|
||
15 years ago
|
{
|
||
|
// failed to rename file
|
||
|
if (!aVerbose) return FALSE;
|
||
|
rc = KMessageBox::warningContinueCancel(0,
|
||
|
i18n("Failed to make a backup copy of %1.\nContinue anyway?")
|
||
13 years ago
|
.arg(aFileName),
|
||
15 years ago
|
i18n("Save to File"), KStdGuiItem::save() );
|
||
|
if (rc != KMessageBox::Continue) return FALSE;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!file.open(IO_Raw|IO_WriteOnly|IO_Truncate))
|
||
|
{
|
||
|
if (aVerbose) switch(file.status())
|
||
|
{
|
||
|
case IO_WriteError:
|
||
13 years ago
|
msgDialog(i18n("Could not write to file:\n%1").arg(aFileName));
|
||
15 years ago
|
break;
|
||
|
case IO_OpenError:
|
||
|
msgDialog(i18n("Could not open file for writing:\n%1")
|
||
13 years ago
|
.arg(aFileName));
|
||
15 years ago
|
break;
|
||
|
default:
|
||
13 years ago
|
msgDialog(i18n("Error while writing file:\n%1").arg(aFileName));
|
||
15 years ago
|
}
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
writeLen = file.writeBlock(aBuffer, len);
|
||
|
|
||
|
if (writeLen < 0)
|
||
|
{
|
||
|
if (aVerbose)
|
||
13 years ago
|
msgDialog(i18n("Could not write to file:\n%1").arg(aFileName));
|
||
15 years ago
|
return FALSE;
|
||
|
}
|
||
|
else if (writeLen < len)
|
||
|
{
|
||
15 years ago
|
TQString msg = i18n("Could only write %1 bytes of %2.")
|
||
13 years ago
|
.arg(writeLen).arg(len);
|
||
15 years ago
|
if (aVerbose)
|
||
|
msgDialog(msg);
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
15 years ago
|
KDE_EXPORT bool kCStringToFile(const TQCString& aBuffer, const TQString &aFileName,
|
||
15 years ago
|
bool aAskIfExists, bool aBackup, bool aVerbose)
|
||
|
{
|
||
|
return kBytesToFile(aBuffer, aBuffer.length(), aFileName, aAskIfExists,
|
||
|
aBackup, aVerbose);
|
||
|
}
|
||
|
|
||
15 years ago
|
KDE_EXPORT bool kByteArrayToFile(const TQByteArray& aBuffer, const TQString &aFileName,
|
||
15 years ago
|
bool aAskIfExists, bool aBackup, bool aVerbose)
|
||
|
{
|
||
|
return kBytesToFile(aBuffer, aBuffer.size(), aFileName, aAskIfExists,
|
||
|
aBackup, aVerbose);
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQString checkAndCorrectPermissionsIfPossible( const TQString &toCheck,
|
||
15 years ago
|
const bool recursive, const bool wantItReadable,
|
||
|
const bool wantItWritable )
|
||
|
{
|
||
|
// First we have to find out which type the toCheck is. This can be
|
||
|
// a directory (follow if recursive) or a file (check permissions).
|
||
|
// Symlinks are followed as expected.
|
||
15 years ago
|
TQFileInfo fiToCheck(toCheck);
|
||
15 years ago
|
fiToCheck.setCaching(false);
|
||
15 years ago
|
TQCString toCheckEnc = TQFile::encodeName(toCheck);
|
||
|
TQString error;
|
||
15 years ago
|
struct stat statbuffer;
|
||
|
|
||
|
if ( !fiToCheck.exists() ) {
|
||
|
error.append( i18n("%1 does not exist")
|
||
13 years ago
|
.arg(toCheck) + "\n");
|
||
15 years ago
|
}
|
||
|
|
||
|
// check the access bit of a folder.
|
||
|
if ( fiToCheck.isDir() ) {
|
||
|
if ( stat( toCheckEnc,&statbuffer ) != 0 ) {
|
||
|
kdDebug() << "wantItA: Can't read perms of " << toCheck << endl;
|
||
|
}
|
||
15 years ago
|
TQDir g( toCheck );
|
||
15 years ago
|
if ( !g.isReadable() ) {
|
||
|
if ( chmod( toCheckEnc, statbuffer.st_mode + S_IXUSR ) != 0 ) {
|
||
|
error.append( i18n("%1 is not accessible and that is "
|
||
13 years ago
|
"unchangeable.").arg(toCheck) + "\n");
|
||
15 years ago
|
} else {
|
||
|
kdDebug() << "Changed access bit for " << toCheck << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
// For each file or folder we can check if the file is readable
|
||
15 years ago
|
// and writable, as requested.
|
||
|
if ( fiToCheck.isFile() || fiToCheck.isDir() ) {
|
||
|
|
||
|
if ( !fiToCheck.isReadable() && wantItReadable ) {
|
||
15 years ago
|
// Get the current permissions. No need to do anything with an
|
||
15 years ago
|
// error, it will het added to errors anyhow, later on.
|
||
|
if ( stat(toCheckEnc,&statbuffer) != 0 ) {
|
||
|
kdDebug() << "wantItR: Can't read perms of " << toCheck << endl;
|
||
|
}
|
||
|
|
||
|
// Lets try changing it.
|
||
|
if ( chmod( toCheckEnc, statbuffer.st_mode + S_IRUSR ) != 0 ) {
|
||
|
error.append( i18n("%1 is not readable and that is unchangeable.")
|
||
13 years ago
|
.arg(toCheck) + "\n");
|
||
15 years ago
|
} else {
|
||
|
kdDebug() << "Changed the read bit for " << toCheck << endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ( !fiToCheck.isWritable() && wantItWritable ) {
|
||
|
// Gets the current persmissions. Needed because it can be changed
|
||
|
// curing previous operation.
|
||
|
if (stat(toCheckEnc,&statbuffer) != 0) {
|
||
|
kdDebug() << "wantItW: Can't read perms of " << toCheck << endl;
|
||
|
}
|
||
|
|
||
|
// Lets try changing it.
|
||
|
if ( chmod (toCheckEnc, statbuffer.st_mode + S_IWUSR ) != 0 ) {
|
||
|
error.append( i18n("%1 is not writable and that is unchangeable.")
|
||
13 years ago
|
.arg(toCheck) + "\n");
|
||
15 years ago
|
} else {
|
||
|
kdDebug() << "Changed the write bit for " << toCheck << endl;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
// If it is a folder and recursive is true, then we check the contents of
|
||
15 years ago
|
// the folder.
|
||
|
if ( fiToCheck.isDir() && recursive ){
|
||
15 years ago
|
TQDir g(toCheck);
|
||
15 years ago
|
// First check if the folder is readable for us. If not, we get
|
||
|
// some ugly crashes.
|
||
|
if ( !g.isReadable() ){
|
||
13 years ago
|
error.append(i18n("Folder %1 is inaccessible.").arg(toCheck) + "\n");
|
||
15 years ago
|
} else {
|
||
14 years ago
|
const TQFileInfoList *list = g.entryInfoList();
|
||
|
TQFileInfoListIterator it( *list );
|
||
15 years ago
|
TQFileInfo *fi;
|
||
15 years ago
|
while ((fi = it.current()) != 0) {
|
||
15 years ago
|
TQString newToCheck = toCheck + "/" + fi->fileName();
|
||
|
TQFileInfo fiNewToCheck(newToCheck);
|
||
15 years ago
|
if ( fi->fileName() != "." && fi->fileName() != ".." ) {
|
||
15 years ago
|
error.append ( checkAndCorrectPermissionsIfPossible( newToCheck,
|
||
15 years ago
|
recursive, wantItReadable, wantItWritable) );
|
||
|
}
|
||
|
++it;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return error;
|
||
|
}
|
||
|
|
||
14 years ago
|
bool checkAndCorrectPermissionsIfPossibleWithErrorHandling( TQWidget *parent,
|
||
15 years ago
|
const TQString &toCheck, const bool recursive, const bool wantItReadable,
|
||
15 years ago
|
const bool wantItWritable )
|
||
|
{
|
||
15 years ago
|
TQString error = checkAndCorrectPermissionsIfPossible(toCheck, recursive,
|
||
15 years ago
|
wantItReadable, wantItWritable);
|
||
15 years ago
|
// There is no KMessageBox with Retry, Cancel and Details.
|
||
|
// so, I can't provide a functionality to recheck. So it now
|
||
15 years ago
|
// it is just a warning.
|
||
|
if ( !error.isEmpty() ) {
|
||
|
kdDebug() << "checkPermissions found:" << error << endl;
|
||
14 years ago
|
KMessageBox::detailedSorry(parent,
|
||
15 years ago
|
i18n("Some files or folders do not have "
|
||
|
"the right permissions, please correct them "
|
||
|
"manually."),
|
||
|
error, i18n("Permissions Check"), false);
|
||
|
return false;
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|