Example #1
0
 public void Stop()
 {
     if (IsRunning)
     {
         FireStateChange(LiquesceSvcState.Unknown, "Stop has been requested");
         int retVal = Dokan.DokanUnmount(mountedDriveLetter);
         Log.Info("Stop returned[{0}]", retVal);
     }
 }
Example #2
0
        /// <summary>
        /// Invokes DokanNet.DokanMain function to mount a drive.
        /// The function blocks until the file system is unmounted.
        /// Administrator privilege is needed to communicate with Dokan driver.
        /// You need a manifest file for .NET application.
        /// </summary>
        /// <returns></returns>
        public void Start(object obj)
        {
            try
            {
                int repeatWait = 0;
                while (IsRunning &&
                       (repeatWait++ < 100)
                       )
                {
                    Log.Warn("Last Dokan is still running");
                    Thread.Sleep(250);
                }
                if (!IsRunning)
                {
                    if (currentConfigDetails == null)
                    {
                        ReadConfigDetails();
                    }
                    FireStateChange(LiquesceSvcState.InError, "Starting up");
                    if (currentConfigDetails == null)
                    {
                        Log.Fatal("Unable to read the config details to allow this service to run. Will now exit");
                        Environment.Exit(-1);
                        // ReSharper disable HeuristicUnreachableCode
                        return;
                        // ReSharper restore HeuristicUnreachableCode
                    }
                    SetNLogLevel(currentConfigDetails.ServiceLogLevel);

                    FireStateChange(LiquesceSvcState.Unknown, "Dokan initialised");
                    IsRunning = true;

                    // TODO: Search all usages of the DriveLetter and make sure they become MountPoint compatible
                    string mountPoint = currentConfigDetails.DriveLetter;
                    //if (mountPoint.Length == 1)
                    //   mountPoint += ":\\"; // Make this into a MountPoint for V 0.6.0
                    DokanOptions options = new DokanOptions
                    {
                        MountPoint  = mountPoint,
                        ThreadCount = currentConfigDetails.ThreadCount,
                        DebugMode   = currentConfigDetails.DebugMode,
                        //      public bool UseStdErr;
                        // UseAltStream = true, // This needs all sorts of extra API's
                        UseKeepAlive = true,  // When you set TRUE on DokanOptions->UseKeepAlive, dokan library automatically unmounts 15 seconds after user-mode file system hanged up
                        NetworkDrive = false, // Set this to true to see if it stops the recycler bin question until [workitem:7253] is sorted
                        VolumeLabel  = currentConfigDetails.VolumeLabel
                    };

                    IServicePlugin plugin = pluginProviders.Where(pluginProvider => 0 == String.Compare(pluginProvider.Metadata.Description, currentConfigDetails.PluginMode, true)).Select(pluginProvider => pluginProvider.Value.Create()).FirstOrDefault();
                    plugin.Initialise(currentConfigDetails.DriveLetter, currentConfigDetails.HoldOffBufferBytes);
                    dokanOperations = new LiquesceOps(currentConfigDetails, plugin);
                    ThreadPool.QueueUserWorkItem(dokanOperations.InitialiseShares, dokanOperations);

                    mountedDriveLetter = currentConfigDetails.DriveLetter[0];


                    try
                    {
                        Log.Info("DokanVersion:[{0}], DokanDriverVersion[{1}]", Dokan.DokanVersion(), Dokan.DokanDriverVersion());
                        Dokan.DokanUnmount(mountedDriveLetter);
                    }
                    catch (Exception ex)
                    {
                        Log.InfoException("Make sure it's unmounted threw:", ex);
                    }
                    int retVal = Dokan.DokanMain(options, dokanOperations);
                    Log.Warn("Dokan.DokanMain has exited");
                    IsRunning = false;
                    switch (retVal)
                    {
                    case Dokan.DOKAN_SUCCESS: // = 0;
                        FireStateChange(LiquesceSvcState.Stopped, "Dokan is not mounted");
                        break;

                    case Dokan.DOKAN_ERROR:// = -1; // General Error
                        FireStateChange(LiquesceSvcState.InError, "Dokan is not mounted [DOKAN_ERROR] - General Error");
                        break;

                    case Dokan.DOKAN_DRIVE_LETTER_ERROR: // = -2; // Bad Drive letter
                        FireStateChange(LiquesceSvcState.InError, "Dokan is not mounted [DOKAN_DRIVE_LETTER_ERROR] - Bad drive letter");
                        break;

                    case Dokan.DOKAN_DRIVER_INSTALL_ERROR: // = -3; // Can't install driver
                        FireStateChange(LiquesceSvcState.InError, "Dokan is not mounted [DOKAN_DRIVER_INSTALL_ERROR]");
                        Environment.Exit(-1);
                        break;

                    case Dokan.DOKAN_START_ERROR: // = -4; // Driver something wrong
                        FireStateChange(LiquesceSvcState.InError, "Dokan is not mounted [DOKAN_START_ERROR] - Driver Something is wrong");
                        Environment.Exit(-1);
                        break;

                    case Dokan.DOKAN_MOUNT_ERROR: // = -5; // Can't assign drive letter
                        FireStateChange(LiquesceSvcState.InError, "Dokan is not mounted [DOKAN_MOUNT_ERROR] - Can't assign drive letter");
                        break;

                    default:
                        FireStateChange(LiquesceSvcState.InError, String.Format("Dokan is not mounted [Uknown Error: {0}]", retVal));
                        Environment.Exit(-1);
                        break;
                    }
                }
                else
                {
                    FireStateChange(LiquesceSvcState.InError, "Seems like the last exit request into Dokan did not exit in time");
                }
            }
            catch (Exception ex)
            {
                Log.ErrorException("Start has failed in an uncontrolled way: ", ex);
                Environment.Exit(-1);
            }
            finally
            {
                IsRunning = false;
            }
        }