]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - SQUASHFS/squashfs-tools-4.4/squashfs-tools/restore.c
1.1.07 release
[Ventoy.git] / SQUASHFS / squashfs-tools-4.4 / squashfs-tools / restore.c
1 /*
2 * Create a squashfs filesystem. This is a highly compressed read only
3 * filesystem.
4 *
5 * Copyright (c) 2013, 2014, 2019
6 * Phillip Lougher <phillip@squashfs.org.uk>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2,
11 * or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 * restore.c
23 */
24
25 #include <pthread.h>
26 #include <sys/ioctl.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <sys/time.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <stdarg.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <dirent.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include "caches-queues-lists.h"
40 #include "squashfs_fs.h"
41 #include "mksquashfs.h"
42 #include "error.h"
43 #include "progressbar.h"
44 #include "info.h"
45
46 #define FALSE 0
47 #define TRUE 1
48
49 extern pthread_t reader_thread, writer_thread, main_thread, order_thread;
50 extern pthread_t *deflator_thread, *frag_deflator_thread, *frag_thread;
51 extern struct queue *to_deflate, *to_writer, *to_frag, *to_process_frag;
52 extern struct seq_queue *to_main, *to_order;
53 extern void restorefs();
54 extern int processors;
55 extern int reproducible;
56
57 static int interrupted = 0;
58 static pthread_t restore_thread;
59
60 void *restore_thrd(void *arg)
61 {
62 sigset_t sigmask, old_mask;
63 int i, sig;
64
65 sigemptyset(&sigmask);
66 sigaddset(&sigmask, SIGINT);
67 sigaddset(&sigmask, SIGTERM);
68 sigaddset(&sigmask, SIGUSR1);
69 pthread_sigmask(SIG_BLOCK, &sigmask, &old_mask);
70
71 while(1) {
72 sigwait(&sigmask, &sig);
73
74 if((sig == SIGINT || sig == SIGTERM) && !interrupted) {
75 ERROR("Interrupting will restore original "
76 "filesystem!\n");
77 ERROR("Interrupt again to quit\n");
78 interrupted = TRUE;
79 continue;
80 }
81
82 /* kill main thread/worker threads and restore */
83 set_progressbar_state(FALSE);
84 disable_info();
85
86 /* first kill the reader thread */
87 pthread_cancel(reader_thread);
88 pthread_join(reader_thread, NULL);
89
90 /*
91 * then flush the reader to deflator thread(s) output queue.
92 * The deflator thread(s) will idle
93 */
94 queue_flush(to_deflate);
95
96 /* now kill the deflator thread(s) */
97 for(i = 0; i < processors; i++)
98 pthread_cancel(deflator_thread[i]);
99 for(i = 0; i < processors; i++)
100 pthread_join(deflator_thread[i], NULL);
101
102 /*
103 * then flush the reader to process fragment thread(s) output
104 * queue. The process fragment thread(s) will idle
105 */
106 queue_flush(to_process_frag);
107
108 /* now kill the process fragment thread(s) */
109 for(i = 0; i < processors; i++)
110 pthread_cancel(frag_thread[i]);
111 for(i = 0; i < processors; i++)
112 pthread_join(frag_thread[i], NULL);
113
114 /*
115 * then flush the reader/deflator/process fragment to main
116 * thread output queue. The main thread will idle
117 */
118 seq_queue_flush(to_main);
119
120 /* now kill the main thread */
121 pthread_cancel(main_thread);
122 pthread_join(main_thread, NULL);
123
124 /* then flush the main thread to fragment deflator thread(s)
125 * queue. The fragment deflator thread(s) will idle
126 */
127 queue_flush(to_frag);
128
129 /* now kill the fragment deflator thread(s) */
130 for(i = 0; i < processors; i++)
131 pthread_cancel(frag_deflator_thread[i]);
132 for(i = 0; i < processors; i++)
133 pthread_join(frag_deflator_thread[i], NULL);
134
135 if(reproducible) {
136 /* then flush the fragment deflator_threads(s)
137 * to frag orderer thread. The frag orderer
138 * thread will idle
139 */
140 seq_queue_flush(to_order);
141
142 /* now kill the frag orderer thread */
143 pthread_cancel(order_thread);
144 pthread_join(order_thread, NULL);
145 }
146
147 /*
148 * then flush the main thread/fragment deflator thread(s)
149 * to writer thread queue. The writer thread will idle
150 */
151 queue_flush(to_writer);
152
153 /* now kill the writer thread */
154 pthread_cancel(writer_thread);
155 pthread_join(writer_thread, NULL);
156
157 TRACE("All threads cancelled\n");
158
159 restorefs();
160 }
161 }
162
163
164 pthread_t *init_restore_thread()
165 {
166 pthread_create(&restore_thread, NULL, restore_thrd, NULL);
167 return &restore_thread;
168 }