Line data Source code
1 : /**
2 : * This file is part of rmlint.
3 : *
4 : * rmlint is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * rmlint is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with rmlint. If not, see <http://www.gnu.org/licenses/>.
16 : *
17 : ** Authors:
18 : *
19 : * - Christopher <sahib> Pahl 2010-2015 (https://github.com/sahib)
20 : * - Daniel <SeeSpotRun> T. 2014-2015 (https://github.com/SeeSpotRun)
21 : *
22 : ** Hosted on http://github.com/sahib/rmlint
23 : *
24 : **/
25 :
26 : #include <string.h>
27 : #include <stdbool.h>
28 : #include <unistd.h>
29 :
30 : #include "config.h"
31 : #include "session.h"
32 : #include "formats.h"
33 : #include "traverse.h"
34 : #include "preprocess.h"
35 :
36 : #if HAVE_UNAME
37 : #include "sys/utsname.h"
38 :
39 54884 : void rm_session_read_kernel_version(RmSession *session) {
40 : struct utsname buf;
41 54884 : if(uname(&buf) == -1) {
42 0 : return;
43 : }
44 :
45 54884 : if(sscanf(buf.release, "%d.%d.*", &session->kernel_version[0],
46 : &session->kernel_version[1]) == EOF) {
47 0 : session->kernel_version[0] = -1;
48 0 : session->kernel_version[1] = -1;
49 0 : return;
50 : }
51 :
52 54884 : rm_log_debug_line("Linux kernel version is %d.%d.",
53 : session->kernel_version[0],
54 : session->kernel_version[1]);
55 : }
56 : #else
57 : void rm_session_read_kernel_version(RmSession *session) {
58 : (void)session;
59 : }
60 : #endif
61 :
62 0 : bool rm_session_check_kernel_version(RmSession *session, int major, int minor) {
63 0 : int found_major = session->kernel_version[0];
64 0 : int found_minor = session->kernel_version[1];
65 :
66 : /* Could not read kernel version: Assume failure on our side. */
67 0 : if(found_major <= 0 && found_minor <= 0) {
68 0 : return true;
69 : }
70 :
71 : /* Lower is bad. */
72 0 : if(found_major < major || found_minor < minor) {
73 0 : return false;
74 : }
75 :
76 0 : return true;
77 : }
78 :
79 54884 : void rm_session_init(RmSession *session, RmCfg *cfg) {
80 54884 : memset(session, 0, sizeof(RmSession));
81 54884 : session->timer = g_timer_new();
82 54884 : g_queue_init(&session->cache_list);
83 :
84 54884 : session->cfg = cfg;
85 54884 : session->tables = rm_file_tables_new(session);
86 54884 : session->formats = rm_fmt_open(session);
87 :
88 54884 : session->verbosity_count = 2;
89 54884 : session->paranoia_count = 0;
90 54884 : session->output_cnt[0] = -1;
91 54884 : session->output_cnt[1] = -1;
92 :
93 54884 : session->offsets_read = 0;
94 54884 : session->offset_fragments = 0;
95 54884 : session->offset_fails = 0;
96 54884 : g_queue_init(&session->replay_files);
97 :
98 54884 : rm_session_read_kernel_version(session);
99 54884 : }
100 :
101 52994 : void rm_session_clear(RmSession *session) {
102 52994 : RmCfg *cfg = session->cfg;
103 :
104 : /* Free mem */
105 52994 : if(cfg->paths) {
106 52989 : if(cfg->use_meta_cache) {
107 1311 : g_free(cfg->paths);
108 : } else {
109 51678 : g_strfreev(cfg->paths);
110 : }
111 : }
112 :
113 52994 : g_timer_destroy(session->timer);
114 52994 : rm_file_tables_destroy(session->tables);
115 52994 : rm_fmt_close(session->formats);
116 :
117 52994 : if(session->mounts) {
118 36878 : rm_mounts_table_destroy(session->mounts);
119 : }
120 :
121 52994 : if(session->dir_merger) {
122 1120 : rm_tm_destroy(session->dir_merger);
123 : }
124 :
125 53078 : for(GList *iter = session->cache_list.head; iter; iter = iter->next) {
126 84 : g_free((char *)iter->data);
127 : }
128 :
129 52994 : if(session->meta_cache) {
130 1311 : GError *error = NULL;
131 1311 : rm_swap_table_close(session->meta_cache, &error);
132 :
133 1311 : if(error != NULL) {
134 0 : rm_log_error_line(_("Cannot close tmp cache: %s\n"), error->message);
135 0 : g_error_free(error);
136 : }
137 : }
138 :
139 52994 : g_queue_clear(&session->cache_list);
140 :
141 52994 : g_free(cfg->joined_argv);
142 52994 : g_free(cfg->is_prefd);
143 52994 : g_free(cfg->iwd);
144 :
145 69101 : for(GList *iter = session->replay_files.head; iter; iter = iter->next) {
146 16107 : g_free(iter->data);
147 : }
148 :
149 52994 : g_queue_clear(&session->replay_files);
150 52994 : rm_trie_destroy(&cfg->file_trie);
151 52994 : }
152 :
153 0 : void rm_session_abort(RmSession *session) {
154 0 : g_atomic_int_set(&session->aborted, 1);
155 0 : }
156 :
157 839090 : bool rm_session_was_aborted(RmSession *session) {
158 839090 : return g_atomic_int_get(&session->aborted);
159 : }
|