182
193
warning ("Failed to get file info for file %s", file.uri);
199
debug ("success %s; enclosing mount %s", success.to_string (), file.mount != null ? file.mount.get_name () : "null");
185
200
yield make_ready (is_no_info || success, file_loaded_func); /* Only place that should call this function */
188
203
/*** Returns false if should be able to get info but were unable to ***/
189
204
private async bool get_file_info () {
190
/* Force info to be refreshed - the GOF.File may have been created already by another part of the program
191
* that did not ensure the correct info Aync purposes, and retrieved from cache (bug 1511307).
205
debug ("get_file_info");
195
207
if (is_network && !yield check_network ()) {
208
warning ("No network found");
196
209
file.is_connected = false;
201
/* Not a failure when not expected to get file info */
214
debug ("Loading info for local directory");
206
215
return file.ensure_query_info ();
209
218
if (!yield try_query_info ()) { /* may already be mounted */
219
debug ("try query info failed - trying to mount");
210
220
if (yield mount_mountable ()) {
211
221
/* Previously mounted Samba servers still appear mounted even if disconnected
212
222
* e.g. by unplugging the network cable. So the following function can block for
213
223
* a long time; we therefore use a timeout */
214
224
debug ("successful mount %s", file.uri);
215
return yield try_query_info ();
225
file.is_mounted = true;
226
return (yield try_query_info ()) || is_no_info;
228
warning ("failed mount %s", file.uri);
256
271
private async bool mount_mountable () {
272
debug ("mount_mountable");
274
var mount_op = new Gtk.MountOperation (null);
275
cancellable = new Cancellable ();
258
var mount_op = new Gtk.MountOperation (null);
259
cancellable = new Cancellable ();
260
278
bool mounting = true;
261
279
bool asking_password = false;
262
280
assert (mount_timeout_id == 0);
264
282
mount_timeout_id = Timeout.add_seconds (MOUNT_TIMEOUT_SEC, () => {
265
283
if (mounting && !asking_password) {
284
mount_timeout_id = 0;
266
285
warning ("Cancelled after timeout in mount mountable %s", file.uri);
286
last_error_message = ("Timed out when trying to mount %s").printf (file.uri);
287
state = State.TIMED_OUT;
267
288
cancellable.cancel ();
268
mount_timeout_id = 0;
275
296
mount_op.ask_password.connect (() => {
297
debug ("Asking for password");
276
298
asking_password = true;
279
301
mount_op.reply.connect (() => {
302
debug ("Password dialog finished");
280
303
asking_password = false;
283
yield location.mount_enclosing_volume (0, mount_op, cancellable);
284
var mount = location.find_enclosing_mount ();
286
debug ("Found enclosing mount %s", mount != null ? mount.get_name () : "null");
287
return mount != null;
306
debug ("mounting ....");
307
res =yield location.mount_enclosing_volume (GLib.MountMountFlags.NONE, mount_op, cancellable);
288
308
} catch (Error e) {
309
last_error_message = e.message;
289
310
if (e is IOError.ALREADY_MOUNTED) {
290
311
debug ("Already mounted %s", file.uri);
291
312
file.is_connected = true;
292
314
} else if (e is IOError.NOT_FOUND) {
293
315
debug ("Enclosing mount not found %s (may be remote share)", file.uri);
294
file.is_mounted = false;
316
/* Do not fail loading at this point - may still load */
318
yield location.mount_mountable (GLib.MountMountFlags.NONE, mount_op, cancellable);
320
} catch (GLib.Error e2) {
321
last_error_message = e2.message;
322
warning ("Unable to mount mountable");
297
327
file.is_connected = false;
298
328
file.is_mounted = false;
329
debug ("Setting mount null 1");
299
331
warning ("Mount_mountable failed: %s", e.message);
300
332
if (e is IOError.PERMISSION_DENIED || e is IOError.FAILED_HANDLED) {
301
333
permission_denied = true;
306
337
cancel_timeout (ref mount_timeout_id);
340
debug ("success %s; enclosing mount %s", res.to_string (), file.mount != null ? file.mount.get_name () : "null");
310
344
public async bool check_network () {
345
debug ("check network");
311
346
var net_mon = GLib.NetworkMonitor.get_default ();
312
347
network_available = net_mon.get_network_available ();
314
349
bool success = false;
316
351
if (network_available) {
317
SocketConnectable? connectable = null;
319
connectable = NetworkAddress.parse_uri (file.uri, 21);
320
if (((NetworkAddress)(connectable)).get_hostname () != "" && scheme != "smb") {
321
success = net_mon.can_reach (connectable, cancellable);
322
/* Try to connect for real. This should time out after about 15 seconds if
323
* the host is not reachable */
324
var scl = new SocketClient ();
325
var sc = yield scl.connect_async (connectable, cancellable);
326
success = (sc != null && sc.is_connected ());
327
debug ("Attempt to connect to %s %s", file.uri, success ? "succeeded" : "failed");
352
if (!file.is_mounted) {
353
debug ("Network is available");
354
if (scheme != "smb") {
356
/* Try to connect for real. */
357
var scl = new SocketClient ();
358
scl.set_timeout (CONNECT_SOCKET_TIMEOUT_SEC);
359
scl.set_tls (PF.FileUtils.get_is_tls_for_protocol (scheme));
360
debug ("Trying to connect to connectable");
361
var sc = yield scl.connect_to_uri_async (file.uri, PF.FileUtils.get_default_port_for_protocol (scheme), cancellable);
362
success = (sc != null && sc.is_connected ());
363
debug ("Socketclient is %s", sc == null ? "null" : (sc.is_connected () ? "connected" : "not connected"));
364
} catch (GLib.Error e) {
365
last_error_message = e.message;
366
warning ("Error: could not connect to connectable %s - %s", file.uri, e.message);
332
catch (GLib.Error e) {
333
warning ("Error connecting to connectable %s - %s", file.uri, e.message);
373
debug ("File is already mounted - not reconnecting");
338
377
warning ("No network available");
381
debug ("Attempt to connect to %s %s", file.uri, success ? "succeeded" : "failed");
344
386
private async void make_ready (bool ready, GOFFileLoadedFunc? file_loaded_func = null) {
387
debug ("make ready");
345
388
can_load = ready;
347
390
warning ("%s cannot load. Connected %s, Mounted %s, Exists %s", file.uri,
348
391
file.is_connected.to_string (),
349
392
file.is_mounted.to_string (),
350
393
file.exists.to_string ());
351
state = State.NOT_LOADED; /* ensure state is correct */
394
after_loading (file_loaded_func);
524
580
/* This may hang for a long time if the connection was closed but is still mounted so we
525
581
* impose a time limit */
526
582
load_timeout_id = Timeout.add_seconds (ENUMERATE_TIMEOUT_SEC, () => {
527
cancellable.cancel ();
583
if (server_responding) {
586
debug ("Load timeout expired");
587
state = State.TIMED_OUT;
588
last_error_message = _("Server did not respond within time limit");
590
cancellable.cancel ();
532
596
var e = yield this.location.enumerate_children_async (gio_attrs, 0, Priority.HIGH, cancellable);
533
cancel_timeout (ref load_timeout_id);
597
debug ("Obtained file enumerator for location %s", location.get_uri ());
537
601
while (!cancellable.is_cancelled ()) {
538
var files = yield e.next_files_async (200, 0, cancellable);
542
foreach (var file_info in files) {
543
loc = location.get_child (file_info.get_name ());
544
assert (loc != null);
545
gof = GOF.File.cache_lookup (loc);
548
gof = new GOF.File (loc, location); /*does not add to GOF file cache */
603
server_responding = false;
604
var files = yield e.next_files_async (200, GLib.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, cancellable);
605
server_responding = true;
610
foreach (var file_info in files) {
611
loc = location.get_child (file_info.get_name ());
612
assert (loc != null);
613
gof = GOF.File.cache_lookup (loc);
616
gof = new GOF.File (loc, location); /*does not add to GOF file cache */
619
gof.info = file_info;
622
file_hash.insert (gof.location, gof);
623
after_load_file (gof, show_hidden, file_loaded_func);
551
gof.info = file_info;
554
file_hash.insert (gof.location, gof);
555
after_load_file (gof, show_hidden, file_loaded_func);
628
last_error_message = e.message;
629
warning ("Error reported by next_files_async - %s", e.message);
560
state = State.LOADED;
632
/* Load as many files as we can get info for */
633
if (!(cancellable.is_cancelled ())) {
634
state = State.LOADED;
561
636
} catch (Error err) {
562
warning ("Listing directory error: %s %s", err.message, file.uri);
637
warning ("Listing directory error: %s, %s %s", last_error_message, err.message, file.uri);
563
638
can_load = false;
564
639
if (err is IOError.NOT_FOUND || err is IOError.NOT_DIRECTORY) {
565
640
file.exists = false;
566
} else if (err is IOError.PERMISSION_DENIED)
641
} else if (err is IOError.PERMISSION_DENIED) {
567
642
permission_denied = true;
568
else if (err is IOError.NOT_MOUNTED)
643
} else if (err is IOError.NOT_MOUNTED) {
569
645
file.is_mounted = false;
648
cancel_timeout (ref load_timeout_id);
649
after_loading (file_loaded_func);
572
after_loading (file_loaded_func);
575
653
private void after_load_file (GOF.File gof, bool show_hidden, GOFFileLoadedFunc? file_loaded_func) {