internal static string GetMessage(string filePath, FileAccess accessType, Exception underlyingException)
        {
            var msg = $@"Failed to get {accessType} access a file at <{filePath}>.{Environment.NewLine}Possible reasons:{Environment.NewLine}";

            switch (underlyingException)
            {
            //precaution, for completeness sake
            case ArgumentException _:     //for windows
            case NotSupportedException _: //for posix
                msg += $"* The file path refers to a non-file device.{Environment.NewLine}";
                break;

            case PathTooLongException _:     //precaution, for completeness sake
                msg += $"* The specified path, file name, or both exceed the system-defined maximum length.{Environment.NewLine}";
                break;

            case FileNotFoundException _:
                msg += $"* The file path refers to a non-existing file.{Environment.NewLine}";
                break;

            case SecurityException _:
                msg += $"* RavenDB process does not have the required permissions to open the file.{Environment.NewLine}";
                break;
            }

            if ((File.GetAttributes(filePath) & FileAttributes.Directory) != 0)
            {
                msg += $@"* The file path points to a directory and thus cannot be accessed as a file. {Environment.NewLine}";
            }

            if (PlatformDetails.RunningOnPosix)
            {
                msg +=
                    $"* The file may be locked by another process that has it opened. The 'lslk' utility can help list locking processes. Please refer to man pages for more information{Environment.NewLine}";
            }
            else
            {
                try
                {
                    var whoIsLocking = WhoIsLocking.GetProcessesUsingFile(filePath);
                    if (whoIsLocking.Count > 0) //on posix this will be always empty
                    {
                        msg += $@"* The file is being used by the following process(es): {string.Join(",", whoIsLocking)}. {Environment.NewLine}";
                    }
                }
                catch (Exception e)
                {
                    msg += $@"* Failed to identify which process(es) is holding the file: {e}. {Environment.NewLine}";
                }
            }

            return(msg);
        }
Ejemplo n.º 2
0
        private static void TryHandlingError(string directory, int i, Exception e)
        {
            if (i == Retries - 1) // last try also failed
            {
                foreach (var file in Directory.GetFiles(directory, "*", SearchOption.AllDirectories))
                {
                    var path = Path.GetFullPath(file);
                    try
                    {
                        File.Delete(path);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        throw new IOException(WhoIsLocking.ThisFile(path), ex);
                    }
                    catch (IOException ex)
                    {
                        var processesUsingFiles = WhoIsLocking.GetProcessesUsingFile(path);
                        if (processesUsingFiles.Count == 0)
                        {
                            throw new IOException("Unable to figure out who is locking " + path, ex);
                        }

                        var stringBuilder = new StringBuilder();
                        stringBuilder.Append("The following processes are locking ").Append(path).AppendLine();
                        foreach (var processesUsingFile in processesUsingFiles)
                        {
                            stringBuilder.Append(" ").Append(processesUsingFile.ProcessName).Append(' ').Append(processesUsingFile.Id).
                            AppendLine();
                        }
                        throw new IOException(stringBuilder.ToString(), ex);
                    }
                }
                throw new IOException("Could not delete " + Path.GetFullPath(directory), e);
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Thread.Sleep(100);
        }
Ejemplo n.º 3
0
        public virtual void Dispose()
        {
            Authentication.Disable();
            GC.SuppressFinalize(this);

            var errors = new List <Exception>();

            foreach (var store in stores)
            {
                try
                {
                    store.Dispose();
                }
                catch (Exception e)
                {
                    errors.Add(e);
                }
            }

            stores.Clear();

            foreach (var server in servers)
            {
                if (server == null)
                {
                    continue;
                }
                try
                {
                    server.Dispose();
                }
                catch (Exception e)
                {
                    errors.Add(e);
                }
            }

            servers.Clear();

            GC.Collect(2);
            GC.WaitForPendingFinalizers();

            foreach (var pathToDelete in pathsToDelete)
            {
                try
                {
                    ClearDatabaseDirectory(pathToDelete);
                }
                catch (Exception e)
                {
                    errors.Add(e);
                }
                finally
                {
                    if (File.Exists(pathToDelete)) // Just in order to be sure we didn't created a file in that path, by mistake)
                    {
                        errors.Add(new IOException(string.Format("We tried to delete the '{0}' directory, but failed because it is a file.\r\n{1}", pathToDelete,
                                                                 WhoIsLocking.ThisFile(pathToDelete))));
                    }
                    else if (Directory.Exists(pathToDelete))
                    {
                        string filePath;
                        try
                        {
                            filePath = Directory.GetFiles(pathToDelete, "*", SearchOption.AllDirectories).FirstOrDefault() ?? pathToDelete;
                        }
                        catch (Exception)
                        {
                            filePath = pathToDelete;
                        }
                        errors.Add(new IOException(string.Format("We tried to delete the '{0}' directory.\r\n{1}", pathToDelete,
                                                                 WhoIsLocking.ThisFile(filePath))));
                    }
                }
            }

            if (errors.Count > 0)
            {
                throw new AggregateException(errors);
            }
        }
Ejemplo n.º 4
0
        public static void DeletePaths(ConcurrentSet <string> pathsToDelete, ExceptionAggregator exceptionAggregator)
        {
            var localPathsToDelete = pathsToDelete.ToArray();

            foreach (var pathToDelete in localPathsToDelete)
            {
                pathsToDelete.TryRemove(pathToDelete);

                exceptionAggregator.Execute(() => ClearDatabaseDirectory(pathToDelete));

                if (File.Exists(pathToDelete))
                {
                    exceptionAggregator.Execute(() =>
                    {
                        throw new IOException(string.Format("We tried to delete the '{0}' directory, but failed because it is a file.\r\n{1}", pathToDelete, WhoIsLocking.ThisFile(pathToDelete)));
                    });
                }
                else if (Directory.Exists(pathToDelete))
                {
                    exceptionAggregator.Execute(() =>
                    {
                        string filePath;
                        try
                        {
                            filePath = Directory.GetFiles(pathToDelete, "*", SearchOption.AllDirectories).FirstOrDefault() ?? pathToDelete;
                        }
                        catch (Exception)
                        {
                            filePath = pathToDelete;
                        }

                        throw new IOException(string.Format("We tried to delete the '{0}' directory.\r\n{1}", pathToDelete, WhoIsLocking.ThisFile(filePath)));
                    });
                }
            }
        }