libmacchina 8.1.0

A library that can fetch all sorts of system information.
Documentation
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
#![allow(clippy::unnecessary_cast)]
mod pci_devices;
mod sysinfo_ffi;

use self::pci_devices::get_pci_devices;
use crate::extra;
use crate::extra::get_entries;
use crate::extra::path_extension;
use crate::shared;
use crate::traits::*;
use itertools::Itertools;
use pciid_parser::Database;
use regex::Regex;
use std::ffi::OsStr;
use std::fs;
use std::fs::read_dir;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use sysctl::{Ctl, Sysctl};
use sysinfo_ffi::sysinfo;

impl From<sqlite::Error> for ReadoutError {
    fn from(e: sqlite::Error) -> Self {
        ReadoutError::Other(e.to_string())
    }
}

pub struct LinuxKernelReadout {
    os_release_ctl: Option<Ctl>,
    os_type_ctl: Option<Ctl>,
}

pub struct LinuxGeneralReadout {
    hostname_ctl: Option<Ctl>,
    sysinfo: sysinfo,
}

pub struct LinuxMemoryReadout {
    sysinfo: sysinfo,
}

pub struct LinuxBatteryReadout;
pub struct LinuxProductReadout;
pub struct LinuxPackageReadout;
pub struct LinuxNetworkReadout;

impl BatteryReadout for LinuxBatteryReadout {
    fn new() -> Self {
        LinuxBatteryReadout
    }

    fn percentage(&self) -> Result<u8, ReadoutError> {
        if let Some(entries) = get_entries(Path::new("/sys/class/power_supply")) {
            let dirs: Vec<PathBuf> = entries
                .into_iter()
                .filter(|x| {
                    x.components()
                        .last()
                        .unwrap()
                        .as_os_str()
                        .to_string_lossy()
                        .starts_with("BAT")
                })
                .collect();

            if let Some(battery) = dirs.first() {
                let path_to_capacity = battery.join("capacity");
                let percentage_text = extra::pop_newline(fs::read_to_string(path_to_capacity)?);
                let percentage_parsed = percentage_text.parse::<u8>();

                match percentage_parsed {
                    Ok(p) => return Ok(p),
                    Err(e) => {
                        return Err(ReadoutError::Other(format!(
                            "Could not parse the value '{percentage_text}' into a digit: {e:?}"
                        )))
                    }
                };
            }
        };

        Err(ReadoutError::Other("No batteries detected.".to_string()))
    }

    fn status(&self) -> Result<BatteryState, ReadoutError> {
        if let Some(entries) = get_entries(Path::new("/sys/class/power_supply")) {
            let dirs: Vec<PathBuf> = entries
                .into_iter()
                .filter(|x| {
                    x.components()
                        .last()
                        .unwrap()
                        .as_os_str()
                        .to_string_lossy()
                        .starts_with("BAT")
                })
                .collect();

            if let Some(battery) = dirs.first() {
                let path_to_status = battery.join("status");
                let status_text =
                    extra::pop_newline(fs::read_to_string(path_to_status)?).to_lowercase();

                match &status_text[..] {
                    "charging" => return Ok(BatteryState::Charging),
                    "discharging" | "full" => return Ok(BatteryState::Discharging),
                    s => {
                        return Err(ReadoutError::Other(format!(
                            "Got an unexpected value \"{s}\" reading battery status"
                        )))
                    }
                }
            }
        }

        Err(ReadoutError::Other("No batteries detected.".to_string()))
    }

    fn health(&self) -> Result<u8, ReadoutError> {
        if let Some(entries) = get_entries(Path::new("/sys/class/power_supply")) {
            let dirs: Vec<PathBuf> = entries
                .into_iter()
                .filter(|x| {
                    !x.components()
                        .last()
                        .unwrap()
                        .as_os_str()
                        .to_string_lossy()
                        .starts_with("ADP")
                })
                .collect();

            if let Some(battery) = dirs.first() {
                let energy_full =
                    extra::pop_newline(fs::read_to_string(battery.join("energy_full"))?)
                        .parse::<u8>();

                let energy_full_design =
                    extra::pop_newline(fs::read_to_string(battery.join("energy_full_design"))?)
                        .parse::<u8>();

                match (energy_full, energy_full_design) {
                    (Ok(mut ef), Ok(efd)) => {
                        if ef > efd {
                            ef = efd;
                            return Ok((ef as f32 / efd as f32 * 100_f32).ceil() as u8);
                        }

                        return Ok((ef as f32 / efd as f32 * 100_f32).ceil() as u8);
                    }
                    _ => {
                        return Err(ReadoutError::Other(
                            "Error calculating battery health.".to_string(),
                        ))
                    }
                }
            }
        }

        Err(ReadoutError::Other("No batteries detected.".to_string()))
    }
}

impl KernelReadout for LinuxKernelReadout {
    fn new() -> Self {
        LinuxKernelReadout {
            os_release_ctl: Ctl::new("kernel.osrelease").ok(),
            os_type_ctl: Ctl::new("kernel.ostype").ok(),
        }
    }

    fn os_release(&self) -> Result<String, ReadoutError> {
        Ok(self
            .os_release_ctl
            .as_ref()
            .ok_or(ReadoutError::MetricNotAvailable)?
            .value_string()?)
    }

    fn os_type(&self) -> Result<String, ReadoutError> {
        Ok(self
            .os_type_ctl
            .as_ref()
            .ok_or(ReadoutError::MetricNotAvailable)?
            .value_string()?)
    }
}

impl NetworkReadout for LinuxNetworkReadout {
    fn new() -> Self {
        LinuxNetworkReadout
    }

    fn tx_bytes(&self, interface: Option<&str>) -> Result<usize, ReadoutError> {
        if let Some(ifname) = interface {
            let rx_file = PathBuf::from("/sys/class/net")
                .join(ifname)
                .join("statistics/tx_bytes");
            let content = std::fs::read_to_string(rx_file)?;
            let bytes = extra::pop_newline(content)
                .parse::<usize>()
                .unwrap_or_default();
            Ok(bytes)
        } else {
            Err(ReadoutError::Other(String::from(
                "Please specify a network interface to query.",
            )))
        }
    }

    fn tx_packets(&self, interface: Option<&str>) -> Result<usize, ReadoutError> {
        if let Some(ifname) = interface {
            let rx_file = PathBuf::from("/sys/class/net")
                .join(ifname)
                .join("statistics/tx_packets");
            let content = std::fs::read_to_string(rx_file)?;
            let packets = extra::pop_newline(content)
                .parse::<usize>()
                .unwrap_or_default();
            Ok(packets)
        } else {
            Err(ReadoutError::Other(String::from(
                "Please specify a network interface to query.",
            )))
        }
    }

    fn rx_bytes(&self, interface: Option<&str>) -> Result<usize, ReadoutError> {
        if let Some(ifname) = interface {
            let rx_file = PathBuf::from("/sys/class/net")
                .join(ifname)
                .join("statistics/rx_bytes");
            let content = std::fs::read_to_string(rx_file)?;
            let bytes = extra::pop_newline(content)
                .parse::<usize>()
                .unwrap_or_default();
            Ok(bytes)
        } else {
            Err(ReadoutError::Other(String::from(
                "Please specify a network interface to query.",
            )))
        }
    }

    fn rx_packets(&self, interface: Option<&str>) -> Result<usize, ReadoutError> {
        if let Some(ifname) = interface {
            let rx_file = PathBuf::from("/sys/class/net")
                .join(ifname)
                .join("statistics/rx_packets");
            let content = std::fs::read_to_string(rx_file)?;
            let packets = extra::pop_newline(content)
                .parse::<usize>()
                .unwrap_or_default();
            Ok(packets)
        } else {
            Err(ReadoutError::Other(String::from(
                "Please specify a network interface to query.",
            )))
        }
    }

    fn physical_address(&self, interface: Option<&str>) -> Result<String, ReadoutError> {
        if let Some(ifname) = interface {
            let rx_file = PathBuf::from("/sys/class/net").join(ifname).join("address");
            let content = std::fs::read_to_string(rx_file)?;
            Ok(content)
        } else {
            Err(ReadoutError::Other(String::from(
                "Please specify a network interface to query.",
            )))
        }
    }

    fn logical_address(&self, interface: Option<&str>) -> Result<String, ReadoutError> {
        shared::logical_address(interface)
    }
}

impl GeneralReadout for LinuxGeneralReadout {
    fn new() -> Self {
        LinuxGeneralReadout {
            hostname_ctl: Ctl::new("kernel.hostname").ok(),
            sysinfo: sysinfo::new(),
        }
    }

    fn backlight(&self) -> Result<usize, ReadoutError> {
        if let Some(base) = get_entries(Path::new("/sys/class/backlight/")) {
            if let Some(backlight_path) = base.into_iter().next() {
                let max_brightness_path = backlight_path.join("max_brightness");
                let current_brightness_path = backlight_path.join("brightness");

                let max_brightness_value =
                    extra::pop_newline(fs::read_to_string(max_brightness_path)?)
                        .parse::<usize>()
                        .ok();

                let current_brightness_value =
                    extra::pop_newline(fs::read_to_string(current_brightness_path)?)
                        .parse::<usize>()
                        .ok();

                match (current_brightness_value, max_brightness_value) {
                    (Some(c), Some(m)) => {
                        let brightness = c as f64 / m as f64 * 100f64;
                        return Ok(brightness.round() as usize);
                    }
                    _ => {
                        return Err(ReadoutError::Other(String::from(
                            "Error occurred while calculating backlight (brightness) value.",
                        )));
                    }
                }
            }
        }

        Err(ReadoutError::Other(String::from(
            "Could not obtain backlight information.",
        )))
    }

    fn resolution(&self) -> Result<String, ReadoutError> {
        let drm = Path::new("/sys/class/drm");

        if let Some(entries) = get_entries(drm) {
            let mut resolutions: Vec<String> = Vec::new();
            entries.into_iter().for_each(|entry| {
                // Append "modes" to /sys/class/drm/<card>/
                let modes = entry.join("modes");
                if let Ok(file) = File::open(modes) {
                    // Push the resolution to the resolutions vector.
                    if let Some(Ok(res)) = BufReader::new(file).lines().next() {
                        resolutions.push(res);
                    }
                }
            });

            return Ok(resolutions.join(", "));
        }

        Err(ReadoutError::Other(
            "Could not obtain screen resolution from /sys/class/drm".to_string(),
        ))
    }

    fn username(&self) -> Result<String, ReadoutError> {
        shared::username()
    }

    fn hostname(&self) -> Result<String, ReadoutError> {
        Ok(self
            .hostname_ctl
            .as_ref()
            .ok_or(ReadoutError::MetricNotAvailable)?
            .value_string()?)
    }

    fn distribution(&self) -> Result<String, ReadoutError> {
        use os_release::OsRelease;
        let content = OsRelease::new()?;

        if !content.version.is_empty() {
            return Ok(format!("{} {}", content.name, content.version));
        } else if !content.version_id.is_empty() {
            return Ok(format!("{} {}", content.name, content.version_id));
        }

        Ok(content.name)
    }

    fn desktop_environment(&self) -> Result<String, ReadoutError> {
        shared::desktop_environment()
    }

    fn session(&self) -> Result<String, ReadoutError> {
        shared::session()
    }

    fn window_manager(&self) -> Result<String, ReadoutError> {
        shared::window_manager()
    }

    fn terminal(&self) -> Result<String, ReadoutError> {
        // This function returns the PPID of a given PID:
        //  - The file used to extract this data: /proc/<pid>/status
        //  - This function parses and returns the value of the ppid line.
        fn get_parent(pid: i32) -> i32 {
            let process_path = PathBuf::from("/proc").join(pid.to_string()).join("status");
            let file = File::open(process_path);
            match file {
                Ok(content) => {
                    let reader = BufReader::new(content);
                    for line in reader.lines().map_while(Result::ok) {
                        if line.to_uppercase().starts_with("PPID") {
                            let s_mem_kb: String =
                                line.chars().filter(|c| c.is_ascii_digit()).collect();
                            return s_mem_kb.parse::<i32>().unwrap_or(-1);
                        }
                    }

                    -1
                }

                Err(_) => -1,
            }
        }

        // This function returns the name associated with a given PPID
        fn terminal_name() -> String {
            let mut terminal_pid = get_parent(unsafe { libc::getppid() });

            let path = PathBuf::from("/proc")
                .join(terminal_pid.to_string())
                .join("comm");

            // The below loop will traverse /proc to find the
            // terminal inside of which the user is operating
            if let Ok(mut terminal_name) = fs::read_to_string(path) {
                // Any command_name we find that matches
                // one of the elements within this table
                // is effectively ignored
                while extra::common_shells().contains(&terminal_name.replace('\n', "").as_str()) {
                    let ppid = get_parent(terminal_pid);
                    terminal_pid = ppid;

                    let path = PathBuf::from("/proc").join(ppid.to_string()).join("comm");

                    if let Ok(comm) = fs::read_to_string(path) {
                        terminal_name = comm;
                    }
                }

                return terminal_name;
            }

            String::new()
        }

        let terminal = terminal_name();

        if terminal.is_empty() {
            return Err(ReadoutError::Other(
                "Querying terminal information failed".to_string(),
            ));
        }

        Ok(terminal)
    }

    fn shell(&self, format: ShellFormat, kind: ShellKind) -> Result<String, ReadoutError> {
        shared::shell(format, kind)
    }

    fn cpu_model_name(&self) -> Result<String, ReadoutError> {
        Ok(shared::cpu_model_name())
    }

    fn cpu_usage(&self) -> Result<usize, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };

        if ret != -1 {
            let f_load = 1f64 / (1 << libc::SI_LOAD_SHIFT) as f64;
            let cpu_usage = info.loads[0] as f64 * f_load;
            let cpu_usage_u =
                (cpu_usage / self.cpu_cores().unwrap() as f64 * 100.0).round() as usize;
            return Ok(cpu_usage_u as usize);
        }

        Err(ReadoutError::Other(
            "Something went wrong during the initialization of the sysinfo struct.".to_string(),
        ))
    }

    fn cpu_physical_cores(&self) -> Result<usize, ReadoutError> {
        use std::io::{BufRead, BufReader};
        if let Ok(content) = File::open("/proc/cpuinfo") {
            let reader = BufReader::new(content);
            for line in reader.lines().map_while(Result::ok) {
                if line.to_lowercase().starts_with("cpu cores") {
                    return Ok(line
                        .split(':')
                        .nth(1)
                        .unwrap()
                        .trim()
                        .parse::<usize>()
                        .unwrap());
                }
            }
        }

        Err(ReadoutError::MetricNotAvailable)
    }

    fn cpu_cores(&self) -> Result<usize, ReadoutError> {
        Ok(unsafe { libc::sysconf(libc::_SC_NPROCESSORS_CONF) } as usize)
    }

    fn uptime(&self) -> Result<usize, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };

        if ret != -1 {
            return Ok(info.uptime as usize);
        }

        Err(ReadoutError::Other(
            "Something went wrong during the initialization of the sysinfo struct.".to_string(),
        ))
    }

    fn machine(&self) -> Result<String, ReadoutError> {
        let product_readout = LinuxProductReadout::new();

        let vendor = product_readout.vendor()?;
        let family = product_readout.family()?;
        let product = product_readout.product()?;
        let version = extra::pop_newline(fs::read_to_string("/sys/class/dmi/id/product_version")?);

        // If one field is generic, the others are likely the same, so fail the readout.
        if vendor.eq_ignore_ascii_case("system manufacturer") {
            return Err(ReadoutError::Other(String::from(
                "Your manufacturer may have not specified your machine's product information.",
            )));
        }

        let new_product =
            format!("{vendor} {family} {product} {version}").replace("To be filled by O.E.M.", "");

        if family == product && family == version {
            return Ok(family);
        } else if version.is_empty() || version.len() <= 22 {
            return Ok(new_product.split_whitespace().unique().join(" "));
        }

        Ok(version)
    }

    fn os_name(&self) -> Result<String, ReadoutError> {
        Err(ReadoutError::NotImplemented)
    }

    fn disk_space(&self, path: &Path) -> Result<(u64, u64), ReadoutError> {
        shared::disk_space(path)
    }

    fn gpus(&self) -> Result<Vec<String>, ReadoutError> {
        let db = match Database::read() {
            Ok(db) => db,
            _ => return Err(ReadoutError::MetricNotAvailable),
        };

        let devices = get_pci_devices()?;
        let mut gpus = vec![];

        for device in devices {
            if !device.is_gpu(&db) {
                continue;
            };

            if let Some(sub_device_name) = device.get_device_name(&db) {
                gpus.push(sub_device_name);
            };
        }

        if gpus.is_empty() {
            Err(ReadoutError::MetricNotAvailable)
        } else {
            Ok(gpus)
        }
    }
}

impl MemoryReadout for LinuxMemoryReadout {
    fn new() -> Self {
        LinuxMemoryReadout {
            sysinfo: sysinfo::new(),
        }
    }

    fn total(&self) -> Result<u64, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };
        if ret != -1 {
            Ok(info.totalram as u64 * info.mem_unit as u64 / 1024)
        } else {
            Err(ReadoutError::Other(
                "Something went wrong during the initialization of the sysinfo struct.".to_string(),
            ))
        }
    }

    fn free(&self) -> Result<u64, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };
        if ret != -1 {
            Ok(info.freeram as u64 * info.mem_unit as u64 / 1024)
        } else {
            Err(ReadoutError::Other(
                "Something went wrong during the initialization of the sysinfo struct.".to_string(),
            ))
        }
    }

    fn buffers(&self) -> Result<u64, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };
        if ret != -1 {
            Ok(info.bufferram as u64 * info.mem_unit as u64 / 1024)
        } else {
            Err(ReadoutError::Other(
                "Something went wrong during the initialization of the sysinfo struct.".to_string(),
            ))
        }
    }

    fn cached(&self) -> Result<u64, ReadoutError> {
        Ok(shared::get_meminfo_value("Cached"))
    }

    fn reclaimable(&self) -> Result<u64, ReadoutError> {
        Ok(shared::get_meminfo_value("SReclaimable"))
    }

    fn used(&self) -> Result<u64, ReadoutError> {
        let total = self.total().unwrap();
        match shared::get_meminfo_value("MemAvailable") {
            0 => {
                let free = self.free().unwrap();
                let cached = self.cached().unwrap();
                let reclaimable = self.reclaimable().unwrap();
                let buffers = self.buffers().unwrap();
                Ok(total - free - cached - reclaimable - buffers)
            }
            available => Ok(total - available),
        }
    }

    fn swap_total(&self) -> Result<u64, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };
        if ret != -1 {
            Ok(info.totalswap as u64 * info.mem_unit as u64 / 1024)
        } else {
            Err(ReadoutError::Other(
                "Something went wrong during the initialization of the sysinfo struct.".to_string(),
            ))
        }
    }

    fn swap_free(&self) -> Result<u64, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };
        if ret != -1 {
            Ok(info.freeswap as u64 * info.mem_unit as u64 / 1024)
        } else {
            Err(ReadoutError::Other(
                "Something went wrong during the initialization of the sysinfo struct.".to_string(),
            ))
        }
    }

    fn swap_used(&self) -> Result<u64, ReadoutError> {
        let mut info = self.sysinfo;
        let info_ptr: *mut sysinfo = &mut info;
        let ret = unsafe { sysinfo(info_ptr) };
        if ret != -1 {
            Ok((info.totalswap as u64 - info.freeswap as u64) * info.mem_unit as u64 / 1024)
        } else {
            Err(ReadoutError::Other(
                "Something went wrong during the initialization of the sysinfo struct.".to_string(),
            ))
        }
    }
}

impl ProductReadout for LinuxProductReadout {
    fn new() -> Self {
        LinuxProductReadout
    }

    fn vendor(&self) -> Result<String, ReadoutError> {
        Ok(extra::pop_newline(fs::read_to_string(
            "/sys/class/dmi/id/sys_vendor",
        )?))
    }

    fn family(&self) -> Result<String, ReadoutError> {
        Ok(extra::pop_newline(fs::read_to_string(
            "/sys/class/dmi/id/product_family",
        )?))
    }

    fn product(&self) -> Result<String, ReadoutError> {
        Ok(extra::pop_newline(fs::read_to_string(
            "/sys/class/dmi/id/product_name",
        )?))
    }
}

impl PackageReadout for LinuxPackageReadout {
    fn new() -> Self {
        LinuxPackageReadout
    }

    fn count_pkgs(&self) -> Vec<(PackageManager, usize)> {
        let mut packages = Vec::new();
        let mut home = PathBuf::new();

        // Acquire the value of HOME early on to avoid
        // doing it multiple times.
        if let Ok(path) = std::env::var("HOME") {
            home = PathBuf::from(path);
        }

        if let Some(c) = LinuxPackageReadout::count_pacman() {
            packages.push((PackageManager::Pacman, c));
        }

        if let Some(c) = LinuxPackageReadout::count_dpkg() {
            packages.push((PackageManager::Dpkg, c));
        }

        if let Some(c) = LinuxPackageReadout::count_rpm() {
            packages.push((PackageManager::Rpm, c));
        }

        if let Some(c) = LinuxPackageReadout::count_portage() {
            packages.push((PackageManager::Portage, c));
        }

        if let Some(c) = LinuxPackageReadout::count_cargo() {
            packages.push((PackageManager::Cargo, c));
        }

        if let Some(c) = LinuxPackageReadout::count_xbps() {
            packages.push((PackageManager::Xbps, c));
        }

        if let Some(c) = LinuxPackageReadout::count_eopkg() {
            packages.push((PackageManager::Eopkg, c));
        }

        if let Some(c) = LinuxPackageReadout::count_apk() {
            packages.push((PackageManager::Apk, c));
        }

        if let Some(c) = LinuxPackageReadout::count_flatpak(&home) {
            packages.push((PackageManager::Flatpak, c));
        }

        if let Some(c) = LinuxPackageReadout::count_snap() {
            packages.push((PackageManager::Snap, c));
        }

        if let Some(c) = LinuxPackageReadout::count_homebrew(&home) {
            packages.push((PackageManager::Homebrew, c));
        }

        if let Some(c) = LinuxPackageReadout::count_nix() {
            packages.push((PackageManager::Nix, c));
        }

        packages
    }
}

impl LinuxPackageReadout {
    /// Returns the number of installed packages for systems
    /// that utilize `rpm` as their package manager.
    fn count_rpm() -> Option<usize> {
        // Return the number of installed packages using sqlite (~1ms)
        // as directly calling rpm or dnf is too expensive (~500ms)
        let count_sqlite = 'sqlite: {
            let db = "/var/lib/rpm/rpmdb.sqlite";
            if !Path::new(db).is_file() {
                break 'sqlite None;
            }

            let connection = sqlite::open(db);
            if let Ok(con) = connection {
                let statement = con.prepare("SELECT COUNT(*) FROM Installtid");
                if let Ok(mut s) = statement {
                    if s.next().is_ok() {
                        break 'sqlite match s.read::<Option<i64>, _>(0) {
                            Ok(Some(count)) => Some(count as usize),
                            _ => None,
                        };
                    }
                }
            }

            None
        };

        // If counting with sqlite failed, try using librpm instead
        count_sqlite.or_else(|| unsafe { rpm_pkg_count::count() }.map(|count| count as usize))
    }

    /// Returns the number of installed packages for systems
    /// that utilize `pacman` as their package manager.
    fn count_pacman() -> Option<usize> {
        let pacman_dir = Path::new("/var/lib/pacman/local");
        if pacman_dir.is_dir() {
            if let Ok(read_dir) = read_dir(pacman_dir) {
                return Some(read_dir.count() - 1); // Ignore ALPM_DB_VERSION
            };
        }

        None
    }

    /// Returns the number of installed packages for systems
    /// that utilize `eopkg` as their package manager.
    fn count_eopkg() -> Option<usize> {
        let eopkg_dir = Path::new("/var/lib/eopkg/package");
        if eopkg_dir.is_dir() {
            if let Ok(read_dir) = read_dir(eopkg_dir) {
                return Some(read_dir.count());
            };
        }

        None
    }

    /// Returns the number of installed packages for systems
    /// that utilize `portage` as their package manager.
    fn count_portage() -> Option<usize> {
        let pkg_dir = Path::new("/var/db/pkg");
        if pkg_dir.exists() {
            return Some(
                walkdir::WalkDir::new(pkg_dir)
                    .min_depth(2)
                    .max_depth(2)
                    .into_iter()
                    .count(),
            );
        }

        None
    }

    /// Returns the number of installed packages for systems
    /// that utilize `dpkg` as their package manager.
    fn count_dpkg() -> Option<usize> {
        let dpkg_dir = Path::new("/var/lib/dpkg/info");

        get_entries(dpkg_dir).map(|entries| {
            entries
                .iter()
                .filter(|x| extra::path_extension(x).unwrap_or_default() == "list")
                .count()
        })
    }

    /// Returns the number of installed packages for systems
    /// that have `homebrew` installed.
    fn count_homebrew(home: &Path) -> Option<usize> {
        let keepme = OsStr::new(".keepme");
        let mut base = home.join(".linuxbrew");

        if !base.is_dir() {
            base = PathBuf::from("/home/linuxbrew/.linuxbrew");
        }

        if !base.is_dir() {
            return None;
        }

        match read_dir(base.join("Cellar")) {
            Ok(dir) => Some(
                dir.filter(|entry| match entry {
                    Err(_) => false,
                    Ok(file) => file.file_name() != keepme,
                })
                .count(),
            ),
            Err(_) => None,
        }
    }

    /// Returns the number of installed packages for systems
    /// that utilize `xbps` as their package manager.
    fn count_xbps() -> Option<usize> {
        if !extra::which("xbps-query") {
            return None;
        }

        let xbps_output = Command::new("xbps-query")
            .arg("-l")
            .stdout(Stdio::piped())
            .output()
            .unwrap();

        extra::count_lines(
            String::from_utf8(xbps_output.stdout)
                .expect("ERROR: \"xbps-query -l\" output was not valid UTF-8"),
        )
    }

    /// Returns the number of installed packages for systems
    /// that utilize `apk` as their package manager.
    fn count_apk() -> Option<usize> {
        // faster method for alpine: count empty lines in /lib/apk/db/installed
        if let Ok(content) = fs::read_to_string(Path::new("/lib/apk/db/installed")) {
            return Some(content.lines().filter(|l| l.is_empty()).count());
        }

        // fallback to command invocation
        if !extra::which("apk") {
            return None;
        }

        let apk_output = Command::new("apk")
            .arg("info")
            .stdout(Stdio::piped())
            .output()
            .unwrap();

        extra::count_lines(
            String::from_utf8(apk_output.stdout)
                .expect("ERROR: \"apk info\" output was not valid UTF-8"),
        )
    }

    /// Returns the number of installed packages for systems
    /// that have `cargo` installed.
    fn count_cargo() -> Option<usize> {
        shared::count_cargo()
    }

    /// Returns the number of installed packages for systems
    /// that have `flatpak` installed.
    fn count_flatpak(home: &Path) -> Option<usize> {
        let mut total: usize = 0;
        let filter = Regex::new(r".*\.(Locale|Debug)").unwrap();
        for install in [
            Path::new("/var/lib/flatpak"),
            &home.join(".local/share/flatpak"),
        ] {
            if install.exists() {
                for dir in ["app", "runtime"] {
                    let pkgdir = install.join(dir);
                    if pkgdir.exists() {
                        for package in walkdir::WalkDir::new(&pkgdir)
                            .min_depth(1)
                            .max_depth(1)
                            .into_iter()
                            .filter_entry(|e| !filter.is_match(&e.path().to_string_lossy()))
                        {
                            total += walkdir::WalkDir::new(package.ok()?.path())
                                .min_depth(2)
                                .max_depth(2)
                                .into_iter()
                                .count();
                        }
                    }
                }
            }
        }
        if total > 0 {
            return Some(total);
        }

        None
    }

    /// Returns the number of installed packages for systems
    /// that have `snap` installed.
    fn count_snap() -> Option<usize> {
        let snap_dir = Path::new("/var/lib/snapd/snaps");
        if let Some(entries) = get_entries(snap_dir) {
            return Some(
                entries
                    .iter()
                    .filter(|&x| path_extension(x).unwrap_or_default() == "snap")
                    .count(),
            );
        }

        None
    }

    /// Returns the number of installed packages for systems
    /// that utilize `nix` as their package manager.
    fn count_nix() -> Option<usize> {
        return 'sqlite: {
            let db = "/nix/var/nix/db/db.sqlite";
            if !Path::new(db).is_file() {
                break 'sqlite None;
            }

            let connection = sqlite::Connection::open_with_flags(
                // The nix store is immutable, so we need to inform sqlite about it
                "file:".to_owned() + db + "?immutable=1",
                sqlite::OpenFlags::new().with_read_only().with_uri(),
            );

            if let Ok(con) = connection {
                let statement =
                    con.prepare("SELECT COUNT(path) FROM ValidPaths WHERE sigs IS NOT NULL");

                if let Ok(mut s) = statement {
                    if s.next().is_ok() {
                        break 'sqlite match s.read::<Option<i64>, _>(0) {
                            Ok(Some(count)) => Some(count as usize),
                            _ => None,
                        };
                    }
                }
            }

            None
        };
    }
}