-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
925 lines (780 loc) · 27.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/*
* Copyright (C) 2016-2021 i.Dark_Templar <[email protected]>
*
* This file is part of DT Kernel Cleaner.
*
* DT Kernel Cleaner is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DT Kernel Cleaner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DT Kernel Cleaner. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <functional>
#include <list>
#include <optional>
#include <regex>
#include <set>
#include <string>
#include <vector>
const std::string directory_boot = "/boot";
const std::string directory_modules = "/lib/modules";
const std::string directory_src = "/usr/src";
const std::string prefix_boot_config = "config-";
const std::string prefix_boot_map = "System.map-";
const std::string prefix_boot_map_regex = "System\\.map-";
const std::string prefix_boot_image = "vmlinuz-";
const std::string prefix_boot_initramfs = "initramfs-";
const std::string prefix_src = "linux-";
const std::string regex_version = "\\d+(?:\\.\\d+)*";
const std::string regex_revision_and_local_version = "(?:-|_)\\S+";
const std::string regex_files_boot_check = "^(?:(?:" + prefix_boot_config + ")|(?:" + prefix_boot_map_regex + ")|(?:" + prefix_boot_image + "))?" + regex_version + regex_revision_and_local_version + "(?:\\.old)?$";
const std::string regex_files_boot_initramfs_check = "^(?:" + prefix_boot_initramfs + ")?" + regex_version + regex_revision_and_local_version + "\\.img(?:\\.old)?$";
const std::string regex_files_modules_check = "^" + regex_version + regex_revision_and_local_version + "$";
const std::string regex_files_src_check = "^(?:" + prefix_src + ")?" + regex_version + regex_revision_and_local_version + "$";
const std::string regex_files_boot_capture = "^(?:(?:" + prefix_boot_config + ")|(?:" + prefix_boot_map_regex + ")|(?:" + prefix_boot_image + "))?(" + regex_version + ")(" + regex_revision_and_local_version + ")$";
const std::string regex_files_boot_capture_old = "^(?:(?:" + prefix_boot_config + ")|(?:" + prefix_boot_map_regex + ")|(?:" + prefix_boot_image + "))?(" + regex_version + ")(" + regex_revision_and_local_version + ")\\.old$";
const std::string regex_files_boot_initramfs_capture = "^(?:" + prefix_boot_initramfs + ")?(" + regex_version + ")(" + regex_revision_and_local_version + ")\\.img(?:\\.old)?$";
const std::string regex_files_modules_capture = "^(" + regex_version + ")(" + regex_revision_and_local_version + ")$";
const std::string regex_files_src_capture = "^(?:" + prefix_src + ")?(" + regex_version + ")(" + regex_revision_and_local_version + ")$";
const std::string regex_input_capture = "^(" + regex_version + ")((?:" + regex_revision_and_local_version + ")?)$";
typedef unsigned int version_info_type;
std::string versionToString(const std::vector<version_info_type> &version)
{
std::stringstream str;
std::vector<version_info_type>::const_iterator iter, iter_end;
iter = version.begin();
iter_end = version.end();
if (iter != iter_end)
{
str << *iter;
++iter;
for (; iter != iter_end; ++iter)
{
str << "." << *iter;
}
}
return str.str();
}
struct version_info
{
version_info(const std::vector<version_info_type> &l_version, const std::string &l_revision, const std::string &l_local_version = std::string())
: version(l_version),
revision(l_revision),
local_version(l_local_version)
{
}
std::vector<version_info_type> version;
std::string revision;
std::string local_version;
std::string toString() const
{
return versionToString(version) + revision + local_version;
}
};
struct VersionLess: std::binary_function<std::vector<version_info_type>, std::vector<version_info_type>, bool>
{
bool operator()(const std::vector<version_info_type> &lhs, const std::vector<version_info_type> &rhs) const
{
std::vector<version_info_type>::const_iterator iter_lhs, iter_rhs, iter_lhs_end, iter_rhs_end;
iter_lhs_end = lhs.end();
iter_rhs_end = rhs.end();
for (iter_lhs = lhs.begin(), iter_rhs = rhs.begin(); (iter_lhs != iter_lhs_end) && (iter_rhs != iter_rhs_end); ++iter_lhs, ++iter_rhs)
{
if (*iter_lhs != *iter_rhs)
{
return (*iter_lhs < *iter_rhs);
}
}
return (iter_rhs != iter_rhs_end);
}
};
bool operator<(const version_info &a, const version_info &b)
{
std::vector<version_info_type>::const_iterator iter_a, iter_b, iter_a_end, iter_b_end;
iter_a_end = a.version.end();
iter_b_end = b.version.end();
for (iter_a = a.version.begin(), iter_b = b.version.begin(); (iter_a != iter_a_end) && (iter_b != iter_b_end); ++iter_a, ++iter_b)
{
if (*iter_a != *iter_b)
{
return (*iter_a < *iter_b);
}
}
if (iter_b != iter_b_end)
{
return true;
}
else if (iter_a != iter_a_end)
{
return false;
}
else if (a.revision < b.revision)
{
return true;
}
else if (b.revision < a.revision)
{
return false;
}
else
{
return (a.local_version < b.local_version);
}
}
std::set<std::string> list_files_in_directory(const std::string &location, const std::list<std::string> &filter_regex_string_list)
{
std::set<std::string> files;
struct stat buffer;
std::list<std::regex> filter_regex_list;
for (auto iter = filter_regex_string_list.begin(); iter != filter_regex_string_list.end(); ++iter)
{
filter_regex_list.push_back(std::regex(*iter));
}
if (stat(location.c_str(), &buffer) != -1)
{
if (S_ISDIR(buffer.st_mode))
{
DIR *dirp;
if ((dirp = opendir(location.c_str())) != NULL)
{
try
{
for (;;)
{
struct dirent *dp = readdir(dirp);
if (dp == NULL)
{
break;
}
if ((strcmp(dp->d_name,".") == 0) || (strcmp(dp->d_name,"..") == 0))
{
continue;
}
for (auto iter = filter_regex_list.begin(); iter != filter_regex_list.end(); ++iter)
{
if (std::regex_match(dp->d_name, *iter))
{
files.insert(dp->d_name);
break;
}
}
}
}
catch (...)
{
closedir(dirp);
throw;
}
closedir(dirp);
}
}
}
return files;
}
void find_all_files_and_dirs(const std::string &location, std::set<std::string> &files, std::set<std::string> &directories)
{
struct stat buffer;
if (lstat(location.c_str(), &buffer) != -1)
{
if (S_ISDIR(buffer.st_mode))
{
DIR *dirp;
directories.insert(location);
if ((dirp = opendir(location.c_str())) != NULL)
{
try
{
struct dirent *dp;
do
{
if ((dp = readdir(dirp)) != NULL)
{
if ((strcmp(dp->d_name, ".") != 0) && (strcmp(dp->d_name, "..") != 0))
{
find_all_files_and_dirs(location + "/" + std::string(dp->d_name), files, directories);
}
}
} while (dp != NULL);
}
catch (...)
{
closedir(dirp);
throw;
}
closedir(dirp);
}
}
else /* if (S_ISREG(buffer.st_mode) || S_ISLNK(buffer.st_mode)) */
{
files.insert(location);
}
}
}
std::vector<version_info_type> convertStringToVersion(const std::string &version_string)
{
std::vector<version_info_type> version_vector;
size_t last_pos = 0;
for (;;)
{
size_t pos = version_string.find('.', last_pos);
version_vector.push_back(std::stoul(version_string.substr(last_pos, pos - last_pos)));
if (pos == std::string::npos)
{
break;
}
last_pos = pos + 1;
}
return version_vector;
}
void remove_file(const std::string &file)
{
if (unlink(file.c_str()) < 0)
{
fprintf(stderr, "Failed to remove file: %s\n", file.c_str());
}
}
void remove_directory(const std::string &directory)
{
if (rmdir(directory.c_str()) < 0)
{
fprintf(stderr, "Failed to remove directory: %s\n", directory.c_str());
}
}
// kernel version, kernel revision
std::map<std::vector<version_info_type>, std::set<std::string>, VersionLess> kernel_src_versions;
// kernel version, kernel revision, kernel local version
std::map<std::vector<version_info_type>, std::map<std::string, std::set<std::string> >, VersionLess> kernel_versions_tree;
void print_help(const char *name)
{
fprintf(stderr,
"USAGE: %s [options] kernel_version\n"
"Options:\n"
"\t[-h] --help - shows this info\n"
"\t[-l] --list-only - list found kernel versions and exit. Do not specify kernel versions with this option\n"
"\t[-v] --verbose - list found files and also print actions before executing them\n"
"\t[-n] --dryrun - do not execute actions, only print them\n"
"\t[-k] --keep-vmlinuzold - do not remove vmlinuz.old symlink if it becomes obsolete\n"
"\t[-c] --clean-old - remove all kernels except the one currently running\n"
"\t[-s] --keep-sources - keep sources even if no kernel is built out of those sources is present\n"
"\n"
"\tkernel version is in format d.d.d-revision or just d.d.d (number of digits is variable)\n",
name);
}
int main(int argc, char **argv)
{
try
{
bool list_only = false;
bool verbose = false;
bool dry_run = false;
bool help = false;
bool do_not_touch_vmlinuzold = false;
bool clean_old = false;
bool keep_sources = false;
std::set<version_info> selected_kernels;
for (int i = 1; i < argc; ++i)
{
if ((strcmp(argv[i],"--help") == 0) || (strcmp(argv[i], "-h") == 0))
{
help = true;
}
else if ((strcmp(argv[i],"--list-only") == 0) || (strcmp(argv[i], "-l") == 0))
{
list_only = true;
}
else if ((strcmp(argv[i],"--verbose") == 0) || (strcmp(argv[i], "-v") == 0))
{
verbose = true;
}
else if ((strcmp(argv[i],"--dryrun") == 0) || (strcmp(argv[i], "-n") == 0))
{
dry_run = true;
}
else if ((strcmp(argv[i],"--keep-vmlinuzold") == 0) || (strcmp(argv[i], "-k") == 0))
{
do_not_touch_vmlinuzold = true;
}
else if ((strcmp(argv[i],"--clean-old") == 0) || (strcmp(argv[i], "-c") == 0))
{
clean_old = true;
}
else if ((strcmp(argv[i],"--keep-sources") == 0) || (strcmp(argv[i], "-s") == 0))
{
keep_sources = true;
}
else
{
std::cmatch reg_results;
if (std::regex_match(argv[i], reg_results, std::regex(regex_input_capture)))
{
version_info version(convertStringToVersion(reg_results.str(1)), reg_results.str(2));
if (selected_kernels.find(version) == selected_kernels.end())
{
selected_kernels.insert(version);
}
else
{
fprintf(stderr, "Kernel version is specified multiple times: %s\n", version.toString().c_str());
return 0;
}
}
else
{
fprintf(stderr, "Unknown option or invalid format of kernel version: %s, try %s --help for more information\n", argv[i], argv[0]);
return 0;
}
}
}
if (help)
{
print_help(argv[0]);
return 0;
}
if ((!list_only) && selected_kernels.empty() && (!clean_old))
{
fprintf(stderr, "Error: no kernel versions or other actions are specified. Try %s --help for more information\n", argv[0]);
return -1;
}
if (list_only && (!selected_kernels.empty()) && clean_old)
{
fprintf(stderr, "Error: too much incompatible action options are specified. Try %s --help for more information\n", argv[0]);
return -1;
}
// First, get all kernel source versions from /usr/src. There's no way to differ between revision and local version without checking against available kernel source versions
if (verbose)
{
printf("Directories in %s:\n", directory_src.c_str());
}
std::set<std::string> files = list_files_in_directory(directory_src, { regex_files_src_check });
for (auto iter = files.begin(); iter != files.end(); ++iter)
{
if (verbose)
{
printf("%s\n", iter->c_str());
}
std::smatch reg_results;
if (std::regex_match(*iter, reg_results, std::regex(regex_files_src_capture)))
{
std::vector<version_info_type> version_vector = convertStringToVersion(reg_results.str(1));
std::string revision_string = reg_results.str(2);
kernel_src_versions[version_vector].insert(revision_string);
}
}
// Now check /boot
if (verbose)
{
printf("\nFiles in %s:\n", directory_boot.c_str());
}
files = list_files_in_directory(directory_boot, { regex_files_boot_check, regex_files_boot_initramfs_check });
for (auto iter = files.begin(); iter != files.end(); ++iter)
{
if (verbose)
{
printf("%s\n", iter->c_str());
}
std::smatch reg_results;
if (std::regex_match(*iter, reg_results, std::regex(regex_files_boot_capture_old))
|| std::regex_match(*iter, reg_results, std::regex(regex_files_boot_capture))
|| std::regex_match(*iter, reg_results, std::regex(regex_files_boot_initramfs_capture)))
{
std::vector<version_info_type> version_vector = convertStringToVersion(reg_results.str(1));
std::string revision_and_local_version_string = reg_results.str(2);
auto kernel_src_version = kernel_src_versions.find(version_vector);
if (kernel_src_version != kernel_src_versions.end())
{
auto kernel_src_revision = kernel_src_version->second.begin();
auto kernel_src_revision_end = kernel_src_version->second.end();
for ( ; kernel_src_revision != kernel_src_revision_end; ++kernel_src_revision)
{
if (kernel_src_revision->compare(0, std::string::npos, revision_and_local_version_string, 0, kernel_src_revision->length()) == 0)
{
break;
}
}
if (kernel_src_revision != kernel_src_revision_end)
{
kernel_versions_tree[version_vector][*kernel_src_revision].insert(revision_and_local_version_string.substr(kernel_src_revision->length()));
}
else
{
kernel_versions_tree[version_vector][revision_and_local_version_string].insert(std::string());
}
}
else
{
kernel_versions_tree[version_vector][revision_and_local_version_string].insert(std::string());
}
}
}
// Now check /lib/modules
if (verbose)
{
printf("\nDirectories in %s:\n", directory_modules.c_str());
}
files = list_files_in_directory(directory_modules, { regex_files_modules_check });
for (auto iter = files.begin(); iter != files.end(); ++iter)
{
if (verbose)
{
printf("%s\n", iter->c_str());
}
std::smatch reg_results;
if (std::regex_match(*iter, reg_results, std::regex(regex_files_modules_capture)))
{
std::vector<version_info_type> version_vector = convertStringToVersion(reg_results.str(1));
std::string revision_and_local_version_string = reg_results.str(2);
auto kernel_src_version = kernel_src_versions.find(version_vector);
if (kernel_src_version != kernel_src_versions.end())
{
auto kernel_src_revision = kernel_src_version->second.begin();
auto kernel_src_revision_end = kernel_src_version->second.end();
for ( ; kernel_src_revision != kernel_src_revision_end; ++kernel_src_revision)
{
if (kernel_src_revision->compare(0, std::string::npos, revision_and_local_version_string, 0, kernel_src_revision->length()) == 0)
{
break;
}
}
if (kernel_src_revision != kernel_src_revision_end)
{
kernel_versions_tree[version_vector][*kernel_src_revision].insert(revision_and_local_version_string.substr(kernel_src_revision->length()));
}
else
{
kernel_versions_tree[version_vector][revision_and_local_version_string].insert(std::string());
}
}
else
{
kernel_versions_tree[version_vector][revision_and_local_version_string].insert(std::string());
}
}
}
if (verbose)
{
printf("\n");
}
if (list_only)
{
printf("kernel source tree for versions:\n");
for (auto iter_version = kernel_src_versions.begin(); iter_version != kernel_src_versions.end(); ++iter_version)
{
for (auto iter_revision = iter_version->second.begin(); iter_revision != iter_version->second.end(); ++iter_revision)
{
printf("%s\n", version_info(iter_version->first, *iter_revision).toString().c_str());
}
}
printf("\nkernel image and module versions:\n");
for (auto iter_version = kernel_versions_tree.begin(); iter_version != kernel_versions_tree.end(); ++iter_version)
{
for (auto iter_revision = iter_version->second.begin(); iter_revision != iter_version->second.end(); ++iter_revision)
{
for (auto iter_local_version = iter_revision->second.begin(); iter_local_version != iter_revision->second.end(); ++iter_local_version)
{
printf("%s\n", version_info(iter_version->first, iter_revision->first, *iter_local_version).toString().c_str());
}
}
}
}
else
{
if (clean_old)
{
struct utsname name;
if (uname(&name) == -1)
{
throw std::runtime_error("uname() call failed");
}
std::string current_version = name.release;
std::smatch reg_results;
if (!std::regex_match(current_version, reg_results, std::regex(regex_input_capture)))
{
std::stringstream str;
str << "Failed to parse version string returned by uname(): " << current_version;
throw std::runtime_error(str.str());
}
version_info version(convertStringToVersion(reg_results.str(1)), reg_results.str(2));
selected_kernels.clear();
auto kernel_version = kernel_versions_tree.begin();
auto kernel_version_end = kernel_versions_tree.end();
for ( ; kernel_version != kernel_version_end; ++kernel_version)
{
auto kernel_revision = kernel_version->second.begin();
auto kernel_revision_end = kernel_version->second.end();
for ( ; kernel_revision != kernel_revision_end; ++kernel_revision)
{
auto kernel_local_version = kernel_revision->second.begin();
auto kernel_local_version_end = kernel_revision->second.end();
for ( ; kernel_local_version != kernel_local_version_end; ++kernel_local_version)
{
version_info found_version(kernel_version->first, kernel_revision->first, *kernel_local_version);
if (version.toString() != found_version.toString())
{
selected_kernels.insert(found_version);
}
}
}
}
}
auto kernel_version_iter = selected_kernels.begin();
auto kernel_version_iter_end = selected_kernels.end();
for (; kernel_version_iter != kernel_version_iter_end; ++kernel_version_iter)
{
bool found_kernels_matched = false;
std::set<version_info> found_kernels;
std::optional<version_info> found_kernel_sources;
{
std::vector<version_info_type> version_vector = kernel_version_iter->version;
std::string revision_and_local_version_string = kernel_version_iter->revision + kernel_version_iter->local_version;
auto kernel_version = kernel_versions_tree.find(version_vector);
if (kernel_version != kernel_versions_tree.end())
{
auto kernel_revision = kernel_version->second.begin();
auto kernel_revision_end = kernel_version->second.end();
for ( ; kernel_revision != kernel_revision_end; ++kernel_revision)
{
if (kernel_revision->first.compare(0, std::string::npos, revision_and_local_version_string, 0, kernel_revision->first.length()) == 0)
{
std::string kernel_local_version_string = revision_and_local_version_string.substr(kernel_revision->first.length());
auto kernel_local_version = kernel_revision->second.find(kernel_local_version_string);
if (kernel_local_version != kernel_revision->second.end())
{
found_kernels.insert(version_info(version_vector, kernel_revision->first, kernel_local_version_string));
found_kernels_matched = true;
break;
}
}
}
}
auto kernel_src_version = kernel_src_versions.find(version_vector);
if (kernel_src_version != kernel_src_versions.end())
{
auto kernel_src_revision = kernel_src_version->second.begin();
auto kernel_src_revision_end = kernel_src_version->second.end();
for ( ; kernel_src_revision != kernel_src_revision_end; ++kernel_src_revision)
{
if (*kernel_src_revision == revision_and_local_version_string)
{
found_kernel_sources = version_info(version_vector, revision_and_local_version_string);
break;
}
}
}
}
// If kernel not found, then kernel sources are removed. Find all kernels built from this source and remove them.
// They'll have same version and revision and different local version
if (found_kernels.empty() && found_kernel_sources)
{
std::vector<version_info_type> version_vector = found_kernel_sources->version;
auto kernel_version = kernel_versions_tree.find(version_vector);
if (kernel_version != kernel_versions_tree.end())
{
auto kernel_revision = kernel_version->second.find(found_kernel_sources->revision);
if (kernel_revision != kernel_version->second.end())
{
auto kernel_local_version_iter = kernel_revision->second.begin();
auto kernel_local_version_end = kernel_revision->second.end();
for ( ; kernel_local_version_iter != kernel_local_version_end; ++kernel_local_version_iter)
{
found_kernels.insert(version_info(found_kernel_sources->version, found_kernel_sources->revision, *kernel_local_version_iter));
}
}
}
}
for (auto found_kernel_iter = found_kernels.begin(); found_kernel_iter != found_kernels.end(); ++found_kernel_iter)
{
std::string version_str = found_kernel_iter->toString();
printf("Removing kernel version %s\n", version_str.c_str());
struct stat buffer;
std::set<std::string> found_files;
std::set<std::string> directories;
find_all_files_and_dirs(directory_modules + "/" + version_str, found_files, directories);
// clean everything in /boot
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_config + version_str).c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_config + version_str);
}
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_map + version_str).c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_map + version_str);
}
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_image + version_str).c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_image + version_str);
}
if (stat((directory_boot + "/" + prefix_boot_initramfs + version_str + ".img").c_str(), &buffer) != -1)
{
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_initramfs + version_str + ".img").c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_initramfs + version_str + ".img");
}
}
if (stat((directory_boot + "/" + prefix_boot_config + version_str + ".old").c_str(), &buffer) != -1)
{
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_config + version_str + ".old").c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_config + version_str + ".old");
}
}
if (stat((directory_boot + "/" + prefix_boot_map + version_str + ".old").c_str(), &buffer) != -1)
{
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_map + version_str + ".old").c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_map + version_str + ".old");
}
}
if (stat((directory_boot + "/" + prefix_boot_image + version_str + ".old").c_str(), &buffer) != -1)
{
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_image + version_str + ".old").c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_image + version_str + ".old");
}
}
if (stat((directory_boot + "/" + prefix_boot_initramfs + version_str + ".img.old").c_str(), &buffer) != -1)
{
if (verbose)
{
printf("Removing file %s\n", (directory_boot + "/" + prefix_boot_initramfs + version_str + ".img.old").c_str());
}
if (!dry_run)
{
remove_file(directory_boot + "/" + prefix_boot_initramfs + version_str + ".img.old");
}
}
// clean everything in /lib/modules
if (verbose)
{
printf("Recursively removing directory %s\n", (directory_modules + "/" + version_str).c_str());
}
if (!dry_run)
{
auto files_end = found_files.end();
for (auto files_cur = found_files.begin(); files_cur != files_end; ++files_cur)
{
remove_file(*files_cur);
}
// go in reverse order to make sure that top-most directories are removed last
auto dirs_end = directories.rend();
for (auto dirs_cur = directories.rbegin(); dirs_cur != dirs_end; ++dirs_cur)
{
remove_directory(*dirs_cur);
}
}
// remove kernel from lists
kernel_versions_tree[found_kernel_iter->version][found_kernel_iter->revision].erase(found_kernel_iter->local_version);
}
if ((!keep_sources)
&& ((found_kernels_matched
&& kernel_versions_tree[found_kernels.begin()->version][found_kernels.begin()->revision].empty()
&& (kernel_src_versions.find(found_kernels.begin()->version) != kernel_src_versions.end()))
|| ((!found_kernels_matched) && found_kernel_sources)))
{
std::string version_str;
if (found_kernels_matched)
{
version_str = versionToString(found_kernels.begin()->version) + found_kernels.begin()->revision;
}
else
{
version_str = found_kernel_sources->toString();
}
printf("Removing kernel sources version %s\n", version_str.c_str());
std::set<std::string> found_files;
std::set<std::string> directories;
find_all_files_and_dirs(directory_src + "/" + prefix_src + version_str, found_files, directories);
// clean everything in /usr/src
if (verbose)
{
printf("Recursively removing directory %s\n", (directory_src + "/" + prefix_src + version_str).c_str());
}
if (!dry_run)
{
auto files_end = found_files.end();
for (auto files_cur = found_files.begin(); files_cur != files_end; ++files_cur)
{
remove_file(*files_cur);
}
// go in reverse order to make sure that top-most directories are removed last
auto dirs_end = directories.rend();
for (auto dirs_cur = directories.rbegin(); dirs_cur != dirs_end; ++dirs_cur)
{
remove_directory(*dirs_cur);
}
}
}
}
// TODO: this currently does not work in dry-run, i.e. it does not show that it would remove the symlink due to file it points to not being removed
if (!do_not_touch_vmlinuzold)
{
const std::string vmlinuzold_name = "/boot/vmlinuz.old";
struct stat buffer;
if ((lstat(vmlinuzold_name.c_str(), &buffer) != -1)
&& (S_ISLNK(buffer.st_mode))
&& (stat(vmlinuzold_name.c_str(), &buffer) == -1))
{
if (verbose)
{
printf("Removing file %s\n", vmlinuzold_name.c_str());
}
if (!dry_run)
{
remove_file(vmlinuzold_name);
}
}
}
}
}
catch (const std::exception &exc)
{
fprintf(stderr, "Caught std::exception: %s\n", exc.what());
return -1;
}
catch (...)
{
fprintf(stderr, "Caught unknown exception\n");
return -1;
}
return 0;
}