| 1 |
# Developer Documentation |
| 2 |
|
| 3 |
This document describes some aspects of GVfs architecture and explains reasons |
| 4 |
why things are done the way they are. It is intended for developers as a |
| 5 |
starting point, to help easily find the area to look at. Most information can |
| 6 |
be read from the sources and I'm also not going to describe all details here. |
| 7 |
|
| 8 |
## What it is and what it does |
| 9 |
|
| 10 |
GVfs is a set of libraries and daemons extending the GIO API. In fact, it's an |
| 11 |
extension point, default GIO implementation of non-local services. It's designed |
| 12 |
to provide information suitable for usage in the GUI, that means in a polite and |
| 13 |
filtered way. |
| 14 |
|
| 15 |
It provides everything needed for accessing files on remote servers (networking) |
| 16 |
or devices attached to the physical computer. It also takes care of local |
| 17 |
storage management. |
| 18 |
|
| 19 |
## Requirements, running and usage |
| 20 |
|
| 21 |
The only requirement is a running user D-Bus session, typically started with the |
| 22 |
desktop session on login. When using GVfs in an isolated shell (console, SSH |
| 23 |
login), make sure to start the D-Bus session (the `dbus-launch` command, e.g. |
| 24 |
`export $(dbus-launch)`). Active GVfs mounts are only available to the specific |
| 25 |
session, making it work nicely in a concurrent environment like multi-seat |
| 26 |
systems or terminal servers. |
| 27 |
|
| 28 |
Everything is autostarted on-demand on first access through GIO. No need to |
| 29 |
start any daemon explicitly. Some mounts may require credentials, this is done |
| 30 |
using standard `GMountOperation` object. |
| 31 |
|
| 32 |
All daemons are listening to session bus changes and will exit once the bus is |
| 33 |
torn down. Should this not be true, please file a bug. |
| 34 |
|
| 35 |
## Environment variables |
| 36 |
|
| 37 |
See also |
| 38 |
[Running GIO applications](https://docs.gtk.org/gio/overview.html#running-gio-applications) |
| 39 |
in the GIO Reference Manual for common GIO environment variables. |
| 40 |
|
| 41 |
The following environment variables can be used for controlling certain aspects |
| 42 |
of some services: |
| 43 |
|
| 44 |
* `DBUS_SESSION_BUS_ADDRESS` - if unset, gvfs will refuse to load to prevent |
| 45 |
spawning private bus |
| 46 |
* `GVFS_DEBUG` - if set (no matter the value), daemons will be printing more |
| 47 |
information on console. This is useful for debugging and reveals parts of |
| 48 |
internal protocol. You can also toggle the debugging for each backend using |
| 49 |
`SIGUSR2`. |
| 50 |
* `GVFS_DEBUG_FUSE` - if set (no matter the value), fuse daemon will be printing |
| 51 |
more information on console. |
| 52 |
* `GVFS_MOUNTABLE_EXTENSION` - filename extension of gvfs backend setup files. |
| 53 |
Default value is ".mount" |
| 54 |
* `GVFS_MOUNTABLE_DIR` - a path to look for the backend setup files. Default |
| 55 |
value is determined by configure script |
| 56 |
* `GVFS_MONITOR_DIR` - a path to look for volume monitors setup files |
| 57 |
(`.monitor` suffix). Default value is determined by configure script |
| 58 |
* `GVFS_REMOTE_VOLUME_MONITOR_IGNORE` - if set (no matter the value), gvfs |
| 59 |
volume monitors will not be activated |
| 60 |
* `GVFS_DISABLE_FUSE` - if set (no matter the value), FUSE daemon will not be |
| 61 |
autostarted |
| 62 |
* `GVFS_AFC_DEBUG` - if set (no matter the value), the AFC backend will print |
| 63 |
more debugging messages |
| 64 |
* `GVFS_GPHOTO2_DEBUG` - controls amount of debug information printed out by the |
| 65 |
gphoto2 backend. Allowed values are "all", "data", "debug", "verbose". |
| 66 |
* `GVFS_HTTP_DEBUG` - controls amount of debug information printed out by the |
| 67 |
http backend. Allowed values are "all", "body", "header". |
| 68 |
* `GVFS_SMB_DEBUG` - sets libsmbclient debug level. Allowed values are integers |
| 69 |
from 0 to 10. |
| 70 |
* `GVFS_MTP_DEBUG` - sets libmtp debug level. Allowed values are "all", "data", |
| 71 |
"usb", "ptp". |
| 72 |
* `GVFS_NFS_DEBUG` - sets libnfs verbosity. Allowed values are integers from 0 |
| 73 |
to ?. |
| 74 |
* `GVFS_WSDD_DEBUG` - if set (no matter the value), the `gvfsd-wsdd` daemon will |
| 75 |
print more debug messages. If set to "all", the underlying wsdd daemon will be |
| 76 |
spawned with "-vvv". |
| 77 |
|
| 78 |
# Components, the architecture |
| 79 |
|
| 80 |
The project consists of three more or less independent parts. Those parts |
| 81 |
comprise of one or more daemons. Everything is glued together using D-Bus, it's |
| 82 |
best to illustrate implementation details on the |
| 83 |
[gvfs D-Bus interface](https://gitlab.gnome.org/GNOME/gvfs/-/raw/master/common/org.gtk.vfs.xml). |
| 84 |
|
| 85 |
Since GVfs is implemented as a GIO extension point, its libraries are loaded in |
| 86 |
every process. This may be expensive and GVfs may need some time to initialize, |
| 87 |
etc. That's one of the reasons a client-server architecture was chosen; client |
| 88 |
libraries (loaded in applications, GIO clients) are basically only proxy modules |
| 89 |
communicating with the gvfs server side through D-Bus. |
| 90 |
|
| 91 |
## Volume monitors |
| 92 |
|
| 93 |
Volume monitors provide a set of `GDrive`/`GVolume`/`GMount` objects |
| 94 |
representing physical device or service hierarchy. So-called native volume |
| 95 |
monitors provide access to locally available devices, i.e. those appearing in |
| 96 |
`/dev` and mountable by standard POSIX ways (`mount`, `umount`). Currently, |
| 97 |
GVfs provides only one native monitor (`udisks2`). |
| 98 |
|
| 99 |
Every volume monitor possesses its own process, communicating with the client |
| 100 |
side through `GProxyVolumeMonitor` infrastructure over D-Bus (see the note about |
| 101 |
client-server architecture above). See the `/monitor/proxy/dbus-interfaces.xml` |
| 102 |
file for D-Bus interface description. Other than usual volume monitor-related |
| 103 |
signals and methods, the interface also wraps `GMountOperation` implemented by a |
| 104 |
combination of method calls and signals for bi-directional communication. |
| 105 |
Signals have been chosen to work around possible deadlocks. |
| 106 |
|
| 107 |
Every volume monitor ships a `.monitor` file containing key-value information |
| 108 |
and that is used for registration. Registration is done on startup (more |
| 109 |
specifically on `libgioremote-volume-monitor.so` load) by going through the |
| 110 |
`.monitor` files in `/usr/share/gvfs/remote-volume-monitors` directory. The |
| 111 |
`.monitor` file provides a D-Bus service name used for daemon autostart; a D-Bus |
| 112 |
service file should be installed along this. |
| 113 |
|
| 114 |
Other than native volume monitors, GVfs ships several others that are related to |
| 115 |
some kind of service or device; in both cases, access is provided usually only |
| 116 |
through GVfs backends. The goal is to present `GVolume` objects mountable by |
| 117 |
users on demand. This applies e.g. to digital cameras or media players connected |
| 118 |
to the computer. |
| 119 |
|
| 120 |
On the client side, `GVolumeMonitor` will mix results from all volume monitors |
| 121 |
together, hiding implementation details. Check the output of `gvfs-mount -l` for |
| 122 |
illustration. |
| 123 |
|
| 124 |
### udisks2 volume monitor |
| 125 |
|
| 126 |
The `udisks2` volume monitor is the preferred native volume monitor for the |
| 127 |
moment. It's the most up-to-date and fully featured among others, and the |
| 128 |
majority of fixes go there. |
| 129 |
|
| 130 |
David Zeuthen wrote a nice document describing what is exposed to the UI and how |
| 131 |
to control it: |
| 132 |
[/monitor/udisks2/what-is-shown.txt](https://gitlab.gnome.org/GNOME/gvfs/raw/master/monitor/udisks2/what-is-shown.txt) |
| 133 |
|
| 134 |
## Core VFS Daemon |
| 135 |
|
| 136 |
The master process, `gvfsd`, is basically a manager and message router. |
| 137 |
Initialized on the client side again as an extension point by loading the |
| 138 |
`libgvfsdbus.so` library, the master daemon is autospawned on first use. The |
| 139 |
client library (running in a GIO application) provides a number of interfaces |
| 140 |
that are proxied to the master daemon or its backends. |
| 141 |
|
| 142 |
All the hard I/O work is done by backends - separate processes for one or more |
| 143 |
mounts, and the master gvfs daemon is just controlling spawning, mounting, |
| 144 |
unmounting, and informs clients which backend to use for the requested file. |
| 145 |
Having backends out of process brings robustness (crashed backend is treated as |
| 146 |
unmounted) and solves some technical difficulties such as threading, locking, |
| 147 |
forking, usually specific to an underlying library. Separate processes can also |
| 148 |
link to non-GPL compatible code without violating licenses of the other parts. |
| 149 |
|
| 150 |
### Mount tracker, GDaemonVolumeMonitor |
| 151 |
|
| 152 |
Among other things, the master daemon is doing on startup, it is registering the |
| 153 |
`org.gtk.vfs.MountTracker` interface. It keeps track of active mounts, handles |
| 154 |
mounting, unmounting, and provides runtime mount information. On the client |
| 155 |
side, `GDaemonVolumeMonitor` for gvfs mounts is available by registering itself |
| 156 |
at the gvfs extension point, tracking changes on the mount tracker. This is also |
| 157 |
used by the FUSE daemon to detect new mounts. |
| 158 |
|
| 159 |
### Spawning and unmounting backends |
| 160 |
|
| 161 |
Mounting is typically initiated by the `g_file_mount_enclosing_volume()` call, |
| 162 |
which goes through `GDaemonFile`, calling the |
| 163 |
`org.gtk.vfs.MountTracker.MountLocation()` D-Bus method. The mount tracker |
| 164 |
(master gvfs daemon) then checks whether the location is not mounted and either |
| 165 |
spawns a new process for the particular mount, or, in case of "Mountable", |
| 166 |
it uses D-Bus service autostart feature for the associated D-Bus name. |
| 167 |
|
| 168 |
The difference is that Mountable backends are able to handle any request for |
| 169 |
specified schemes, sharing one process for any target hostname, contrary to |
| 170 |
standard backends having separate processes for each hostname. This is indicated |
| 171 |
by `.mount` files (the `/usr/share/gvfs/mounts/` path by default or |
| 172 |
`$GVFS_MOUNTABLE_DIR` and `$GVFS_MOUNTABLE_EXTENSION` for override) carrying |
| 173 |
backend-specific information. The principle of setup files is nearly identical |
| 174 |
to `.monitor` files described in the Volume monitors section. |
| 175 |
|
| 176 |
Backends are typically spawned by the master daemon but can also be started |
| 177 |
manually, e.g. when debugging. In the case of being spawned by the master |
| 178 |
daemon, a `--spawner` command line argument with a value of the master daemon |
| 179 |
D-Bus connection name and D-Bus path are appended. These are then used by the |
| 180 |
backend to announce itself (using the `org.gtk.vfs.Spawner` D-Bus interface), |
| 181 |
awaiting more information. The master daemon calls |
| 182 |
`org.gtk.vfs.Mountable.Mount()` method back to the backend with more arguments |
| 183 |
and a mount spec. |
| 184 |
|
| 185 |
The mount spec is a crucial piece of information required for the mount |
| 186 |
operation to succeed. When started manually, the mount spec must be provided as |
| 187 |
command line arguments (in the form of `key1=value key2=value ...`) since the |
| 188 |
master daemon is not aware of this initiative and can't provide any information. |
| 189 |
Once the mount spec is provided, the backend can start the mount process, |
| 190 |
creating `GVfsJobMount`, going through the backend methods. Once that job is |
| 191 |
finished, the result is sent back to the master daemon by the |
| 192 |
`org.gtk.vfs.MountTracker.RegisterMount()` call and only then the mount is |
| 193 |
treated as mounted. A *Mounted* signal is emitted by the mount tracker and |
| 194 |
caught by subscribed `GDaemonVolumeMonitor` instances on client sides. |
| 195 |
|
| 196 |
Unmounting is easier; the mount tracker watches the D-Bus name of all registered |
| 197 |
backends, and when it disappears, i.e. exits or crashes, the mount tracker emits |
| 198 |
an *Unmounted* signal. Historically, this used to be the only way of unmount |
| 199 |
signaling, letting the backend exit gracefully first. However, certain issues |
| 200 |
with unexpected forking in backends were discovered (triggered by an external |
| 201 |
library used), preventing the detection from working properly, so |
| 202 |
`org.gtk.vfs.MountTracker.UnregisterMount()` D-Bus method has been put in use. |
| 203 |
It's also the preferred way of unmounting, called at the `GVfsJobUnmount` |
| 204 |
operation finish. |
| 205 |
|
| 206 |
### Automounting |
| 207 |
|
| 208 |
Some backends don't need credentials or specific mount options; this is the case |
| 209 |
of general mounts like `computer://`, `network://`, `trash://`, etc. For this, |
| 210 |
the automounting feature has been introduced; backends need to indicate that in |
| 211 |
their `.mount` setup files. Technically, such backends are not running by |
| 212 |
default and are only spawned on first use. Automounted mounts need to be of type |
| 213 |
*Mountable* as they can be mounted only once. Automounted mounts are usually |
| 214 |
hidden from `GDaemonVolumeMonitor`, and neither the mount tracker is propagating |
| 215 |
their presence. |
| 216 |
|
| 217 |
### GFile methods |
| 218 |
|
| 219 |
Most of `GFile` methods are implemented in `GDaemonFile` class, on the client |
| 220 |
side. All methods have a common start, doing some validation (mounted location |
| 221 |
check), creating a private connection to the backend, and returning `GDBusProxy` |
| 222 |
instance for immediate use. This proxy is then used for calling respective |
| 223 |
`org.gtk.vfs.Mount` methods that roughly correspond to `GFile` methods. The |
| 224 |
first argument of every call represents the path inside the mount, i.e. stripped |
| 225 |
from path elements that should be part of the mount info - mount prefix (think |
| 226 |
of `smb://server/share/path`). |
| 227 |
|
| 228 |
On the daemon side, incoming `org.gtk.vfs.Mount` method calls are directly |
| 229 |
transformed to new jobs. The job, descendant of the `GVfsJobDBus` class, takes |
| 230 |
reference to the incoming D-Bus message (`GDBusMethodInvocation`) and keeps it |
| 231 |
until the job is finished. Once finished, the result from the job is sent as a |
| 232 |
method call reply, together with any (optional) data. If the job has failed, an |
| 233 |
error is returned and is properly handled by the client side, propagated back to |
| 234 |
the `GDaemonFile` method caller, usually displayed in UI. For this reason, it's |
| 235 |
crucial to set method call timeouts in a sane way, i.e. for long I/O operations |
| 236 |
the timeout should be set to infinite. |
| 237 |
|
| 238 |
### Jobs |
| 239 |
|
| 240 |
Jobs are essentially wrappers around backend methods, carrying all arguments |
| 241 |
from the D-Bus method call. Backend methods are those doing actual I/O; for that |
| 242 |
reason, we need to have some kind of control over them. Two types of methods |
| 243 |
exist in the backend. Methods prefixed with `try_` are supposed to be |
| 244 |
asynchronous and non-blocking as much as possible. The return value of boolean |
| 245 |
indicates whether the request has been handled or not. If not (or the `try_` |
| 246 |
method is not implemented), the `do_` prefixed method is called (or non-prefixed |
| 247 |
version respectively). This call is more heavyweight and runs in a thread pool. |
| 248 |
That way, the backend could handle multiple requests simultaneously. Daemon |
| 249 |
thread pool is on the daemon (process, see below) basis and the maximum number |
| 250 |
of threads is controlled by the `MAX_JOB_THREADS` define during compilation |
| 251 |
(see `meson.build`). |
| 252 |
|
| 253 |
When talking about backends, let's see how objects are organized. A daemon |
| 254 |
instance represents the process itself. Daemon can handle one or more mounts |
| 255 |
(this is actually determined by the master gvfs daemon, the backend daemon |
| 256 |
infrastructure is generally always able to handle multiple mounts) and maintains |
| 257 |
a list of job sources. The default job source is created for every single mount |
| 258 |
(`GVfsBackend` instance); additional job sources are created for each opened |
| 259 |
file (both for reading and writing, `GVfsChannel`). Job source (`GVfsJobSource`) |
| 260 |
is an interface implemented by `GVfsBackend` and `GVfsChannel`. |
| 261 |
|
| 262 |
Every job source maintains a list of actual jobs. Jobs are submitted via the |
| 263 |
`g_vfs_daemon_queue_job()` call. Typically, every new job created by the |
| 264 |
`org.gtk.vfs.Mount` method call handler queues itself once all arguments are |
| 265 |
collected. More jobs are queued during file transfer, from the `GVfsChannel` |
| 266 |
side. This will then go through the `try_` and `do_` backend methods which |
| 267 |
effectively start the operation. |
| 268 |
|
| 269 |
Backends are required to return the status of the operation by calling either |
| 270 |
`g_vfs_job_succeed()` or `g_vfs_job_failed()` in case of error. That will |
| 271 |
indicate the `GVfsJob` instance to finish. This is also the way the asynchronous |
| 272 |
`try_` methods should complete. Some job classes may provide more methods that |
| 273 |
are required to be called in order to pass some data to the job (e.g. file infos |
| 274 |
for `GVfsJobEnumerate`). |
| 275 |
|
| 276 |
If anything happens to the master gvfs daemon (crashes or is being replaced by |
| 277 |
another instance) and its D-Bus name owner changes, every running backend daemon |
| 278 |
will go through the list of active job sources and re-register all mounted |
| 279 |
backends with the new master daemon. This is a nice way of crash recovery, |
| 280 |
bringing more robustness. |
| 281 |
|
| 282 |
### Cancellation |
| 283 |
|
| 284 |
Cancellation is well integrated by GIO nature and GVfs needs to provide a |
| 285 |
channel to cancel running operations. This is done by calling the |
| 286 |
`org.gtk.vfs.Daemon.Cancel()` method taking the serial number as the only |
| 287 |
argument. This is available on both session bus and private connections. |
| 288 |
|
| 289 |
The job system in backends works by taking reference to the originating D-Bus |
| 290 |
message for later reply. Every D-Bus message contains a unique serial number |
| 291 |
and by going through the list of running operations we are able to find the |
| 292 |
right one to cancel. |
| 293 |
|
| 294 |
### Private peer-to-peer connection |
| 295 |
|
| 296 |
For particular `GDaemonFile` operations, a private peer-to-peer D-Bus connection |
| 297 |
is created between the client library and the backend directly. This allows us |
| 298 |
to overcome potential weak points of overloaded session bus daemon, relieve from |
| 299 |
data marshalling, and be somewhat independent of the session bus. This is quite |
| 300 |
independent implementation and can be easily switched back to the use of a |
| 301 |
session bus. Similarly, we provide private sockets for raw data transfers, to |
| 302 |
maximize throughput. |
| 303 |
|
| 304 |
Initiated from most `GDaemonFile` methods, the way of setting up a private |
| 305 |
connection is rather complex. On the client side, we first need to know the |
| 306 |
backend D-Bus ID. By calling the backend `org.gtk.vfs.Daemon.GetConnection()` |
| 307 |
method, we get a D-Bus address. Using this address, we create a new D-Bus |
| 308 |
connection that is used for further `org.gtk.vfs.Mount` calls. Every client |
| 309 |
maintains a cache of thread-local private connections and tries to reuse them |
| 310 |
whenever possible. |
| 311 |
|
| 312 |
On the backend side, on `GetConnection()` request, a socket with a unique name |
| 313 |
is created and GDBus server is started on it. The server then awaits incoming |
| 314 |
client and registers several interfaces on the connection once the client comes. |
| 315 |
The server is then killed as no more clients are expected, leaving the active |
| 316 |
connection open. When the connection is closed, the backend cancels all active |
| 317 |
jobs. |
| 318 |
|
| 319 |
### Skeletons, registered paths |
| 320 |
|
| 321 |
Related to private connections, we need to make available several interfaces on |
| 322 |
them. Every time a new private connection is opened, several |
| 323 |
`GDBusInterfaceSkeleton` objects are registered on it. |
| 324 |
|
| 325 |
Historically, GDBus was not able to export a single `GDBusInterfaceSkeleton` |
| 326 |
over multiple connections, and a workaround solution was created. By calling |
| 327 |
`g_vfs_daemon_register_path()`, interested parties register themselves handing |
| 328 |
over an object path and a callback that is used for creating new skeletons for |
| 329 |
the new connection. This way we export `org.gtk.vfs.Mount` and |
| 330 |
`org.gtk.vfs.Monitor` interfaces on the private connection. |
| 331 |
|
| 332 |
Similar approach is taken on the client side for `GDaemonFile` methods; |
| 333 |
interested parties register their ability to provide certain services by calling |
| 334 |
`_g_dbus_register_vfs_filter()` and their callback is then used to create |
| 335 |
interface skeletons on every new private connection (by calling |
| 336 |
`_g_dbus_connect_vfs_filters()`). This way we export `org.gtk.vfs.Enumerator` |
| 337 |
and `org.gtk.vfs.MonitorClient` interfaces, typically used as a form of callback |
| 338 |
on events from daemon to clients. |
| 339 |
|
| 340 |
### Enumerator |
| 341 |
|
| 342 |
Enumeration (initiated by `g_file_enumerate_children()`), just like several |
| 343 |
other `GFile` methods, needs to be stateful to be able to continuously provide |
| 344 |
data back to the client. For this, GVfs provides `GDaemonFileEnumerator` that is |
| 345 |
used on the client side. A new VFS filter (see above) is registered to provide |
| 346 |
the `org.gtk.vfs.Enumerator` interface on the connection. That's wrapped by |
| 347 |
`GDaemonFileEnumerator` and is used to receive data from the backend. Every |
| 348 |
`GDaemonFileEnumerator` instance creates a unique ID that is used as an object |
| 349 |
path for creating interface skeleton on the client side and for `GDBusProxy` |
| 350 |
construction on the backend side. It's worth noting that `GDaemonFileEnumerator` |
| 351 |
can handle both sync and async ways of enumeration. |
| 352 |
|
| 353 |
`GVfsJobEnumerate` provides several methods used by backends to send data back |
| 354 |
to the client. Collected infos are sent using the |
| 355 |
`org.gtk.vfs.Enumerator.GotInfo()` call as an array, and when enumeration is |
| 356 |
finished, `org.gtk.vfs.Enumerator.Done()` is called to indicate that to the |
| 357 |
client side. Additional backend info is added in `GVfsJobEnumerate`, and |
| 358 |
metadata info is added on the client side if applicable. |
| 359 |
|
| 360 |
Note that any unsuccessful method call will lead to a warning message printed to |
| 361 |
the console; sometimes messages can be seen when the client closes the |
| 362 |
enumerator before it's fully finished. |
| 363 |
|
| 364 |
### Monitoring |
| 365 |
|
| 366 |
Structure-wise, monitoring is very similar to enumeration. Client |
| 367 |
`g_file_monitor*()` calls will result in `GVfsJobCreateMonitor` on the backend |
| 368 |
side, which lets the backend create a `GVfsMonitor` instance and hand it over to |
| 369 |
the job. This instance wraps around the `org.gtk.vfs.Monitor` interface, waiting |
| 370 |
for clients to subscribe and unsubscribe particular files and directories |
| 371 |
they're interested in monitoring. This is done on the client side; when the |
| 372 |
`GVfsJobCreateMonitor` call is finished, the client creates a |
| 373 |
`GDaemonFileMonitor` instance and subscribes itself to the backend-side monitor. |
| 374 |
Every subscription takes a reference to the `GVfsMonitor` instance, every |
| 375 |
unsubscribe releases it. That way, the monitor is automatically destroyed when |
| 376 |
no one is interested in monitoring anymore. Subscriptions on a particular |
| 377 |
private D-Bus connection are automatically unsubscribed when the connection is |
| 378 |
closed. |
| 379 |
|
| 380 |
When the backend emits a change event, `GVfsMonitor` calls the |
| 381 |
`org.gtk.vfs.MonitorClient.Changed()` method, and the client side |
| 382 |
`GDaemonFileMonitor` emits an event to the original client. |
| 383 |
|
| 384 |
### GMountOperation proxy |
| 385 |
|
| 386 |
For `GDaemonFile` methods that are taking a `GMountOperation` argument, we need |
| 387 |
to provide a way to proxy that to the daemon side. This is done by the |
| 388 |
`g_mount_operation_dbus_wrap()` call which wraps the foreign instance on the |
| 389 |
specified D-Bus connection. Internally, that means creating an |
| 390 |
`org.gtk.vfs.MountOperation` interface skeleton on the client side and assigning |
| 391 |
its D-Bus specific data to a newly created `GMountSource` instance. These are |
| 392 |
then used by several `org.gtk.vfs.Mount` methods to reconstruct a `GMountSource` |
| 393 |
instance on the daemon side and finally get a `GMountOperation` instance through |
| 394 |
`g_mount_source_get_operation()`. This call creates a new local |
| 395 |
`GMountOperation` instance catching its signals. When asked for something, the |
| 396 |
machinery makes a call back to the client and sets the `GMountOperation` |
| 397 |
credentials if succeeded. |
| 398 |
|
| 399 |
### File copy |
| 400 |
|
| 401 |
File copy or move in the form of copy-and-delete is supported in GVfs; however, |
| 402 |
the open-read-write-close fallback is done automatically by GIO when |
| 403 |
`G_IO_ERROR_NOT_SUPPORTED` is returned. To overcome potential backend |
| 404 |
limitations and to optimize the data flow, two new backend operations have been |
| 405 |
introduced along standard `copy()` and `move()`: `push()` and `pull()`. These |
| 406 |
methods work with one endpoint being a local file; `pull()` having the source |
| 407 |
file local, `push()` transfers data from backend to a local file. The standard |
| 408 |
`copy()` and `move()` are supposed to work within the same mount only. |
| 409 |
|
| 410 |
Some backends are not able to provide universal open, read, write, close methods |
| 411 |
either due to the service nature or due to a limitation of the underlying |
| 412 |
library. The `push()`/`pull()` methods take care of file transfer completely, |
| 413 |
i.e. file open, data transfer, and file close, with optional progress callback |
| 414 |
reporting. These methods are only used if implemented by the backend and if one |
| 415 |
of the endpoints is a local file. |
| 416 |
|
| 417 |
In order to provide progress reports during the transfer, the |
| 418 |
`org.gtk.vfs.Progress` D-Bus interface is set up on the client side of the |
| 419 |
connection, awaiting incoming `Progress()` method calls that will then call the |
| 420 |
progress callback specified in the original `GDaemonFile` call. |
| 421 |
|
| 422 |
### Reading and writing data |
| 423 |
|
| 424 |
Since the open-read-write-close operations are used as a copy fallback by GIO, |
| 425 |
it's recommended to have these methods implemented to maintain a degree of |
| 426 |
universality. This is impossible for some backends though (e.g. `mtp`) where the |
| 427 |
full file has to be always transferred. |
| 428 |
|
| 429 |
Unlike file copy, which handles data blocks internally, the |
| 430 |
`GDaemonFile.read()`, `GDaemonFile.create()`, and similar methods are returning |
| 431 |
`GFileInputStream` and `GFileOutputStream` respectively, meaning the data are |
| 432 |
being transferred outside of GVfs. In order to efficiently transfer data blocks |
| 433 |
between two processes, a Unix socket is created, and using the GDBus fd-passing |
| 434 |
feature, the client receives an fd for reading/writing. `GFileInputStream` and |
| 435 |
`GFileOutputStream` are implemented by `GDaemonFileInputStream` and |
| 436 |
`GDaemonFileOutputStream` respectively, providing all necessary infrastructure |
| 437 |
for data handling. This allows us to overcome unnecessary round-trips through |
| 438 |
the D-Bus daemon, achieving almost native speed. |
| 439 |
|
| 440 |
The daemon open job creates a `GVfsReadChannel` or `GVfsWriteChannel` |
| 441 |
respectively, taking a pointer returned by the backend as an internal file |
| 442 |
handle. The `GVfsChannel` base class takes care of creating a Unix socket for |
| 443 |
data transfer, buffering, simple protocol, and queue handling, etc. Its |
| 444 |
descendants implement more functionality like readahead and are processing the |
| 445 |
channel protocol, calling particular jobs (`GVfsJobRead`, `GVfsJobWrite`, |
| 446 |
`GVfsJobSeekRead`, `GVfsJobSeekWrite`, `GVfsJobQueryInfoRead`, |
| 447 |
`GVfsJobQueryInfoWrite`, `GVfsJobCloseRead`, `GVfsJobCloseWrite`) to do the |
| 448 |
actual work. |
| 449 |
|
| 450 |
### Mount spec |
| 451 |
|
| 452 |
Mount spec is a tiny class that helps to identify and match mounts. It carries |
| 453 |
key-value pairs, e.g. information about the URI scheme used, backend type, |
| 454 |
hostname, port, username, domain, etc. |
| 455 |
|
| 456 |
Specifically, during the mount operation, two instances of mount spec are used. |
| 457 |
The source one carries requested information and is handed over to the backend's |
| 458 |
`GVfsJobMount` operation. It is the backend's responsibility to process this |
| 459 |
mount spec, create a new one based on the source one, and send it back to the |
| 460 |
mount tracker for registration. The backend can filter out some information that |
| 461 |
are not strictly required and only sets those values required for unique mount |
| 462 |
identification. |
| 463 |
|
| 464 |
Mount spec matching is done by comparing all its values; it must be completely |
| 465 |
equal. In practice, this also means that e.g. `scheme://hostname/` and |
| 466 |
`scheme://username@hostname/` may result in two mounts even if the user enters |
| 467 |
the same username in a credential prompt. There's another limitation in real |
| 468 |
applications at the moment; requesting mount on `scheme://hostname/` wouldn't |
| 469 |
match with `scheme://username@hostname/` at the beginning when the mount tracker |
| 470 |
is trying to find existing mount since we don't know what username to use at |
| 471 |
this point. Consequently, when going through the mount process and the username |
| 472 |
is known, we don't have a tool for canceling the current mount operation and |
| 473 |
redirecting ourselves to an active mount. |
| 474 |
|
| 475 |
### URI mappers |
| 476 |
|
| 477 |
URI mappers are tiny classes helping to parse URI strings with respect to |
| 478 |
special parts, like a domain in `smb://` or to distinguish between `smb-share` |
| 479 |
and `smb-server` backend types, etc. Used mostly when converting URI to a mount |
| 480 |
spec and back. |
| 481 |
|
| 482 |
Default parser is used when a specific URI mapper is not available for the given |
| 483 |
scheme. |
| 484 |
|
| 485 |
### Keyring integration |
| 486 |
|
| 487 |
Secret storage integration is actually very basic; two helper functions are |
| 488 |
available for direct backend use, for storing and retrieving credentials with |
| 489 |
additional arguments. Also, please take the mount spec specifics into account, |
| 490 |
having two nearly identical records with the difference of having the username |
| 491 |
set is perfectly okay. |
| 492 |
|
| 493 |
This feature also helps debugging significantly, allowing developers to overcome |
| 494 |
credential prompts when running the backend manually since it's able to pick |
| 495 |
credentials up from the keyring. Just be sure to pass correct mount spec |
| 496 |
arguments so that matching is successful. |
| 497 |
|
| 498 |
## FUSE daemon |
| 499 |
|
| 500 |
The package provides a convenient FUSE mount for easy POSIX access to active |
| 501 |
GIO mounts. This is handled by the `gvfsd-fuse` process, a daemon that is |
| 502 |
autostarted when `gvfsd` is spawned. The default mount point is |
| 503 |
`/run/user/<UID>/gvfs`, usually located on tmpfs, with a fallback to the old |
| 504 |
location `~/.gvfs` when not available. On some systems, the user needs to be in |
| 505 |
the `fuse` group to be actually able to mount FUSE filesystems. |
| 506 |
|
| 507 |
Even if the FUSE daemon is not autostarted (by running `gvfsd --no-fuse`), it |
| 508 |
can be started manually anytime. The daemon registers itself with the master |
| 509 |
gvfs daemon by calling `org.gtk.vfs.MountTracker.RegisterFuse()` D-Bus method. |
| 510 |
When the master gvfs daemon disappears from the session bus, the FUSE daemon |
| 511 |
is terminated. |
| 512 |
|
| 513 |
The daemon creates a flat structure of active GIO mounts at the topmost level, |
| 514 |
named by internal `GMountSpec` string representation (in the form of |
| 515 |
`protocol:arg1=val,arg2=val`). This string is not guaranteed and may change in |
| 516 |
the future. |
| 517 |
|
| 518 |
Please note that due to the different nature of POSIX and GIO filesystems, not |
| 519 |
everything is possible in the POSIX world and there will always be trade-offs. |
| 520 |
Moreover, not all backends implement every `GVfsBackend` method, e.g. a backend |
| 521 |
implements file open/read/write/close but not seeking. Some POSIX applications |
| 522 |
may not function properly due to that; the translation is not 1:1. Things like |
| 523 |
returning zero filesystem size and free space just because the actual backend |
| 524 |
has no native support may make some apps confused, refusing to write any data. |
| 525 |
|
| 526 |
There are several security concerns; the first and most visible is denied access |
| 527 |
to other UIDs including UID 0. This causes troubles to system commands and |
| 528 |
daemons stat-ing the filesystem (i.e. `df` or `systemd-tmpfiles`) with an error |
| 529 |
printed out. See [bug 560658](https://bugzilla.gnome.org/show_bug.cgi?id=560658) |
| 530 |
for details and discussion. |
| 531 |
|
| 532 |
The other security concern is revealing symlinks from backends. Unmodified, they |
| 533 |
may point to the host system in case of absolute target paths and may not be |
| 534 |
constrained in the mount point in any way. For now, the FUSE daemon dereferences |
| 535 |
all symlinks and presents them as regular files or directories. Only broken |
| 536 |
symlinks are still presented as symlinks. See |
| 537 |
[bug 696298](https://bugzilla.gnome.org/show_bug.cgi?id=696298) for discussion. |
| 538 |
|
| 539 |
### Local path mapping |
| 540 |
|
| 541 |
The `g_file_get_path()` method, as implemented in `GDaemonFile`, can provide a |
| 542 |
local pathname when the FUSE mount point is registered and connected with the |
| 543 |
master gvfs daemon. This path can then be easily used for spawning applications |
| 544 |
with a local path. When an application is asked to create a new `GFile` instance |
| 545 |
using the local FUSE path, it is automatically converted to a native URI. That |
| 546 |
way, we don't need to pass URI strings outside of the GIO world. |
| 547 |
|
| 548 |
## Metadata |
| 549 |
|
| 550 |
GVfs provides its own persistent storage for simple key-value pairs associated |
| 551 |
with a particular file. Typically used for runtime data such as icon positions, |
| 552 |
emblems, position within the document, notes, etc. Only non-critical data should |
| 553 |
be stored. Usage-wise, it's a separate file attribute namespace for easy use |
| 554 |
with `GFileInfo`. |
| 555 |
|
| 556 |
All data are stored in so-called metadata database, a set of binary files in |
| 557 |
`~/.local/share/gvfs-metadata`. Every database file carries a journal where the |
| 558 |
most recent changes are stored for a short moment of time before they're written |
| 559 |
to the master database file. Every file roughly corresponds to a `GMount`, |
| 560 |
identified either by mount spec (for GVfs mounts) or by UUID or device name for |
| 561 |
physical mounts. That brings some flexibility; when a mount is mounted in a |
| 562 |
different path (automount; usually physical devices), the metadata subsystem is |
| 563 |
still able to identify the device and match the existing database. A fallback to |
| 564 |
database "root" is in place, taking everything starting with "/". |
| 565 |
|
| 566 |
Retrieving data is done within `GDaemonFile` (and `GDaemonFileEnumerator`) |
| 567 |
methods, directly accessing database files. Storing data is going through a |
| 568 |
separate daemon, `gvfsd-metadata`, as a single point queueing all requests and |
| 569 |
not blocking applications. Incoming data are first written in a journal file and |
| 570 |
then, after a certain time (60 seconds) or in case of a full journal, they're |
| 571 |
written to the master database file (metatree file). The journal contains all |
| 572 |
incoming requests chronologically stacked up while the metatree file contains |
| 573 |
clean key-value pairs with no duplicates. |
| 574 |
|
| 575 |
The process of writeout is called rotation. The daemon first reads the existing |
| 576 |
metatree file and iterates over the journal, going from oldest entries to newest |
| 577 |
ones, applying the changes on top of existing values. Data are written in a new |
| 578 |
metatree file and this file then atomically replaces the old database. Keeping |
| 579 |
the old database file still open, the "rotated" bit is written in and the file |
| 580 |
is closed. Since it was atomically replaced by the new database, it is unlinked |
| 581 |
and data are destroyed by the operating system in an ideal case. There still may |
| 582 |
be clients having the very same file open; however, in the process of data |
| 583 |
retrieval, the rotated bit then indicates that the currently open database is |
| 584 |
old and reopen is necessary. |
| 585 |
|
| 586 |
Special care is taken when the metadata directory resides on NFS (e.g. having |
| 587 |
the home directory on NFS). See `safe_open()` in `metadata/metatree.c` for |
| 588 |
details; the code is trying to work around possible stale file handles by |
| 589 |
linking to a temp file, opening, and unlinking again so that we actually don't |
| 590 |
operate on the file directly, just modifying the same data. |
| 591 |
|
| 592 |
This all works natively for files on GVfs mounts (`GDaemonFile`); for local |
| 593 |
`file://` scheme, the GIO API has been extended to allow `GLocalFile` to go |
| 594 |
through the default VFS implementation (`g_vfs_get_default()`, `GVfs` class) and |
| 595 |
ask for metadata associated with a local file specifically. |
| 596 |
|
| 597 |
Metadata are tied to the particular file or directory. If it's moved or deleted |
| 598 |
(using GIO), changes are reflected in the metadata database the same way. It |
| 599 |
works very similarly to xattrs. |
| 600 |
|
| 601 |
There were some thoughts about feeding metadata into a Tracker store for easy |
| 602 |
indexing; not sure if there are any drivers for that nowadays. |
| 603 |
|
| 604 |
## Tools |
| 605 |
|
| 606 |
GLib distributes `gio` cmd tool providing convenient access to GIO resources. |
| 607 |
Imitating well-known POSIX commands, taking URI instead of filenames. This is |
| 608 |
useful for testing. |
| 609 |
|
| 610 |
## Tests |
| 611 |
|
| 612 |
An integrated test suite is available and covers quite a large area. Testing a |
| 613 |
project like GVfs is difficult as you need to provide either services for the |
| 614 |
client (gvfs backends) or even simulate physical devices (for volume monitors). |
| 615 |
This is more or less done by using available projects (apache, samba, twisted) |
| 616 |
or system infrastructure (`scsi-debug` kernel module, systemd). Special care |
| 617 |
needs to be held for distro support, taking into account various versions of |
| 618 |
required dependencies and different (config files) locations. |
| 619 |
|
| 620 |
The test suite is able to run in several modes; the most useful is the in-tree |
| 621 |
sandboxed mode, which starts its own D-Bus session bus and uses the binaries |
| 622 |
from the source tree. More advanced tests are available when executed as root. |
| 623 |
|
| 624 |
# What's missing? |
| 625 |
|
| 626 |
* Icon extensions |
| 627 |
|
| 628 |
# FAQ |
| 629 |
|
| 630 |
## Running applications under root |
| 631 |
|
| 632 |
Starting a desktop session under root is generally discouraged and considered a |
| 633 |
security risk. However, since it's a complete session, GVfs will work just fine. |
| 634 |
|
| 635 |
A bigger problem is starting particular applications through `su`, `sudo`, or |
| 636 |
any other graphical equivalent. This also applies to tunneled X connections. |
| 637 |
Please understand that this effectively cuts the original user's D-Bus session |
| 638 |
out; error messages printed on the console will clearly show that as well. |
| 639 |
Tunneling D-Bus connections can be treated as a security risk and is disabled |
| 640 |
AFAIK. You won't be able to access any active mounts; they all need to be opened |
| 641 |
again within the new session. |
| 642 |
|
| 643 |
The right solution is to use `admin://` backend that is based on `PolicyKit`. |
| 644 |
|
| 645 |
## Writing your own backend |
| 646 |
|
| 647 |
GVfs doesn't provide any public headers, and the daemon D-Bus protocol is |
| 648 |
considered private and may change at any time (and that actually happened with |
| 649 |
the GDBus port). Out-of-tree backends are simply not supported; the preferred |
| 650 |
way is to fork/branch gvfs sources and write your backend on top of it. If you |
| 651 |
then send the resulting patch to upstream developers, they'll be happy to review |
| 652 |
it for you for potential inclusion. |
| 653 |
|
| 654 |
The best way to start is grabbing an existing backend and studying its contents. |
| 655 |
The local test backend is basically a proxy to the `file://` scheme originally |
| 656 |
written for testing purposes. Backends are quite independent from the rest of |
| 657 |
the GVfs codebase, and you shouldn't be needing any further changes outside the |
| 658 |
backend. |