gphoto2 3.2.1

High-level wrapper for libgphoto2
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
//! Camera filesystem and storages

use crate::{
  file::{CameraFile, FileType},
  helper::{bitflags, char_slice_to_cow, to_c_string, UninitBox},
  list::{CameraList, FileListIter},
  task::Task,
  try_gp_internal, Camera, Error, Result,
};
use libgphoto2_sys::time_t;
use std::{borrow::Cow, ffi, fmt, fs, path::Path};

macro_rules! storage_info {
  ($(# $attr:tt)* $name:ident: $bitflag_ty:ident, |$inner:ident: $inner_ty:ident| { $($(# $field_attr:tt)* $field:ident: $ty:ty = $bitflag:ident, $expr:expr;)* }) => {
    $(# $attr)*
    #[repr(transparent)]
    #[derive(Clone)]
    pub struct $name {
      inner: libgphoto2_sys::$inner_ty,
    }

    impl $name {
      #[allow(dead_code)]
      pub(crate) fn from_inner_ref(ptr: &libgphoto2_sys::$inner_ty) -> &Self {
        let ptr: *const _ = ptr;
        // Safe because of repr(transparent).
        unsafe { &*ptr.cast::<Self>() }
      }

      $(
        $(# $field_attr)*
        pub fn $field(&self) -> Option<$ty> {
          let $inner = &self.inner;
          if ($inner.fields & libgphoto2_sys::$bitflag_ty::$bitflag).0 != 0 {
            Some($expr)
          } else {
            None
          }
        }
      )*
    }

    impl fmt::Debug for $name {
      fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct(stringify!($name))
          $(
            .field(stringify!($field), &self.$field())
          )*
          .finish()
      }
    }
  };
}

/// Hardware storage type
#[derive(Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum StorageType {
  /// Unknown storage type
  Unknown,
  /// Fixed ROM storage
  FixedRom,
  /// Removable ROM storage
  RemovableRom,
  /// Fixed RAM
  FixedRam,
  /// Removable RAM storage (sd cards)
  RemovableRam,
}

/// Type of the filesystem hierarchy
#[derive(Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum FilesystemType {
  /// Unknown filesystem type
  Unknown,
  /// Flat filesystem (all in one directory)
  Flat,
  /// Tree hierarchy
  Tree,
  /// DCIM style filesystem
  Dcf,
}

/// Access types of storage
#[derive(Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum AccessType {
  /// Read/Write
  Rw,
  /// Read only
  Ro,
  /// Read only with delete
  RoDelete,
}

bitflags!(
  /// Status of [`CameraFile`].
  FileStatus = CameraFileStatus {
    /// This file has been downloaded.
    downloaded: GP_FILE_STATUS_DOWNLOADED,
  }
);

bitflags!(
  /// Permissions of a [`CameraFile`]
  FilePermissions = CameraFilePermissions {
    /// File can be read
    read: GP_FILE_PERM_READ,

    /// File can be deleted
    delete: GP_FILE_PERM_DELETE,
  }
);

storage_info!(
  /// Image thumbnail information
  FileInfoPreview: CameraFileInfoFields, |info: CameraFileInfoPreview| {
    /// Status of the preview file
    status: FileStatus = GP_FILE_INFO_STATUS, info.status.into();
    /// Size of the preview file
    size: u64 = GP_FILE_INFO_SIZE, info.size;
    /// Mime type of the preview file
    mime_type: Cow<str> = GP_FILE_INFO_TYPE, char_slice_to_cow(&info.type_);
    /// Image width,
    width: u32 = GP_FILE_INFO_WIDTH, info.width;
    /// Image height
    height: u32 = GP_FILE_INFO_HEIGHT, info.height;
  }
);

storage_info!(
  /// Info for image file
  FileInfoFile: CameraFileInfoFields, |info: CameraFileInfoFile| {
    /// Status of the file
    status: FileStatus = GP_FILE_INFO_STATUS, info.status.into();
    /// File size
    size: u64 = GP_FILE_INFO_SIZE, info.size;
    /// Mime type
    mime_type: Cow<str> = GP_FILE_INFO_TYPE, char_slice_to_cow(&info.type_);
    /// Image width
    width: u32 = GP_FILE_INFO_WIDTH, info.width;
    /// Image height
    height: u32 = GP_FILE_INFO_HEIGHT, info.height;
    /// Image permissions
    permissions: FilePermissions = GP_FILE_INFO_PERMISSIONS, info.permissions.into();
    /// File modification time
    mtime: time_t = GP_FILE_INFO_MTIME, info.mtime;
  }
);

storage_info!(
  /// Info for file audio data
  FileInfoAudio: CameraFileInfoFields, |info: CameraFileInfoAudio| {
    /// Status of the audio file
    status: FileStatus = GP_FILE_INFO_STATUS, info.status.into();
    /// Size of the audio file
    size: u64 = GP_FILE_INFO_SIZE, info.size;
    /// Mime type of the audio
    mime_type: Cow<str> = GP_FILE_INFO_TYPE, char_slice_to_cow(&info.type_);
  }
);

/// File information for preview, normal file and audio
pub struct FileInfo {
  // It's fairly large, so we want to keep it on the heap.
  pub(crate) inner: Box<libgphoto2_sys::CameraFileInfo>,
}

impl FileInfo {
  /// Info for file preview
  pub fn preview(&self) -> &FileInfoPreview {
    FileInfoPreview::from_inner_ref(&self.inner.preview)
  }

  /// Info for normal file
  pub fn file(&self) -> &FileInfoFile {
    FileInfoFile::from_inner_ref(&self.inner.file)
  }

  /// Info for file audio
  pub fn audio(&self) -> &FileInfoAudio {
    FileInfoAudio::from_inner_ref(&self.inner.audio)
  }
}

impl fmt::Debug for FileInfo {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("FileInfo")
      .field("preview", &self.preview())
      .field("file", &self.file())
      .field("audio", &self.audio())
      .finish()
  }
}

/// File system actions for a camera
pub struct CameraFS<'a> {
  pub(crate) camera: &'a Camera,
}

impl From<libgphoto2_sys::CameraStorageType> for StorageType {
  fn from(storage_type: libgphoto2_sys::CameraStorageType) -> Self {
    use libgphoto2_sys::CameraStorageType;

    match storage_type {
      CameraStorageType::GP_STORAGEINFO_ST_UNKNOWN => Self::Unknown,
      CameraStorageType::GP_STORAGEINFO_ST_FIXED_ROM => Self::FixedRom,
      CameraStorageType::GP_STORAGEINFO_ST_REMOVABLE_ROM => Self::RemovableRom,
      CameraStorageType::GP_STORAGEINFO_ST_FIXED_RAM => Self::FixedRam,
      CameraStorageType::GP_STORAGEINFO_ST_REMOVABLE_RAM => Self::RemovableRam,
    }
  }
}

impl From<libgphoto2_sys::CameraStorageFilesystemType> for FilesystemType {
  fn from(fs_type: libgphoto2_sys::CameraStorageFilesystemType) -> Self {
    use libgphoto2_sys::CameraStorageFilesystemType as GPFsType;

    match fs_type {
      GPFsType::GP_STORAGEINFO_FST_UNDEFINED => Self::Unknown,
      GPFsType::GP_STORAGEINFO_FST_GENERICFLAT => Self::Flat,
      GPFsType::GP_STORAGEINFO_FST_GENERICHIERARCHICAL => Self::Tree,
      GPFsType::GP_STORAGEINFO_FST_DCF => Self::Dcf,
    }
  }
}

impl From<libgphoto2_sys::CameraStorageAccessType> for AccessType {
  fn from(access_type: libgphoto2_sys::CameraStorageAccessType) -> Self {
    use libgphoto2_sys::CameraStorageAccessType as GPAccessType;

    match access_type {
      GPAccessType::GP_STORAGEINFO_AC_READWRITE => Self::Rw,
      GPAccessType::GP_STORAGEINFO_AC_READONLY => Self::Ro,
      GPAccessType::GP_STORAGEINFO_AC_READONLY_WITH_DELETE => Self::RoDelete,
    }
  }
}

storage_info!(
  /// Information about a storage on the camera
  StorageInfo: CameraStorageInfoFields, |info: CameraStorageInformation| {
    /// Label of the storage
    label: Cow<str> = GP_STORAGEINFO_LABEL, char_slice_to_cow(&info.label);
    /// Base directory of the storage. If there is only 1 storage on the camera it will be "/"
    base_directory: Cow<str> = GP_STORAGEINFO_BASE, char_slice_to_cow(&info.basedir);
    /// Description of the storage
    description: Cow<str> = GP_STORAGEINFO_DESCRIPTION, char_slice_to_cow(&info.description);
    /// Type of the storage
    storage_type: StorageType = GP_STORAGEINFO_STORAGETYPE, info.type_.into();
    /// Type of the filesystem on the storage
    filesystem_type: FilesystemType = GP_STORAGEINFO_FILESYSTEMTYPE, info.fstype.into();
    /// Access permissions
    access_type: AccessType = GP_STORAGEINFO_ACCESS, info.access.into();
    /// Total storage capacity in Kilobytes
    capacity_kb: u64 = GP_STORAGEINFO_MAXCAPACITY, info.capacitykbytes * 1024;
    /// Free storage in Kilobytes
    free_kb: u64 = GP_STORAGEINFO_FREESPACEKBYTES, info.freekbytes * 1024;
    /// Number of images that fit in free space (guessed by the camera)
    free_images: u64 = GP_STORAGEINFO_FREESPACEIMAGES, info.freeimages;
  }
);

impl<'a> CameraFS<'a> {
  pub(crate) fn new(camera: &'a Camera) -> Self {
    Self { camera }
  }

  /// Delete a file
  pub fn delete_file(&self, folder: &str, file: &str) -> Task<Result<()>> {
    let camera = self.camera.camera;
    let context = self.camera.context.inner;
    let (folder, file) = (folder.to_owned(), file.to_owned());

    unsafe {
      Task::new(move || {
        try_gp_internal!(gp_camera_file_delete(
          *camera,
          to_c_string!(folder),
          to_c_string!(file),
          *context
        )?);
        Ok(())
      })
    }
    .context(context)
  }

  /// Get information of a file
  pub fn file_info(&self, folder: &str, file: &str) -> Task<Result<FileInfo>> {
    let camera = self.camera.camera;
    let context = self.camera.context.inner;
    let (folder, file) = (folder.to_owned(), file.to_owned());

    unsafe {
      Task::new(move || {
        let mut inner = UninitBox::uninit();

        try_gp_internal!(gp_camera_file_get_info(
          *camera,
          to_c_string!(folder),
          to_c_string!(file),
          inner.as_mut_ptr(),
          *context
        )?);

        Ok(FileInfo { inner: inner.assume_init() })
      })
    }
    .context(context)
  }

  /// Downloads a file from the camera
  pub fn download_to(&self, folder: &str, file: &str, path: &Path) -> Task<Result<CameraFile>> {
    self.to_camera_file(folder, file, FileType::Normal, Some(path))
  }

  /// Downloads a camera file to memory
  pub fn download(&self, folder: &str, file: &str) -> Task<Result<CameraFile>> {
    self.to_camera_file(folder, file, FileType::Normal, None)
  }

  /// Downloads a preview into memory
  pub fn download_preview(&self, folder: &str, file: &str) -> Task<Result<CameraFile>> {
    self.to_camera_file(folder, file, FileType::Preview, None)
  }

  /// Downloads the EXIF block into memory
  pub fn download_exif(&self, folder: &str, file: &str) -> Task<Result<CameraFile>> {
    self.to_camera_file(folder, file, FileType::Exif, None)
  }

  /// Upload a file to the camera
  #[allow(clippy::boxed_local)]
  pub fn upload_file(&self, folder: &str, filename: &str, data: Box<[u8]>) -> Task<Result<()>> {
    let camera = self.camera.camera;
    let context = self.camera.context.inner;

    let (folder, filename) = (folder.to_owned(), filename.to_owned());

    unsafe {
      Task::new(move || {
        try_gp_internal!(gp_file_new(&out file)?);
        try_gp_internal!(gp_file_append(file, data.as_ptr().cast(), data.len().try_into()?)?);
        try_gp_internal!(gp_camera_folder_put_file(
          *camera,
          to_c_string!(folder),
          to_c_string!(filename),
          FileType::Normal.into(),
          file,
          *context
        )?);

        Ok(())
      })
    }
    .context(context)
  }

  /// Delete all files in a folder
  pub fn delete_all_in_folder(&self, folder: &str) -> Task<Result<()>> {
    let camera = self.camera.camera;
    let context = self.camera.context.inner;
    let folder = folder.to_owned();

    unsafe {
      Task::new(move || {
        try_gp_internal!(gp_camera_folder_delete_all(*camera, to_c_string!(folder), *context)?);
        Ok(())
      })
    }
    .context(context)
  }

  /// List files in a folder
  pub fn list_files(&self, folder: &str) -> Task<Result<FileListIter>> {
    let camera = self.camera.camera;
    let context = self.camera.context.inner;

    let folder = folder.to_owned();

    unsafe {
      Task::new(move || {
        let file_list = CameraList::new()?;

        try_gp_internal!(gp_camera_folder_list_files(
          *camera,
          to_c_string!(folder),
          *file_list.inner,
          *context
        )?);

        Ok(FileListIter::new(file_list))
      })
    }
    .context(context)
  }

  /// List folders in a folder
  pub fn list_folders(&self, folder: &str) -> Task<Result<FileListIter>> {
    let camera = self.camera.camera;
    let context = self.camera.context.inner;

    let folder = folder.to_owned();

    unsafe {
      Task::new(move || {
        let folder_list = CameraList::new()?;

        try_gp_internal!(gp_camera_folder_list_folders(
          *camera,
          to_c_string!(folder),
          *folder_list.inner,
          *context
        )?);

        Ok(FileListIter::new(folder_list))
      })
    }
    .context(context)
  }

  /// Creates a new folder
  pub fn create_directory(&self, parent_folder: &str, new_folder: &str) -> Task<Result<()>> {
    let (parent_folder, new_folder) = (parent_folder.to_owned(), new_folder.to_owned());
    let camera = self.camera.camera;
    let context = self.camera.context.inner;

    unsafe {
      Task::new(move || {
        try_gp_internal!(gp_camera_folder_make_dir(
          *camera,
          to_c_string!(parent_folder),
          to_c_string!(new_folder),
          *context
        )?);

        Ok(())
      })
    }
    .context(context)
  }

  /// Removes a folder
  pub fn remove_directory(&self, parent: &str, to_remove: &str) -> Task<Result<()>> {
    let (parent, to_remove) = (parent.to_owned(), to_remove.to_owned());
    let camera = self.camera.camera;
    let context = self.camera.context.inner;

    unsafe {
      Task::new(move || {
        try_gp_internal!(gp_camera_folder_remove_dir(
          *camera,
          to_c_string!(parent),
          to_c_string!(to_remove),
          *context
        )?);

        Ok(())
      })
    }
    .context(context)
  }
}

/// Private implementations
impl CameraFS<'_> {
  fn to_camera_file(
    &self,
    folder: &str,
    file: &str,
    type_: FileType,
    path: Option<&Path>,
  ) -> Task<Result<CameraFile>> {
    let (folder, file, path) = (folder.to_owned(), file.to_owned(), path.map(ToOwned::to_owned));
    let camera = self.camera.camera;
    let context = self.camera.context.inner;

    unsafe {
      Task::new(move || {
        let camera_file = match &path {
          Some(dest_path) => CameraFile::new_file(dest_path)?,
          None => CameraFile::new()?,
        };

        try_gp_internal!(gp_camera_file_get(
          *camera,
          to_c_string!(folder),
          to_c_string!(file),
          type_.into(),
          *camera_file.inner,
          *context
        )
        .map_err(|e| {
          if let Some(path) = path {
            if let Err(error) = fs::remove_file(path) {
              return Into::<Error>::into(error);
            }
          }

          e
        })?);

        Ok(camera_file)
      })
    }
    .context(context)
  }
}