2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import Gio from 'gi://Gio';
|
|
|
|
import GLib from 'gi://GLib';
|
|
|
|
import * as Params from '../../misc/params.js';
|
2012-08-17 12:18:53 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import * as GnomeSession from '../../misc/gnomeSession.js';
|
|
|
|
import * as Main from '../main.js';
|
|
|
|
import * as ShellMountOperation from '../shellMountOperation.js';
|
2012-06-26 09:26:36 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const GNOME_SESSION_AUTOMOUNT_INHIBIT = 16;
|
2011-06-20 19:16:40 +00:00
|
|
|
|
|
|
|
// GSettings keys
|
|
|
|
const SETTINGS_SCHEMA = 'org.gnome.desktop.media-handling';
|
|
|
|
const SETTING_ENABLE_AUTOMOUNT = 'automount';
|
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const AUTORUN_EXPIRE_TIMEOUT_SECS = 10;
|
2011-06-22 13:45:03 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
class AutomountManager {
|
2017-10-31 01:19:44 +00:00
|
|
|
constructor() {
|
2023-08-06 22:40:20 +00:00
|
|
|
this._settings = new Gio.Settings({schema_id: SETTINGS_SCHEMA});
|
2018-10-05 17:09:28 +00:00
|
|
|
this._activeOperations = new Map();
|
2012-06-26 09:26:36 +00:00
|
|
|
this._session = new GnomeSession.SessionManager();
|
|
|
|
this._session.connectSignal('InhibitorAdded',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._InhibitorsChanged.bind(this));
|
2012-06-26 09:26:36 +00:00
|
|
|
this._session.connectSignal('InhibitorRemoved',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._InhibitorsChanged.bind(this));
|
2012-06-26 09:26:36 +00:00
|
|
|
this._inhibited = false;
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._volumeMonitor = Gio.VolumeMonitor.get();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
enable() {
|
2021-08-15 22:36:59 +00:00
|
|
|
this._volumeMonitor.connectObject(
|
|
|
|
'volume-added', this._onVolumeAdded.bind(this),
|
|
|
|
'volume-removed', this._onVolumeRemoved.bind(this),
|
|
|
|
'drive-connected', this._onDriveConnected.bind(this),
|
|
|
|
'drive-disconnected', this._onDriveDisconnected.bind(this),
|
|
|
|
'drive-eject-button', this._onDriveEjectButton.bind(this), this);
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2019-08-19 18:50:33 +00:00
|
|
|
this._mountAllId = GLib.idle_add(GLib.PRIORITY_DEFAULT, this._startupMountAll.bind(this));
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(this._mountAllId, '[gnome-shell] this._startupMountAll');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-09-03 01:23:50 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
disable() {
|
2021-08-15 22:36:59 +00:00
|
|
|
this._volumeMonitor.disconnectObject(this);
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
if (this._mountAllId > 0) {
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(this._mountAllId);
|
2012-09-03 01:23:50 +00:00
|
|
|
this._mountAllId = 0;
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
async _InhibitorsChanged(_object, _senderName, [_inhibitor]) {
|
|
|
|
try {
|
|
|
|
const [inhibited] =
|
|
|
|
await this._session.IsInhibitedAsync(GNOME_SESSION_AUTOMOUNT_INHIBIT);
|
|
|
|
this._inhibited = inhibited;
|
|
|
|
} catch (e) {}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-06-26 09:26:36 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_startupMountAll() {
|
2011-06-20 19:16:40 +00:00
|
|
|
let volumes = this._volumeMonitor.get_volumes();
|
2017-10-31 00:38:18 +00:00
|
|
|
volumes.forEach(volume => {
|
2020-03-29 21:51:13 +00:00
|
|
|
this._checkAndMountVolume(volume, {
|
|
|
|
checkSession: false,
|
|
|
|
useMountOp: false,
|
|
|
|
allowAutorun: false,
|
|
|
|
});
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2012-09-03 01:23:50 +00:00
|
|
|
this._mountAllId = 0;
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDriveConnected() {
|
2011-06-27 15:51:23 +00:00
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2012-02-11 03:51:42 +00:00
|
|
|
return;
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2018-12-13 19:30:22 +00:00
|
|
|
let player = global.display.get_sound_player();
|
|
|
|
player.play_from_theme('device-added-media',
|
2023-08-06 22:34:20 +00:00
|
|
|
_('External drive connected'),
|
2018-12-13 19:30:22 +00:00
|
|
|
null);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDriveDisconnected() {
|
2011-06-27 15:51:23 +00:00
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// or screensaver is active, don't play sounds
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2012-02-11 03:51:42 +00:00
|
|
|
return;
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2019-03-04 13:36:13 +00:00
|
|
|
let player = global.display.get_sound_player();
|
|
|
|
player.play_from_theme('device-removed-media',
|
2023-08-06 22:34:20 +00:00
|
|
|
_('External drive disconnected'),
|
2019-03-04 13:36:13 +00:00
|
|
|
null);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-27 15:51:23 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onDriveEjectButton(monitor, drive) {
|
2011-06-27 16:13:54 +00:00
|
|
|
// TODO: this code path is not tested, as the GVfs volume monitor
|
|
|
|
// doesn't emit this signal just yet.
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-27 16:13:54 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// we force stop/eject in this case, so we don't have to pass a
|
|
|
|
// mount operation object
|
|
|
|
if (drive.can_stop()) {
|
2019-01-29 19:36:54 +00:00
|
|
|
drive.stop(Gio.MountUnmountFlags.FORCE, null, null,
|
2019-08-20 00:20:08 +00:00
|
|
|
(o, res) => {
|
2019-01-29 19:36:54 +00:00
|
|
|
try {
|
|
|
|
drive.stop_finish(res);
|
|
|
|
} catch (e) {
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Unable to stop the drive after drive-eject-button ${e.toString()}`);
|
2019-01-29 19:36:54 +00:00
|
|
|
}
|
|
|
|
});
|
2011-06-27 16:13:54 +00:00
|
|
|
} else if (drive.can_eject()) {
|
2019-01-29 19:36:54 +00:00
|
|
|
drive.eject_with_operation(Gio.MountUnmountFlags.FORCE, null, null,
|
2019-08-20 00:20:08 +00:00
|
|
|
(o, res) => {
|
2019-01-29 19:36:54 +00:00
|
|
|
try {
|
|
|
|
drive.eject_with_operation_finish(res);
|
|
|
|
} catch (e) {
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Unable to eject the drive after drive-eject-button ${e.toString()}`);
|
2019-01-29 19:36:54 +00:00
|
|
|
}
|
|
|
|
});
|
2011-06-27 16:13:54 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-27 16:13:54 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onVolumeAdded(monitor, volume) {
|
2011-06-20 19:16:40 +00:00
|
|
|
this._checkAndMountVolume(volume);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_checkAndMountVolume(volume, params) {
|
2020-03-29 21:51:13 +00:00
|
|
|
params = Params.parse(params, {
|
|
|
|
checkSession: true,
|
|
|
|
useMountOp: true,
|
|
|
|
allowAutorun: true,
|
|
|
|
});
|
2011-06-20 19:16:40 +00:00
|
|
|
|
|
|
|
if (params.checkSession) {
|
|
|
|
// if we're not in the current ConsoleKit session,
|
|
|
|
// don't attempt automount
|
2013-02-03 21:24:33 +00:00
|
|
|
if (!this._session.SessionIsActive)
|
2011-06-20 19:16:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-26 09:26:36 +00:00
|
|
|
if (this._inhibited)
|
|
|
|
return;
|
|
|
|
|
2011-09-28 20:29:45 +00:00
|
|
|
// Volume is already mounted, don't bother.
|
|
|
|
if (volume.get_mount())
|
|
|
|
return;
|
|
|
|
|
2011-06-22 13:45:03 +00:00
|
|
|
if (!this._settings.get_boolean(SETTING_ENABLE_AUTOMOUNT) ||
|
|
|
|
!volume.should_automount() ||
|
|
|
|
!volume.can_mount()) {
|
2011-09-28 20:29:45 +00:00
|
|
|
// allow the autorun to run anyway; this can happen if the
|
2019-09-12 15:26:08 +00:00
|
|
|
// mount gets added programmatically later, even if
|
2011-06-22 13:45:03 +00:00
|
|
|
// should_automount() or can_mount() are false, like for
|
|
|
|
// blank optical media.
|
|
|
|
this._allowAutorun(volume);
|
|
|
|
this._allowAutorunExpire(volume);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-22 20:43:16 +00:00
|
|
|
if (params.useMountOp) {
|
|
|
|
let operation = new ShellMountOperation.ShellMountOperation(volume);
|
2012-06-20 00:22:26 +00:00
|
|
|
this._mountVolume(volume, operation, params.allowAutorun);
|
2011-06-22 20:43:16 +00:00
|
|
|
} else {
|
2012-06-19 18:49:40 +00:00
|
|
|
this._mountVolume(volume, null, params.allowAutorun);
|
2011-06-22 20:43:16 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_mountVolume(volume, operation, allowAutorun) {
|
2012-06-19 18:49:40 +00:00
|
|
|
if (allowAutorun)
|
|
|
|
this._allowAutorun(volume);
|
|
|
|
|
2020-08-12 18:59:01 +00:00
|
|
|
const mountOp = operation?.mountOp ?? null;
|
2018-10-05 17:09:28 +00:00
|
|
|
this._activeOperations.set(volume, operation);
|
2012-06-20 00:22:26 +00:00
|
|
|
|
|
|
|
volume.mount(0, mountOp, null,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onVolumeMounted.bind(this));
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onVolumeMounted(volume, res) {
|
2011-06-22 13:45:03 +00:00
|
|
|
this._allowAutorunExpire(volume);
|
|
|
|
|
2011-06-20 19:16:40 +00:00
|
|
|
try {
|
|
|
|
volume.mount_finish(res);
|
2012-06-20 00:22:26 +00:00
|
|
|
this._closeOperation(volume);
|
2011-06-20 19:16:40 +00:00
|
|
|
} catch (e) {
|
2012-06-20 00:22:26 +00:00
|
|
|
// FIXME: we will always get G_IO_ERROR_FAILED from the gvfs udisks
|
2018-10-11 17:14:25 +00:00
|
|
|
// backend, see https://bugs.freedesktop.org/show_bug.cgi?id=51271
|
|
|
|
// To reask the password if the user input was empty or wrong, we
|
|
|
|
// will check for corresponding error messages. However, these
|
|
|
|
// error strings are not unique for the cases in the comments below.
|
|
|
|
if (e.message.includes('No key available with this passphrase') || // cryptsetup
|
|
|
|
e.message.includes('No key available to unlock device') || // udisks (no password)
|
2019-04-07 21:35:07 +00:00
|
|
|
// libblockdev wrong password opening LUKS device
|
|
|
|
e.message.includes('Failed to activate device: Incorrect passphrase') ||
|
|
|
|
// cryptsetup returns EINVAL in many cases, including wrong TCRYPT password/parameters
|
|
|
|
e.message.includes('Failed to load device\'s parameters: Invalid argument')) {
|
2011-07-12 15:29:49 +00:00
|
|
|
this._reaskPassword(volume);
|
2012-06-20 00:22:26 +00:00
|
|
|
} else {
|
2019-04-07 21:35:07 +00:00
|
|
|
if (e.message.includes('Compiled against a version of libcryptsetup that does not support the VeraCrypt PIM setting')) {
|
2023-08-06 22:34:20 +00:00
|
|
|
Main.notifyError(_('Unable to unlock volume'),
|
|
|
|
_('The installed udisks version does not support the PIM setting'));
|
2019-04-07 21:35:07 +00:00
|
|
|
}
|
|
|
|
|
2012-06-20 20:35:29 +00:00
|
|
|
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.FAILED_HANDLED))
|
2022-02-07 14:14:06 +00:00
|
|
|
log(`Unable to mount volume ${volume.get_name()}: ${e.toString()}`);
|
2012-06-20 00:22:26 +00:00
|
|
|
this._closeOperation(volume);
|
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-20 19:16:40 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onVolumeRemoved(monitor, volume) {
|
2018-07-04 14:55:28 +00:00
|
|
|
if (volume._allowAutorunExpireId && volume._allowAutorunExpireId > 0) {
|
2019-08-19 18:50:33 +00:00
|
|
|
GLib.source_remove(volume._allowAutorunExpireId);
|
2018-07-04 14:55:28 +00:00
|
|
|
delete volume._allowAutorunExpireId;
|
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-22 13:45:03 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_reaskPassword(volume) {
|
2018-10-05 17:09:28 +00:00
|
|
|
let prevOperation = this._activeOperations.get(volume);
|
2020-08-12 18:59:01 +00:00
|
|
|
const existingDialog = prevOperation?.borrowDialog();
|
2019-09-12 15:26:08 +00:00
|
|
|
let operation =
|
2012-06-20 00:22:26 +00:00
|
|
|
new ShellMountOperation.ShellMountOperation(volume,
|
2023-08-06 22:40:20 +00:00
|
|
|
{existingDialog});
|
2012-06-20 00:22:26 +00:00
|
|
|
this._mountVolume(volume, operation);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2012-06-20 00:22:26 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_closeOperation(volume) {
|
2018-10-05 17:09:28 +00:00
|
|
|
let operation = this._activeOperations.get(volume);
|
|
|
|
if (!operation)
|
|
|
|
return;
|
|
|
|
operation.close();
|
|
|
|
this._activeOperations.delete(volume);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-07-12 15:29:49 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_allowAutorun(volume) {
|
2011-06-22 13:45:03 +00:00
|
|
|
volume.allowAutorun = true;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-06-22 13:45:03 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_allowAutorunExpire(volume) {
|
2019-08-19 18:50:33 +00:00
|
|
|
let id = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, AUTORUN_EXPIRE_TIMEOUT_SECS, () => {
|
2011-06-22 13:45:03 +00:00
|
|
|
volume.allowAutorun = false;
|
2018-07-04 14:55:28 +00:00
|
|
|
delete volume._allowAutorunExpireId;
|
2013-11-29 00:45:39 +00:00
|
|
|
return GLib.SOURCE_REMOVE;
|
2011-06-22 13:45:03 +00:00
|
|
|
});
|
2018-07-04 14:55:28 +00:00
|
|
|
volume._allowAutorunExpireId = id;
|
2014-04-10 17:26:52 +00:00
|
|
|
GLib.Source.set_name_by_id(id, '[gnome-shell] volume.allowAutorun');
|
2011-06-20 19:16:40 +00:00
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {AutomountManager as Component};
|