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