~ubuntu-branches/ubuntu/saucy/deja-dup/saucy

« back to all changes in this revision

Viewing changes to common/CommonUtils.vala

  • Committer: Package Import Robot
  • Author(s): Michael Terry
  • Date: 2013-02-04 15:12:32 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20130204151232-3xr9quxod9pltlro
Tags: 25.5-0ubuntu1
* New upstream release
  - Use same partition for temp files as include files (to avoid problems
    with tiny /tmp partitions like tmpfs).  LP: #1100092
  - Tell GNOME 3 about notifications, so the user can disable them
  - Always exclude /run (in case user includes /)
* debian/rules:
  - Be explicit about what features we turn on (ccpanel, nautilus, unity)
* debian/control:
  - Require duplicity 0.6.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
276
276
  return (last_check.compare(now) <= 0);
277
277
}
278
278
 
279
 
public string get_folder_key(SimpleSettings settings, string key)
 
279
public string get_folder_key(FilteredSettings settings, string key)
280
280
{
281
281
  string folder = settings.get_string(key);
282
282
  if (folder.contains("$HOSTNAME")) {
289
289
}
290
290
 
291
291
bool settings_read_only = false;
292
 
HashTable<string, SimpleSettings> settings_table = null;
 
292
HashTable<string, FilteredSettings> settings_table = null;
293
293
public void set_settings_read_only(bool ro)
294
294
{
295
295
  settings_read_only = ro;
297
297
    // When read only, we also need to make sure everyone shares the same
298
298
    // settings object.  Otherwise, they will not notice the changes other
299
299
    // parts of the code make.
300
 
    settings_table = new HashTable<string, SimpleSettings>.full(str_hash,
 
300
    settings_table = new HashTable<string, FilteredSettings>.full(str_hash,
301
301
                                                                str_equal,
302
302
                                                                g_free,
303
303
                                                                g_object_unref);
307
307
  }
308
308
}
309
309
 
310
 
public SimpleSettings get_settings(string? subdir = null)
 
310
public FilteredSettings get_settings(string? subdir = null)
311
311
{
312
312
  string schema = "org.gnome.DejaDup";
313
313
  if (subdir != null && subdir != "")
314
314
    schema += "." + subdir;
315
 
  SimpleSettings rv;
 
315
  FilteredSettings rv;
316
316
  if (settings_read_only) {
317
317
    rv = settings_table.lookup(schema);
318
318
    if (rv == null) {
319
 
      rv = new SimpleSettings(schema, true);
 
319
      rv = new FilteredSettings(schema, true);
320
320
      rv.delay(); // never to be apply()'d again
321
321
      settings_table.insert(schema, rv);
322
322
    }
323
323
  }
324
324
  else {
325
 
    rv = new SimpleSettings(schema, false);
 
325
    rv = new FilteredSettings(schema, false);
326
326
  }
327
327
  return rv;
328
328
}
383
383
  var unused_backend = DejaDup.Backend.get_default();
384
384
  unused_backend = null;
385
385
 
 
386
  // And cleanup from any previous runs
 
387
  clean_tempdirs.begin();
 
388
 
386
389
  return true;
387
390
}
388
391
 
531
534
                           "type", Secret.SchemaAttributeType.STRING);
532
535
}
533
536
 
 
537
public bool ensure_directory_exists(string path)
 
538
{
 
539
  var gfile = File.new_for_path(path);
 
540
  try {
 
541
    if (gfile.make_directory_with_parents())
 
542
      return true;
 
543
  }
 
544
  catch (IOError.EXISTS e) {
 
545
    return true; // ignore
 
546
  }
 
547
  catch (Error e) {
 
548
    warning("%s\n", e.message);
 
549
  }
 
550
  return false;
 
551
}
 
552
 
 
553
// By default, duplicity uses normal tmp folders like /tmp to store its
 
554
// in-process files.  These can get quite large, especially when restoring.
 
555
// You may need up to twice the size of the largest source file.
 
556
// Because /tmp may not be super large, especially on systems that use
 
557
// tmpfs by default (e.g. Fedora 18), we try to use a tempdir that is on
 
558
// the same partition as the source files.
 
559
public async string get_tempdir()
 
560
{
 
561
  var tempdirs = get_tempdirs();
 
562
 
 
563
  // First, decide the "main include".  Assume that if $HOME
 
564
  // is present, that is it.  Else, the first include we find decides it.
 
565
  // This is admittedly fast and loose, but our primary concern is just
 
566
  // avoiding silly choices like tmpfs or tiny special /tmp partitions.
 
567
  var settings = get_settings();
 
568
  var include_list = settings.get_file_list(INCLUDE_LIST_KEY);
 
569
  File main_include = null;
 
570
  var home = File.new_for_path(Environment.get_home_dir());
 
571
  foreach (var include in include_list) {
 
572
    if (include.equal(home)) {
 
573
      main_include = include;
 
574
      break;
 
575
    }
 
576
    else if (main_include == null)
 
577
      main_include = include;
 
578
  }
 
579
  if (main_include == null)
 
580
    return tempdirs[0];
 
581
 
 
582
  // Grab that include's fs ID
 
583
  string filesystem_id;
 
584
  try {
 
585
    var info = yield main_include.query_info_async(FileAttribute.ID_FILESYSTEM,
 
586
                                                   FileQueryInfoFlags.NONE);
 
587
    filesystem_id = info.get_attribute_string(FileAttribute.ID_FILESYSTEM);
 
588
  }
 
589
  catch (Error e) {
 
590
    return tempdirs[0];
 
591
  }
 
592
 
 
593
  // Then, see which of our possible tempdirs matches that partition.
 
594
  foreach (var tempdir in tempdirs) {
 
595
    string temp_id;
 
596
    ensure_directory_exists(tempdir);
 
597
    try {
 
598
      var gfile = File.new_for_path(tempdir);
 
599
      var info = yield gfile.query_info_async(FileAttribute.ID_FILESYSTEM,
 
600
                                              FileQueryInfoFlags.NONE);
 
601
      temp_id = info.get_attribute_string(FileAttribute.ID_FILESYSTEM);
 
602
    }
 
603
    catch (Error e) {
 
604
      continue;
 
605
    }
 
606
    if (temp_id == filesystem_id)
 
607
      return tempdir;
 
608
  }
 
609
 
 
610
  // Fallback to simply using the highest preferred tempdir
 
611
  return tempdirs[0];
 
612
}
 
613
 
 
614
public string[] get_tempdirs()
 
615
{
 
616
  var tempdir = Environment.get_variable("DEJA_DUP_TEMPDIR");
 
617
  if (tempdir != null && tempdir != "")
 
618
    return {tempdir};
 
619
  // Prefer directories that have their own cleanup logic in case ours isn't
 
620
  // run for a while.  (e.g. /tmp every boot, /var/tmp every now and then)
 
621
  return {Environment.get_tmp_dir(), "/var/tmp",
 
622
          Path.build_filename(Environment.get_user_cache_dir(), Config.PACKAGE,
 
623
                              "tmp")};
 
624
}
 
625
 
 
626
public async void clean_tempdirs()
 
627
{
 
628
  var tempdirs = get_tempdirs();
 
629
  const int NUM_ENUMERATED = 16;
 
630
  foreach (var tempdir in tempdirs) {
 
631
    var gfile = File.new_for_path(tempdir);
 
632
 
 
633
    // Now try to find and delete all files that start with "duplicity-"
 
634
    try {
 
635
      var enumerator = yield gfile.enumerate_children_async(
 
636
                         FileAttribute.STANDARD_NAME,
 
637
                         FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
 
638
                         Priority.DEFAULT, null);
 
639
      while (true) {
 
640
        var infos = yield enumerator.next_files_async(NUM_ENUMERATED,
 
641
                                                      Priority.DEFAULT, null);
 
642
        foreach (FileInfo info in infos) {
 
643
          if (info.get_name().has_prefix("duplicity-")) {
 
644
            var child = gfile.get_child(info.get_name());
 
645
            yield new DejaDup.RecursiveDelete(child).start_async();
 
646
          }
 
647
        }
 
648
        if (infos.length() != NUM_ENUMERATED)
 
649
          break;
 
650
      }
 
651
    }
 
652
    catch (Error e) {
 
653
      // No worries
 
654
    }
 
655
  }
 
656
}
 
657
 
534
658
} // end namespace
535
659