[glom] ImageGlom: Save temp files as read-only to avoid unexpected data loss.



commit 4024676573b5e199208564d3dae50b3c9dc349b9
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Jul 15 12:21:34 2011 +0200

    ImageGlom: Save temp files as read-only to avoid unexpected data loss.
    
    * glom/utility_widgets/imageglom.cc: save_temp_file(): Set the file
    permissions.

 ChangeLog                         |    7 +++
 glom/utility_widgets/imageglom.cc |   87 +++++++++++++++++++++++++++++++++++-
 2 files changed, 91 insertions(+), 3 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 9d11d34..65f8662 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2011-07-15  Murray Cumming  <murrayc murrayc com>
 
+	ImageGlom: Save temp files as read-only to avoid unexpected data loss.
+
+	* glom/utility_widgets/imageglom.cc: save_temp_file(): Set the file 
+	permissions.
+
+2011-07-15  Murray Cumming  <murrayc murrayc com>
+
 	Avoid autoconf warning about datarootdir.
 
 	* glom/libglom/glom.pc.in: Set datarootdir to avoid this warning:
diff --git a/glom/utility_widgets/imageglom.cc b/glom/utility_widgets/imageglom.cc
index 3ab4ca9..86f545e 100644
--- a/glom/utility_widgets/imageglom.cc
+++ b/glom/utility_widgets/imageglom.cc
@@ -1,6 +1,6 @@
 /* Glom
  *
- * Copyright (C) 2001-2004 Murray Cumming
+ * Copyright (C) 2001-2011 Murray Cumming
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -482,14 +482,87 @@ void ImageGlom::on_menupopup_activate_open_file_with()
   open_with(app_info);
 }
 
+static void make_file_read_only(const Glib::ustring& uri)
+{
+  std::string filepath;
+
+  try
+  {
+    filepath = Glib::filename_from_uri(uri);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << "Exception: " << ex.what() << std::endl;
+    return;
+  }
+  
+  if(filepath.empty())
+  {
+    std::cerr << G_STRFUNC << ": filepath is empty." << std::endl;
+  }
+  
+  const int result = chmod(filepath.c_str(), S_IRUSR);
+  if(result != 0)
+  {
+    std::cerr << G_STRFUNC << ": chmod() failed." << std::endl;
+  }
+  
+  //Setting the attribute via gio gives us this exception:
+  //"Setting attribute access::can-write not supported"
+  /*
+  Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(uri);
+
+  Glib::RefPtr<Gio::FileInfo> file_info;
+
+  try
+  {
+    file_info = file->query_info(G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": query_info() failed: " << ex.what() << std::endl;
+    return;
+  }
+  
+  if(!file_info)
+  {
+    std::cerr << ": file_info is null" << std::endl;
+    return;
+  }
+  
+  const bool can_write =
+    file_info->get_attribute_boolean(G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
+  if(!can_write)
+    return;
+    
+  file_info->set_attribute_boolean(G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, false);
+  
+  try
+  {
+    file->set_attributes_from_info(file_info);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": set_attributes_from_info() failed: " << ex.what() << std::endl;
+  }
+  */
+}
+
 Glib::ustring ImageGlom::save_to_temp_file(bool show_progress)
 {
   Glib::ustring uri;
   
   //Get a temporary file path:
   std::string filepath;
-  const int filehandle = Glib::file_open_tmp(filepath);
-  ::close(filehandle);
+  try
+  {
+    const int filehandle = Glib::file_open_tmp(filepath);
+    ::close(filehandle);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() failed" << std::endl;
+  }
   
   if(filepath.empty())
   {
@@ -510,6 +583,13 @@ Glib::ustring ImageGlom::save_to_temp_file(bool show_progress)
     uri = Glib::ustring();
     std::cerr << G_STRFUNC << ": save_file() failed." << std::endl;
   }
+  else
+  {
+    //Don't let people easily edit the saved file,
+    //because they would lose data when it is automatically deleted later.
+    //Also they might think that editing it will change it in the database.
+    make_file_read_only(uri);
+  }
 
   return uri;
 }
@@ -647,6 +727,7 @@ bool ImageGlom::save_file(const Glib::ustring& uri)
   dialog_save->save(uri);
 
   dialog_save->run();
+
   return true;
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]