gio/
file_info.rs

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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{
    mem,
    time::{Duration, SystemTime},
};

use glib::{translate::*, StrV};

use crate::{ffi, FileInfo};

impl FileInfo {
    /// Gets the modification time of the current @self and sets it
    /// in @result.
    ///
    /// It is an error to call this if the #GFileInfo does not contain
    /// [`FILE_ATTRIBUTE_TIME_MODIFIED`][crate::FILE_ATTRIBUTE_TIME_MODIFIED]. If [`FILE_ATTRIBUTE_TIME_MODIFIED_USEC`][crate::FILE_ATTRIBUTE_TIME_MODIFIED_USEC] is
    /// provided it will be used too.
    ///
    /// # Deprecated since 2.62
    ///
    /// Use g_file_info_get_modification_date_time() instead, as
    ///    #GTimeVal is deprecated due to the year 2038 problem.
    ///
    /// # Returns
    ///
    ///
    /// ## `result`
    /// a #GTimeVal.
    #[cfg_attr(feature = "v2_62", deprecated)]
    #[doc(alias = "g_file_info_get_modification_time")]
    #[doc(alias = "get_modification_time")]
    pub fn modification_time(&self) -> SystemTime {
        unsafe {
            let mut result = mem::MaybeUninit::uninit();
            ffi::g_file_info_get_modification_time(self.to_glib_none().0, result.as_mut_ptr());
            let result = result.assume_init();

            if result.tv_sec > 0 {
                let duration = Duration::from_secs(result.tv_sec as u64)
                    + Duration::from_millis(result.tv_usec as u64);
                SystemTime::UNIX_EPOCH + duration
            } else {
                let duration = Duration::from_secs((-result.tv_sec) as u64)
                    + Duration::from_millis(result.tv_usec as u64);
                SystemTime::UNIX_EPOCH - duration
            }
        }
    }

    /// Sets the [`FILE_ATTRIBUTE_TIME_MODIFIED`][crate::FILE_ATTRIBUTE_TIME_MODIFIED] and
    /// [`FILE_ATTRIBUTE_TIME_MODIFIED_USEC`][crate::FILE_ATTRIBUTE_TIME_MODIFIED_USEC] attributes in the file info to the
    /// given time value.
    ///
    /// [`FILE_ATTRIBUTE_TIME_MODIFIED_NSEC`][crate::FILE_ATTRIBUTE_TIME_MODIFIED_NSEC] will be cleared.
    ///
    /// # Deprecated since 2.62
    ///
    /// Use g_file_info_set_modification_date_time() instead, as
    ///    #GTimeVal is deprecated due to the year 2038 problem.
    /// ## `mtime`
    /// a #GTimeVal.
    #[cfg_attr(feature = "v2_62", deprecated)]
    #[doc(alias = "g_file_info_set_modification_time")]
    pub fn set_modification_time(&self, mtime: SystemTime) {
        let diff = mtime
            .duration_since(SystemTime::UNIX_EPOCH)
            .expect("failed to convert time");
        unsafe {
            ffi::g_file_info_set_modification_time(
                self.to_glib_none().0,
                mut_override(&glib::ffi::GTimeVal {
                    tv_sec: diff.as_secs() as _,
                    tv_usec: diff.subsec_micros() as _,
                }),
            );
        }
    }

    /// Gets the value of a stringv attribute. If the attribute does
    /// not contain a stringv, [`None`] will be returned.
    /// ## `attribute`
    /// a file attribute key.
    ///
    /// # Returns
    ///
    /// the contents of the @attribute value as a stringv,
    /// or [`None`] otherwise. Do not free. These returned strings are UTF-8.
    #[doc(alias = "g_file_info_get_attribute_stringv")]
    #[doc(alias = "get_attribute_stringv")]
    pub fn attribute_stringv(&self, attribute: &str) -> StrV {
        unsafe {
            FromGlibPtrContainer::from_glib_none(ffi::g_file_info_get_attribute_stringv(
                self.to_glib_none().0,
                attribute.to_glib_none().0,
            ))
        }
    }

    /// Sets the @attribute to contain the given @attr_value,
    /// if possible.
    ///
    /// Sinze: 2.22
    /// ## `attribute`
    /// a file attribute key
    /// ## `attr_value`
    /// a [`None`]
    ///   terminated array of UTF-8 strings.
    #[doc(alias = "g_file_info_set_attribute_stringv")]
    pub fn set_attribute_stringv(&self, attribute: &str, attr_value: impl IntoStrV) {
        unsafe {
            attr_value.run_with_strv(|attr_value| {
                ffi::g_file_info_set_attribute_stringv(
                    self.to_glib_none().0,
                    attribute.to_glib_none().0,
                    attr_value.as_ptr() as *mut _,
                );
            });
        }
    }
}