[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/graphics/digikam/utilities/imageeditor/editor
From:       Marcel Wiesweg <marcel.wiesweg () gmx ! de>
Date:       2006-04-30 20:21:21
Message-ID: 1146428481.152370.29934.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 535964 by mwiesweg:

Restore file permissions when overwriting a file.

We might as well want to copy permissions when a file is newly created in saveAs,
thats a different issue.


 M  +44 -8     editorwindow.cpp  
 M  +1 -0      editorwindow.h  


--- trunk/extragear/graphics/digikam/utilities/imageeditor/editor/editorwindow.cpp \
#535963:535964 @@ -18,6 +18,11 @@
  *
  * ============================================================ */
 
+// C includes
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
 // Qt includes.
 
 #include <qlabel.h>
@@ -1173,11 +1178,8 @@
         
         kdDebug() << "renaming to " << m_savingContext->destinationURL.path() << \
endl;  
-        if (::rename(QFile::encodeName(m_savingContext->saveTempFile->name()),
-              QFile::encodeName(m_savingContext->destinationURL.path())) != 0)
+        if (!moveFile())
         {
-            KMessageBox::error(this, i18n("Failed to overwrite original file"),
-                               i18n("Error Saving File"));
             finishSaving(false);
             return;
         }
@@ -1228,11 +1230,8 @@
         
         kdDebug() << "renaming to " << m_savingContext->destinationURL.path() << \
endl;  
-        if (::rename(QFile::encodeName(m_savingContext->saveTempFile->name()),
-              QFile::encodeName(m_savingContext->destinationURL.path())) != 0)
+        if (!moveFile())
         {
-            KMessageBox::error(this, i18n("Failed to save to new file"),
-                               i18n("Error Saving File"));
             finishSaving(false);
             return;
         }
@@ -1288,6 +1287,7 @@
 
     m_savingContext->srcURL         = url;
     m_savingContext->destinationURL = m_savingContext->srcURL;
+    m_savingContext->destinationExisted = true;
     m_savingContext->savingState    = SavingContextContainer::SavingStateSave;
     m_savingContext->saveTempFile   = new \
KTempFile(m_savingContext->srcURL.directory(false), QString::null);  \
m_savingContext->saveTempFile->setAutoDelete(true); @@ -1453,7 +1453,43 @@
     return true;
 }
 
+bool EditorWindow::moveFile()
+{
+    QCString dstFileName = \
QFile::encodeName(m_savingContext->destinationURL.path());  
+    // store old permissions
+    mode_t filePermissions = S_IREAD | S_IWRITE;
+    if (m_savingContext->destinationExisted)
+    {
+        struct stat stbuf;
+        if (::stat(dstFileName, &stbuf) == 0)
+        {
+            filePermissions = stbuf.st_mode;
+        }
+    }
+
+    // rename tmp file to dest
+    if (::rename(QFile::encodeName(m_savingContext->saveTempFile->name()), \
dstFileName) != 0) +    {
+        KMessageBox::error(this, i18n("Failed to overwrite original file"),
+                           i18n("Error Saving File"));
+        return false;
+    }
+
+    // restore permissions
+    if (m_savingContext->destinationExisted)
+    {
+        if (::chmod(dstFileName, filePermissions) != 0)
+        {
+            kdWarning() << "Failed to restore file permissions for file " << \
dstFileName << endl; +        }
+    }
+
+    return true;
+}
+
+
+
 }  // namespace Digikam
 
 #include "editorwindow.moc"
--- trunk/extragear/graphics/digikam/utilities/imageeditor/editor/editorwindow.h \
#535963:535964 @@ -128,6 +128,7 @@
     void startingSave(const KURL& url);
     bool startingSaveAs(const KURL& url);
     bool checkPermissions(const KURL& url);
+    bool moveFile();
 
     virtual void finishSaving(bool success);
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic