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
use crate::{RepoCheckoutMode, RepoCheckoutOverwriteMode, RepoDevInoCache, SePolicy};
use glib::translate::*;
use libc::c_char;
use ostree_sys::*;
use std::path::PathBuf;

#[cfg(any(feature = "v2018_2", feature = "dox"))]
mod repo_checkout_filter;
#[cfg(any(feature = "v2018_2", feature = "dox"))]
pub use self::repo_checkout_filter::RepoCheckoutFilter;

pub struct RepoCheckoutAtOptions {
    pub mode: RepoCheckoutMode,
    pub overwrite_mode: RepoCheckoutOverwriteMode,
    pub enable_uncompressed_cache: bool,
    pub enable_fsync: bool,
    pub process_whiteouts: bool,
    pub no_copy_fallback: bool,
    #[cfg(any(feature = "v2017_6", feature = "dox"))]
    pub force_copy: bool,
    #[cfg(any(feature = "v2017_7", feature = "dox"))]
    pub bareuseronly_dirs: bool,
    #[cfg(any(feature = "v2018_9", feature = "dox"))]
    pub force_copy_zerosized: bool,
    pub subpath: Option<PathBuf>,
    pub devino_to_csum_cache: Option<RepoDevInoCache>,
    /// A callback function to decide which files and directories will be checked out from the
    /// repo. See the documentation on [RepoCheckoutFilter](struct.RepoCheckoutFilter.html) for more
    /// information on the signature.
    ///
    /// # Panics
    /// This callback may not panic. If it does, `abort()` will be called to avoid unwinding across
    /// an FFI boundary and into the libostree C code (which is Undefined Behavior). If you prefer to
    /// swallow the panic rather than aborting, you can use `std::panic::catch_unwind` inside your
    /// callback to catch and silence any panics that occur.
    #[cfg(any(feature = "v2018_2", feature = "dox"))]
    pub filter: Option<RepoCheckoutFilter>,
    #[cfg(any(feature = "v2017_6", feature = "dox"))]
    pub sepolicy: Option<SePolicy>,
    pub sepolicy_prefix: Option<String>,
}

impl Default for RepoCheckoutAtOptions {
    fn default() -> Self {
        RepoCheckoutAtOptions {
            mode: RepoCheckoutMode::None,
            overwrite_mode: RepoCheckoutOverwriteMode::None,
            enable_uncompressed_cache: false,
            enable_fsync: false,
            process_whiteouts: false,
            no_copy_fallback: false,
            #[cfg(feature = "v2017_6")]
            force_copy: false,
            #[cfg(feature = "v2017_7")]
            bareuseronly_dirs: false,
            #[cfg(feature = "v2018_9")]
            force_copy_zerosized: false,
            subpath: None,
            devino_to_csum_cache: None,
            #[cfg(feature = "v2018_2")]
            filter: None,
            #[cfg(feature = "v2017_6")]
            sepolicy: None,
            sepolicy_prefix: None,
        }
    }
}

type StringStash<'a, T> = Stash<'a, *const c_char, Option<T>>;
type WrapperStash<'a, GlibT, WrappedT> = Stash<'a, *mut GlibT, Option<WrappedT>>;

impl<'a> ToGlibPtr<'a, *const OstreeRepoCheckoutAtOptions> for RepoCheckoutAtOptions {
    #[allow(clippy::type_complexity)]
    type Storage = (
        Box<OstreeRepoCheckoutAtOptions>,
        StringStash<'a, PathBuf>,
        StringStash<'a, String>,
        WrapperStash<'a, OstreeRepoDevInoCache, RepoDevInoCache>,
        WrapperStash<'a, OstreeSePolicy, SePolicy>,
    );

    // We need to make sure that all memory pointed to by the returned pointer is kept alive by
    // either the `self` reference or the returned Stash.
    fn to_glib_none(&'a self) -> Stash<*const OstreeRepoCheckoutAtOptions, Self> {
        // Creating this struct from zeroed memory is fine since it's `repr(C)` and only contains
        // primitive types. In fact, the libostree docs say to zero the struct. This means we handle
        // the unused bytes correctly.
        // The struct needs to be boxed so the pointer we return remains valid even as the Stash is
        // moved around.
        let mut options = Box::new(unsafe { std::mem::zeroed::<OstreeRepoCheckoutAtOptions>() });
        options.mode = self.mode.to_glib();
        options.overwrite_mode = self.overwrite_mode.to_glib();
        options.enable_uncompressed_cache = self.enable_uncompressed_cache.to_glib();
        options.enable_fsync = self.enable_fsync.to_glib();
        options.process_whiteouts = self.process_whiteouts.to_glib();
        options.no_copy_fallback = self.no_copy_fallback.to_glib();

        #[cfg(feature = "v2017_6")]
        {
            options.force_copy = self.force_copy.to_glib();
        }

        #[cfg(feature = "v2017_7")]
        {
            options.bareuseronly_dirs = self.bareuseronly_dirs.to_glib();
        }

        #[cfg(feature = "v2018_9")]
        {
            options.force_copy_zerosized = self.force_copy_zerosized.to_glib();
        }

        // We keep these complex values alive by returning them in our Stash. Technically, some of
        // these are being kept alive by `self` already, but it's better to be consistent here.
        let subpath = self.subpath.to_glib_none();
        options.subpath = subpath.0;
        let sepolicy_prefix = self.sepolicy_prefix.to_glib_none();
        options.sepolicy_prefix = sepolicy_prefix.0;
        let devino_to_csum_cache = self.devino_to_csum_cache.to_glib_none();
        options.devino_to_csum_cache = devino_to_csum_cache.0;

        #[cfg(feature = "v2017_6")]
        let sepolicy = {
            let sepolicy = self.sepolicy.to_glib_none();
            options.sepolicy = sepolicy.0;
            sepolicy
        };
        #[cfg(not(feature = "v2017_6"))]
        let sepolicy = None.to_glib_none();

        #[cfg(feature = "v2018_2")]
        {
            if let Some(filter) = &self.filter {
                options.filter_user_data = filter.to_glib_none().0;
                options.filter = Some(repo_checkout_filter::filter_trampoline_unwindsafe);
            }
        }

        Stash(
            options.as_ref(),
            (
                options,
                subpath,
                sepolicy_prefix,
                devino_to_csum_cache,
                sepolicy,
            ),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use glib_sys::{GFALSE, GTRUE};
    use std::ffi::{CStr, CString};
    use std::ptr;

    #[test]
    fn should_convert_default_options() {
        let options = RepoCheckoutAtOptions::default();
        let stash = options.to_glib_none();
        let ptr = stash.0;
        unsafe {
            assert_eq!((*ptr).mode, OSTREE_REPO_CHECKOUT_MODE_NONE);
            assert_eq!((*ptr).overwrite_mode, OSTREE_REPO_CHECKOUT_OVERWRITE_NONE);
            assert_eq!((*ptr).enable_uncompressed_cache, GFALSE);
            assert_eq!((*ptr).enable_fsync, GFALSE);
            assert_eq!((*ptr).process_whiteouts, GFALSE);
            assert_eq!((*ptr).no_copy_fallback, GFALSE);
            #[cfg(feature = "v2017_6")]
            assert_eq!((*ptr).force_copy, GFALSE);
            #[cfg(feature = "v2017_7")]
            assert_eq!((*ptr).bareuseronly_dirs, GFALSE);
            #[cfg(feature = "v2018_9")]
            assert_eq!((*ptr).force_copy_zerosized, GFALSE);
            assert_eq!((*ptr).unused_bools, [GFALSE; 4]);
            assert_eq!((*ptr).subpath, ptr::null());
            assert_eq!((*ptr).devino_to_csum_cache, ptr::null_mut());
            assert_eq!((*ptr).unused_ints, [0; 6]);
            assert_eq!((*ptr).unused_ptrs, [ptr::null_mut(); 3]);
            #[cfg(feature = "v2018_2")]
            assert_eq!((*ptr).filter, None);
            #[cfg(feature = "v2018_2")]
            assert_eq!((*ptr).filter_user_data, ptr::null_mut());
            #[cfg(feature = "v2017_6")]
            assert_eq!((*ptr).sepolicy, ptr::null_mut());
            assert_eq!((*ptr).sepolicy_prefix, ptr::null());
        }
    }

    #[test]
    fn should_convert_non_default_options() {
        let options = RepoCheckoutAtOptions {
            mode: RepoCheckoutMode::User,
            overwrite_mode: RepoCheckoutOverwriteMode::UnionIdentical,
            enable_uncompressed_cache: true,
            enable_fsync: true,
            process_whiteouts: true,
            no_copy_fallback: true,
            #[cfg(feature = "v2017_6")]
            force_copy: true,
            #[cfg(feature = "v2017_7")]
            bareuseronly_dirs: true,
            #[cfg(feature = "v2018_9")]
            force_copy_zerosized: true,
            subpath: Some("sub/path".into()),
            devino_to_csum_cache: Some(RepoDevInoCache::new()),
            #[cfg(feature = "v2018_2")]
            filter: RepoCheckoutFilter::new(|_repo, _path, _stat| {
                crate::RepoCheckoutFilterResult::Skip
            }),
            #[cfg(feature = "v2017_6")]
            sepolicy: Some(
                SePolicy::new(&gio::File::new_for_path("a/b"), gio::NONE_CANCELLABLE).unwrap(),
            ),
            sepolicy_prefix: Some("prefix".into()),
        };
        let stash = options.to_glib_none();
        let ptr = stash.0;
        unsafe {
            assert_eq!((*ptr).mode, OSTREE_REPO_CHECKOUT_MODE_USER);
            assert_eq!(
                (*ptr).overwrite_mode,
                OSTREE_REPO_CHECKOUT_OVERWRITE_UNION_IDENTICAL
            );
            assert_eq!((*ptr).enable_uncompressed_cache, GTRUE);
            assert_eq!((*ptr).enable_fsync, GTRUE);
            assert_eq!((*ptr).process_whiteouts, GTRUE);
            assert_eq!((*ptr).no_copy_fallback, GTRUE);
            #[cfg(feature = "v2017_6")]
            assert_eq!((*ptr).force_copy, GTRUE);
            #[cfg(feature = "v2017_7")]
            assert_eq!((*ptr).bareuseronly_dirs, GTRUE);
            #[cfg(feature = "v2018_9")]
            assert_eq!((*ptr).force_copy_zerosized, GTRUE);
            assert_eq!((*ptr).unused_bools, [GFALSE; 4]);
            assert_eq!(
                CStr::from_ptr((*ptr).subpath),
                CString::new("sub/path").unwrap().as_c_str()
            );
            assert_eq!(
                (*ptr).devino_to_csum_cache,
                options.devino_to_csum_cache.to_glib_none().0
            );
            assert_eq!((*ptr).unused_ints, [0; 6]);
            assert_eq!((*ptr).unused_ptrs, [ptr::null_mut(); 3]);
            #[cfg(feature = "v2018_2")]
            assert!((*ptr).filter == Some(repo_checkout_filter::filter_trampoline_unwindsafe));
            #[cfg(feature = "v2018_2")]
            assert_eq!(
                (*ptr).filter_user_data,
                options.filter.as_ref().unwrap().to_glib_none().0,
            );
            #[cfg(feature = "v2017_6")]
            assert_eq!((*ptr).sepolicy, options.sepolicy.to_glib_none().0);
            assert_eq!(
                CStr::from_ptr((*ptr).sepolicy_prefix),
                CString::new("prefix").unwrap().as_c_str()
            );
        }
    }
}