gio/
file_info.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{
4    mem,
5    time::{Duration, SystemTime},
6};
7
8use glib::{translate::*, StrV};
9
10use crate::{ffi, FileInfo};
11
12impl FileInfo {
13    /// Gets the modification time of the current @self and sets it
14    /// in @result.
15    ///
16    /// It is an error to call this if the #GFileInfo does not contain
17    /// [`FILE_ATTRIBUTE_TIME_MODIFIED`][crate::FILE_ATTRIBUTE_TIME_MODIFIED]. If [`FILE_ATTRIBUTE_TIME_MODIFIED_USEC`][crate::FILE_ATTRIBUTE_TIME_MODIFIED_USEC] is
18    /// provided it will be used too.
19    ///
20    /// # Deprecated since 2.62
21    ///
22    /// Use g_file_info_get_modification_date_time() instead, as
23    ///    #GTimeVal is deprecated due to the year 2038 problem.
24    ///
25    /// # Returns
26    ///
27    ///
28    /// ## `result`
29    /// a #GTimeVal.
30    #[cfg_attr(feature = "v2_62", deprecated)]
31    #[doc(alias = "g_file_info_get_modification_time")]
32    #[doc(alias = "get_modification_time")]
33    pub fn modification_time(&self) -> SystemTime {
34        unsafe {
35            let mut result = mem::MaybeUninit::uninit();
36            ffi::g_file_info_get_modification_time(self.to_glib_none().0, result.as_mut_ptr());
37            let result = result.assume_init();
38
39            if result.tv_sec > 0 {
40                let duration = Duration::from_secs(result.tv_sec as u64)
41                    + Duration::from_millis(result.tv_usec as u64);
42                SystemTime::UNIX_EPOCH + duration
43            } else {
44                let duration = Duration::from_secs((-result.tv_sec) as u64)
45                    + Duration::from_millis(result.tv_usec as u64);
46                SystemTime::UNIX_EPOCH - duration
47            }
48        }
49    }
50
51    /// Sets the [`FILE_ATTRIBUTE_TIME_MODIFIED`][crate::FILE_ATTRIBUTE_TIME_MODIFIED] and
52    /// [`FILE_ATTRIBUTE_TIME_MODIFIED_USEC`][crate::FILE_ATTRIBUTE_TIME_MODIFIED_USEC] attributes in the file info to the
53    /// given time value.
54    ///
55    /// [`FILE_ATTRIBUTE_TIME_MODIFIED_NSEC`][crate::FILE_ATTRIBUTE_TIME_MODIFIED_NSEC] will be cleared.
56    ///
57    /// # Deprecated since 2.62
58    ///
59    /// Use g_file_info_set_modification_date_time() instead, as
60    ///    #GTimeVal is deprecated due to the year 2038 problem.
61    /// ## `mtime`
62    /// a #GTimeVal.
63    #[cfg_attr(feature = "v2_62", deprecated)]
64    #[doc(alias = "g_file_info_set_modification_time")]
65    pub fn set_modification_time(&self, mtime: SystemTime) {
66        let diff = mtime
67            .duration_since(SystemTime::UNIX_EPOCH)
68            .expect("failed to convert time");
69        unsafe {
70            ffi::g_file_info_set_modification_time(
71                self.to_glib_none().0,
72                mut_override(&glib::ffi::GTimeVal {
73                    tv_sec: diff.as_secs() as _,
74                    tv_usec: diff.subsec_micros() as _,
75                }),
76            );
77        }
78    }
79
80    /// Gets the value of a stringv attribute. If the attribute does
81    /// not contain a stringv, [`None`] will be returned.
82    /// ## `attribute`
83    /// a file attribute key.
84    ///
85    /// # Returns
86    ///
87    /// the contents of the @attribute value as a stringv,
88    /// or [`None`] otherwise. Do not free. These returned strings are UTF-8.
89    #[doc(alias = "g_file_info_get_attribute_stringv")]
90    #[doc(alias = "get_attribute_stringv")]
91    pub fn attribute_stringv(&self, attribute: &str) -> StrV {
92        unsafe {
93            FromGlibPtrContainer::from_glib_none(ffi::g_file_info_get_attribute_stringv(
94                self.to_glib_none().0,
95                attribute.to_glib_none().0,
96            ))
97        }
98    }
99
100    /// Sets the @attribute to contain the given @attr_value,
101    /// if possible.
102    ///
103    /// Sinze: 2.22
104    /// ## `attribute`
105    /// a file attribute key
106    /// ## `attr_value`
107    /// a [`None`]
108    ///   terminated array of UTF-8 strings.
109    #[doc(alias = "g_file_info_set_attribute_stringv")]
110    pub fn set_attribute_stringv(&self, attribute: &str, attr_value: impl IntoStrV) {
111        unsafe {
112            attr_value.run_with_strv(|attr_value| {
113                ffi::g_file_info_set_attribute_stringv(
114                    self.to_glib_none().0,
115                    attribute.to_glib_none().0,
116                    attr_value.as_ptr() as *mut _,
117                );
118            });
119        }
120    }
121}