~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/AppLock.vala

  • Committer: Tony George
  • Date: 2016-10-16 13:42:12 UTC
  • Revision ID: tony.george.kol@gmail.com-20161016134212-ibjsptmgbha7a2mw
Fixed scheduled backups; Initialize display for the scheduled cron task;

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * AppLock.vala
 
4
 *
 
5
 * Copyright 2016 Tony George <teejeetech@gmail.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
 * MA 02110-1301, USA.
 
21
 *
 
22
 *
 
23
 */
 
24
 
 
25
using TeeJee.Logging;
 
26
using TeeJee.FileSystem;
 
27
using TeeJee.ProcessHelper;
 
28
using TeeJee.Misc;
 
29
 
 
30
public class AppLock : GLib.Object {
 
31
        public string lock_file = "";
 
32
        public string lock_message = "";
 
33
        
 
34
        public bool create(string app_name, string message){
 
35
 
 
36
                var lock_dir = "/var/run/lock/%s".printf(app_name);
 
37
                dir_create(lock_dir);
 
38
                lock_file = path_combine(lock_dir, "lock");
 
39
                
 
40
                try{
 
41
                        var file = File.new_for_path(lock_file);
 
42
                        if (file.query_exists()) {
 
43
 
 
44
                                string txt = file_read(lock_file);
 
45
                                string process_id = txt.split(";")[0].strip();
 
46
                                lock_message = txt.split(";")[1].strip();
 
47
                                long pid = long.parse(process_id);
 
48
 
 
49
                                if (process_is_running(pid)){
 
50
                                        log_msg(_("Another instance of this application is running")
 
51
                                                + " (PID=%ld)".printf(pid));
 
52
                                        return false;
 
53
                                }
 
54
                                else{
 
55
                                        log_msg(_("[Warning] Deleted invalid lock"));
 
56
                                        file.delete();
 
57
                                        write_lock_file(message);
 
58
                                        return true;
 
59
                                }
 
60
                        }
 
61
                        else{
 
62
                                write_lock_file(message);
 
63
                                return true;
 
64
                        }
 
65
                }
 
66
                catch (Error e) {
 
67
                        log_error (e.message);
 
68
                        return false;
 
69
                }
 
70
        }
 
71
 
 
72
        private void write_lock_file(string message){
 
73
                string current_pid = ((long) Posix.getpid()).to_string();
 
74
                file_write(lock_file, "%s;%s".printf(current_pid, message));
 
75
        }
 
76
        
 
77
        public void remove(){
 
78
                try{
 
79
                        var file = File.new_for_path (lock_file);
 
80
                        if (file.query_exists()) {
 
81
                                file.delete();
 
82
                        }
 
83
                }
 
84
                catch (Error e) {
 
85
                        log_error (e.message);
 
86
                }
 
87
        }
 
88
 
 
89
}