~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Core/FsTabEntry.vala

  • Committer: Tony George
  • Date: 2016-08-24 17:02:17 UTC
  • Revision ID: tony.george.kol@gmail.com-20160824170217-evbrjfbzrkjbt4x5
Moved classes to separate files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using TeeJee.Logging;
 
2
using TeeJee.FileSystem;
 
3
using TeeJee.Devices;
 
4
using TeeJee.JsonHelper;
 
5
using TeeJee.ProcessHelper;
 
6
using TeeJee.GtkHelper;
 
7
using TeeJee.System;
 
8
using TeeJee.Misc;
 
9
 
 
10
public class FsTabEntry : GLib.Object{
 
11
        public bool is_comment = false;
 
12
        public bool is_empty_line = false;
 
13
        public string device = "";
 
14
        public string mount_point = "";
 
15
        public string type = "";
 
16
        public string options = "defaults";
 
17
        public string dump = "0";
 
18
        public string pass = "0";
 
19
        public string line = "";
 
20
 
 
21
        public static Gee.ArrayList<FsTabEntry> read_fstab_file(string fstab_file_path){
 
22
                Gee.ArrayList<FsTabEntry> list = new Gee.ArrayList<FsTabEntry>();
 
23
 
 
24
                if (!file_exists(fstab_file_path)){ return list; }
 
25
 
 
26
                string text = file_read(fstab_file_path);
 
27
                string[] lines = text.split("\n");
 
28
                foreach(string line in lines){
 
29
                        FsTabEntry entry = new FsTabEntry();
 
30
                        list.add(entry);
 
31
 
 
32
                        entry.is_comment = line.strip().has_prefix("#");
 
33
                        entry.is_empty_line = (line.strip().length == 0);
 
34
 
 
35
                        if (entry.is_comment){
 
36
                                entry.line = line;
 
37
                        }
 
38
                        else if (entry.is_empty_line){
 
39
                                entry.line = "";
 
40
                        }
 
41
                        else{
 
42
                                entry.line = line;
 
43
 
 
44
                                string[] parts = line.replace("\t"," ").split(" ");
 
45
                                int part_num = -1;
 
46
                                foreach(string part in parts){
 
47
                                        if (part.strip().length == 0) { continue; }
 
48
                                        switch (++part_num){
 
49
                                                case 0:
 
50
                                                        entry.device = part.strip();
 
51
                                                        break;
 
52
                                                case 1:
 
53
                                                        entry.mount_point = part.strip();
 
54
                                                        break;
 
55
                                                case 2:
 
56
                                                        entry.type = part.strip();
 
57
                                                        break;
 
58
                                                case 3:
 
59
                                                        entry.options = part.strip();
 
60
                                                        break;
 
61
                                                case 4:
 
62
                                                        entry.dump = part.strip();
 
63
                                                        break;
 
64
                                                case 5:
 
65
                                                        entry.pass = part.strip();
 
66
                                                        break;
 
67
                                        }
 
68
                                }
 
69
                        }
 
70
                }
 
71
 
 
72
                return list;
 
73
        }
 
74
 
 
75
        public static string create_fstab_file(FsTabEntry[] fstab_entries, bool keep_comments_and_empty_lines = true){
 
76
                string text = "";
 
77
                foreach(FsTabEntry entry in fstab_entries){
 
78
                        if (entry.is_comment || entry.is_empty_line){
 
79
                                if (keep_comments_and_empty_lines){
 
80
                                        text += "%s\n".printf(entry.line);
 
81
                                }
 
82
                        }
 
83
                        else {
 
84
                                text += "%s\t%s\t%s\t%s\t%s\t%s\n".printf(entry.device, entry.mount_point, entry.type, entry.options, entry.dump, entry.pass);
 
85
                        }
 
86
                }
 
87
                return text;
 
88
        }
 
89
}