Example #1
0
        private int Mount(string passPhrase, IList <string> userNames)
        {
            var mountSection = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).Sections[MountSection.Name] as MountSection;

            if (mountSection == null)
            {
                throw new ConfigurationErrorsException("Mount configuration missing");
            }

            settingsPassPhrase = passPhrase;

            try {
                using (var logFactory = new LogFactory()) {
                    logger = logFactory.GetCurrentClassLogger();
                    var factory = InitializeCloudDriveFactory(mountSection.LibPath);
                    using (var tokenSource = new CancellationTokenSource()) {
                        var tasks = new List <Task>();
                        foreach (var driveElement in mountSection.Drives.Where(d => !userNames.Any() || userNames.Contains(d.UserName)))
                        {
                            var drive = factory.CreateCloudDrive(driveElement.Schema, driveElement.UserName, driveElement.Root, new CloudDriveParameters()
                            {
                                ApiKey = driveElement.ApiKey, EncryptionKey = driveElement.EncryptionKey, Parameters = driveElement.GetParameters()
                            });
                            if (!drive.TryAuthenticate())
                            {
                                var displayRoot = drive.DisplayRoot;
                                drive.Dispose();
                                logger.Warn($"Authentication failed for drive '{displayRoot}'");
                                continue;
                            }

                            var operations = new CloudOperations(drive, logger);

                            // HACK: handle non-unique parameter set of DokanOperations.Mount() by explicitely specifying AllocationUnitSize and SectorSize
                            tasks.Add(Task.Run(() => operations.Mount(driveElement.Root, DokanOptions.RemovableDrive | DokanOptions.MountManager | DokanOptions.CurrentSession, mountSection.Threads, 1100, TimeSpan.FromSeconds(driveElement.Timeout != 0 ? driveElement.Timeout : 20), null, 512, 512), tokenSource.Token));

                            var driveInfo = new DriveInfo(driveElement.Root);
                            while (!driveInfo.IsReady)
                            {
                                Thread.Sleep(10);
                            }
                            logger.Info($"Drive '{drive.DisplayRoot}' mounted successfully.");
                        }

                        Console.WriteLine("Press CTRL-BACKSPACE to clear log, any other key to unmount drives");
                        while (true)
                        {
                            var keyInfo = Console.ReadKey(true);
                            if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Control) && keyInfo.Key == ConsoleKey.Backspace)
                            {
                                Console.Clear();
                            }
                            else
                            {
                                break;
                            }
                        }

                        tokenSource.Cancel();

                        return(0);
                    }
                }
            } catch (Exception ex) {
                Console.Error.WriteLine($"{ex.GetType().Name}: {ex.Message}");
                return(-1);
            } finally {
                foreach (var driveElement in mountSection.Drives.Cast <DriveElement>())
                {
                    Dokan.Unmount(driveElement.Root[0]);
                }
                UIThread.Shutdown();
            }
        }
Example #2
0
 public static void ClassCleanup()
 {
     UIThread.Shutdown();
 }