~ubuntu-branches/ubuntu/wily/almanah/wily-proposed

« back to all changes in this revision

Viewing changes to src/storage-manager.c

  • Committer: Package Import Robot
  • Author(s): Angel Abad
  • Date: 2013-12-02 13:21:08 UTC
  • mfrom: (10.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20131202132108-sfv3084bowgmzq09
Tags: 0.11.0-1
* Imported Upstream version 0.11.0
* debian/control: Update Homepage field.
* debian/patches/:
  - encrypt_database: Removed, applied upstream
  - spellchecking_error: Removed, applied upstream
  - desktop_keywords: Removed, applied upstream
* Bump Standards-Version to 3.9.5 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include <config.h>
 
21
#include <errno.h>
21
22
#include <glib.h>
22
23
#include <glib/gi18n.h>
23
24
#include <glib/gstdio.h>
525
526
}
526
527
#endif /* ENABLE_ENCRYPTION */
527
528
 
528
 
static void
529
 
back_up_file (const gchar *filename)
 
529
static gboolean
 
530
back_up_file (const gchar *filename, GError **error)
530
531
{
531
532
        GFile *original_file, *backup_file;
532
533
        gchar *backup_filename;
 
534
        gboolean retval = TRUE;
533
535
 
534
536
        /* Make a backup of the encrypted database file */
535
537
        original_file = g_file_new_for_path (filename);
537
539
        backup_file = g_file_new_for_path (backup_filename);
538
540
        g_free (backup_filename);
539
541
 
540
 
        g_file_copy_async (original_file, backup_file, G_FILE_COPY_OVERWRITE, G_PRIORITY_DEFAULT, NULL, NULL, NULL, NULL, NULL);
 
542
        if (g_file_copy (original_file, backup_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, error) == FALSE) {
 
543
                retval = FALSE;
 
544
        }
 
545
 
 
546
        /* Ensure the backup is only readable to the current user. */
 
547
        if (g_chmod (backup_filename, 0600) != 0 && errno != ENOENT) {
 
548
                g_set_error (error, ALMANAH_STORAGE_MANAGER_ERROR, ALMANAH_STORAGE_MANAGER_ERROR_CREATING_CONTEXT,
 
549
                             _("Error changing database backup file permissions: %s"),
 
550
                             g_strerror (errno));
 
551
                retval = FALSE;
 
552
        }
541
553
 
542
554
        g_object_unref (original_file);
543
555
        g_object_unref (backup_file);
 
556
 
 
557
        return retval;
544
558
}
545
559
 
546
560
gboolean
548
562
{
549
563
#ifdef ENABLE_ENCRYPTION
550
564
        struct stat encrypted_db_stat, plaintext_db_stat;
 
565
        GError *child_error = NULL;
551
566
 
552
567
        g_stat (self->priv->filename, &encrypted_db_stat);
553
568
 
 
569
        if (g_chmod (self->priv->filename, 0600) != 0 && errno != ENOENT) {
 
570
                g_set_error (error, ALMANAH_STORAGE_MANAGER_ERROR, ALMANAH_STORAGE_MANAGER_ERROR_CREATING_CONTEXT,
 
571
                             _("Error changing database file permissions: %s"),
 
572
                             g_strerror (errno));
 
573
                return FALSE;
 
574
        }
 
575
 
554
576
        /* If we're decrypting, don't bother if the cipher file doesn't exist (i.e. the database hasn't yet been created), or is empty
555
577
         * (i.e. corrupt). */
556
578
        if (g_file_test (self->priv->filename, G_FILE_TEST_IS_REGULAR) == TRUE && encrypted_db_stat.st_size > 0) {
557
 
                GError *child_error = NULL;
558
 
 
559
579
                /* Make a backup of the encrypted database file */
560
 
                back_up_file (self->priv->filename);
 
580
                back_up_file (self->priv->filename, &child_error);
 
581
                if (child_error != NULL) {
 
582
                        /* Translators: the first parameter is a filename, the second is an error message. */
 
583
                        g_warning (_("Error backing up file ‘%s’: %s"), self->priv->filename, child_error->message);
 
584
                        g_clear_error (&child_error);
 
585
                }
561
586
 
562
587
                g_stat (self->priv->plain_filename, &plaintext_db_stat);
563
588
 
580
605
        self->priv->decrypted = TRUE;
581
606
#else
582
607
        /* Make a backup of the plaintext database file */
583
 
        back_up_file (self->priv->plain_filename);
 
608
        back_up_file (self->priv->plain_filename, &child_error);
 
609
        if (child_error != NULL) {
 
610
                /* Translators: the first parameter is a filename, the second is an error message. */
 
611
                g_warning (_("Error backing up file ‘%s’: %s"), self->priv->plain_filename, child_error->message);
 
612
                g_clear_error (&child_error);
 
613
        }
584
614
        self->priv->decrypted = FALSE;
585
615
#endif /* ENABLE_ENCRYPTION */
586
616
 
592
622
                return FALSE;
593
623
        }
594
624
 
 
625
        if (g_chmod (self->priv->plain_filename, 0600) != 0 && errno != ENOENT) {
 
626
                g_set_error (error, ALMANAH_STORAGE_MANAGER_ERROR, ALMANAH_STORAGE_MANAGER_ERROR_CREATING_CONTEXT,
 
627
                             _("Error changing database file permissions: %s"),
 
628
                             g_strerror (errno));
 
629
                return FALSE;
 
630
        }
 
631
 
595
632
        /* Can't hurt to create the tables now */
596
633
        create_tables (self);
597
634