Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fwupd/libfwupdplugin/fu-volume-locker.c
Line
Count
Source
1
/*
2
 * Copyright 2025 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
0
#define G_LOG_DOMAIN "FuVolumeLocker"
8
9
#include "config.h"
10
11
#include "fu-volume-locker.h"
12
13
/**
14
 * FuVolumeLocker:
15
 *
16
 * Easily unmount a volume when an object goes out of scope.
17
 *
18
 * See also: [class@FuVolume]
19
 */
20
21
struct _FuVolumeLocker {
22
  GObject parent_instance;
23
  FuVolume *volume;
24
  gboolean is_open;
25
};
26
27
0
G_DEFINE_TYPE(FuVolumeLocker, fu_volume_locker, G_TYPE_OBJECT)
28
0
29
0
/**
30
0
 * fu_volume_locker_close:
31
0
 * @self: a #FuVolumeLocker
32
0
 * @error: (nullable): optional return location for an error
33
0
 *
34
0
 * Closes the volume before it gets cleaned up.
35
0
 *
36
0
 * This function can be used to manually unmount a volume managed by a locker,
37
0
 * and allows the caller to properly handle the error.
38
0
 *
39
0
 * Returns: %TRUE for success
40
0
 **/
41
0
gboolean
42
0
fu_volume_locker_close(FuVolumeLocker *self, GError **error)
43
0
{
44
0
  g_autoptr(GError) error_local = NULL;
45
46
0
  g_return_val_if_fail(FU_IS_VOLUME_LOCKER(self), FALSE);
47
0
  g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
48
49
0
  if (!self->is_open)
50
0
    return TRUE;
51
0
  if (!fu_volume_unmount(self->volume, &error_local)) {
52
0
    if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND)) {
53
0
      g_debug("ignoring: %s", error_local->message);
54
0
      return TRUE;
55
0
    }
56
0
    g_propagate_error(error, g_steal_pointer(&error_local));
57
0
    return FALSE;
58
0
  }
59
60
  /* success */
61
0
  self->is_open = FALSE;
62
0
  return TRUE;
63
0
}
64
65
static void
66
fu_volume_locker_finalize(GObject *obj)
67
0
{
68
0
  FuVolumeLocker *self = FU_VOLUME_LOCKER(obj);
69
70
0
  if (self->is_open) {
71
0
    g_autoptr(GError) error_local = NULL;
72
0
    if (!fu_volume_unmount(self->volume, &error_local))
73
0
      g_warning("failed to close volume: %s", error_local->message);
74
0
  }
75
0
  if (self->volume != NULL)
76
0
    g_object_unref(self->volume);
77
0
  G_OBJECT_CLASS(fu_volume_locker_parent_class)->finalize(obj);
78
0
}
79
80
static void
81
fu_volume_locker_class_init(FuVolumeLockerClass *klass)
82
0
{
83
0
  GObjectClass *object_class = G_OBJECT_CLASS(klass);
84
0
  object_class->finalize = fu_volume_locker_finalize;
85
0
}
86
87
static void
88
fu_volume_locker_init(FuVolumeLocker *self)
89
0
{
90
0
}
91
92
/**
93
 * fu_volume_locker_new:
94
 * @volume: a #GObject
95
 * @error: (nullable): optional return location for an error
96
 *
97
 * Locks the volume, mounting it and unmounting it as required. If the volume is
98
 * already mounted then it is _not_ unmounted when the locker is closed.
99
 *
100
 * Returns: (transfer full): a volume locker if mounted, or %NULL
101
 **/
102
FuVolumeLocker *
103
fu_volume_locker_new(FuVolume *volume, GError **error)
104
0
{
105
0
  g_autoptr(FuVolumeLocker) self = g_object_new(FU_TYPE_VOLUME_LOCKER, NULL);
106
107
0
  g_return_val_if_fail(FU_IS_VOLUME(volume), NULL);
108
0
  g_return_val_if_fail(error == NULL || *error == NULL, NULL);
109
110
  /* already open, so NOP */
111
0
  if (fu_volume_is_mounted(volume))
112
0
    return g_steal_pointer(&self);
113
114
  /* open volume */
115
0
  if (!fu_volume_mount(volume, error)) {
116
0
    g_autoptr(GError) error_local = NULL;
117
0
    if (!fu_volume_unmount(volume, &error_local)) {
118
0
      if (!g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_NOTHING_TO_DO)) {
119
0
        g_debug("ignoring unmount error on aborted mount: %s",
120
0
          error_local->message);
121
0
      }
122
0
    }
123
0
    return NULL;
124
0
  }
125
126
  /* success */
127
0
  self->is_open = TRUE;
128
0
  self->volume = g_object_ref(volume);
129
0
  return g_steal_pointer(&self);
130
0
}