示例#1
0
        /// <summary>
        /// Create zip archive for requested <code>sourceToCompress</code>.
        /// If <code>sourceToCompress</code> is a directory then content of that directory and all its sub-directories will be added to the archive.
        /// If <code>sourceToCompress</code> does not exist or is an empty directory then archive will not be created. </summary>
        /// <param name="fileSystem"> source file system </param>
        /// <param name="sourceToCompress"> source file to compress </param>
        /// <param name="destinationZip"> zip file compress source to </param>
        /// <exception cref="IOException"> when underlying file system access produce IOException </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void zip(org.neo4j.io.fs.FileSystemAbstraction fileSystem, java.io.File sourceToCompress, java.io.File destinationZip) throws java.io.IOException
        public static void Zip(FileSystemAbstraction fileSystem, File sourceToCompress, File destinationZip)
        {
            if (!fileSystem.FileExists(sourceToCompress))
            {
                return;
            }
            if (IsEmptyDirectory(fileSystem, sourceToCompress))
            {
                return;
            }
            IDictionary <string, string> env = MapUtil.stringMap("create", "true");
            Path rootPath           = sourceToCompress.toPath();
            URI  archiveAbsoluteURI = URI.create("jar:file:" + destinationZip.toURI().RawPath);

            using (FileSystem zipFs = FileSystems.newFileSystem(archiveAbsoluteURI, env))
            {
                IList <FileHandle> fileHandles = fileSystem.StreamFilesRecursive(sourceToCompress).collect(toList());
                foreach (FileHandle fileHandle in fileHandles)
                {
                    Path sourcePath = fileHandle.File.toPath();
                    Path zipFsPath  = fileSystem.IsDirectory(sourceToCompress) ? zipFs.getPath(rootPath.relativize(sourcePath).ToString()) : zipFs.getPath(sourcePath.FileName.ToString());
                    if (zipFsPath.Parent != null)
                    {
                        Files.createDirectories(zipFsPath.Parent);
                    }
                    Files.copy(sourcePath, zipFsPath);
                }
            }
        }
示例#2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void dumpCountsStore(org.neo4j.io.fs.FileSystemAbstraction fs, java.io.File path, java.io.PrintStream out) throws Exception
//JAVA TO C# CONVERTER NOTE: Members cannot have the same name as their enclosing type:
        public static void DumpCountsStoreConflict(FileSystemAbstraction fs, File path, PrintStream @out)
        {
            using (JobScheduler jobScheduler = createInitialisedScheduler(), PageCache pages = createPageCache(fs, jobScheduler), Lifespan life = new Lifespan())
            {
                NullLogProvider logProvider = NullLogProvider.Instance;
                Config          config      = Config.defaults();
                if (fs.IsDirectory(path))
                {
                    DatabaseLayout databaseLayout = DatabaseLayout.of(path);
                    StoreFactory   factory        = new StoreFactory(databaseLayout, Config.defaults(), new DefaultIdGeneratorFactory(fs), pages, fs, logProvider, EmptyVersionContextSupplier.EMPTY);

                    NeoStores     neoStores     = factory.OpenAllNeoStores();
                    SchemaStorage schemaStorage = new SchemaStorage(neoStores.SchemaStore);
                    neoStores.Counts.accept(new DumpCountsStore(@out, neoStores, schemaStorage));
                }
                else
                {
                    VisitableCountsTracker tracker = new VisitableCountsTracker(logProvider, fs, pages, config, DatabaseLayout.of(path.ParentFile));
                    if (fs.FileExists(path))
                    {
                        tracker.VisitFile(path, new DumpCountsStore(@out));
                    }
                    else
                    {
                        life.Add(tracker).accept(new DumpCountsStore(@out));
                    }
                }
            }
        }
示例#3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private int scrambleIndexFiles(org.neo4j.io.fs.FileSystemAbstraction fs, java.io.File fileOrDir) throws java.io.IOException
        private int ScrambleIndexFiles(FileSystemAbstraction fs, File fileOrDir)
        {
            if (fs.IsDirectory(fileOrDir))
            {
                int    count    = 0;
                File[] children = fs.ListFiles(fileOrDir);
                if (children != null)
                {
                    foreach (File child in children)
                    {
                        count += ScrambleIndexFiles(fs, child);
                    }
                }
                return(count);
            }
            else
            {
                // Completely scramble file, assuming small files
                using (StoreChannel channel = fs.Open(fileOrDir, OpenMode.READ_WRITE))
                {
                    if (channel.size() > mebiBytes(10))
                    {
                        throw new System.ArgumentException("Was expecting small files here");
                    }
                    sbyte[] bytes = new sbyte[( int )channel.size()];
                    _random.NextBytes(bytes);
                    channel.WriteAll(ByteBuffer.wrap(bytes));
                }
                return(1);
            }
        }
示例#4
0
 private static bool IsEmptyDirectory(FileSystemAbstraction fileSystem, File sourceToCompress)
 {
     if (fileSystem.IsDirectory(sourceToCompress))
     {
         File[] files = fileSystem.ListFiles(sourceToCompress);
         return(files == null || Files.Length == 0);
     }
     return(false);
 }
示例#5
0
 private static Stream <File> StreamFilesRecursiveInner(File directory, FileSystemAbstraction fs)
 {
     File[] files = fs.ListFiles(directory);
     if (files == null)
     {
         if (!fs.FileExists(directory))
         {
             return(Stream.empty());
         }
         return(Stream.of(directory));
     }
     else
     {
         return(Stream.of(files).flatMap(f => fs.IsDirectory(f) ? StreamFilesRecursiveInner(f, fs) : Stream.of(f)));
     }
 }
示例#6
0
 /// <summary>
 /// Calculates the size of a given directory or file given the provided abstract filesystem.
 /// </summary>
 /// <param name="fs"> the filesystem abstraction to use </param>
 /// <param name="file"> to the file or directory. </param>
 /// <returns> the size, in bytes, of the file or the total size of the content in the directory, including
 /// subdirectories. </returns>
 public static long Size(FileSystemAbstraction fs, File file)
 {
     if (fs.IsDirectory(file))
     {
         long   size  = 0L;
         File[] files = fs.ListFiles(file);
         if (files == null)
         {
             return(0L);
         }
         foreach (File child in files)
         {
             size += size(fs, child);
         }
         return(size);
     }
     else
     {
         return(fs.GetFileSize(file));
     }
 }
示例#7
0
        /// <summary>
        /// Deletes index folder with the specific indexId, but has the option to first archive the index if it exists.
        /// The zip archive will be placed next to the root directory for that index with a timestamp included in its name.
        /// </summary>
        /// <param name="fs"> <seealso cref="FileSystemAbstraction"/> this index lives in. </param>
        /// <param name="directoryStructure"> <seealso cref="IndexDirectoryStructure"/> knowing the directory structure for the provider owning the index. </param>
        /// <param name="indexId"> id of the index. </param>
        /// <param name="archiveIfExists"> whether or not to archive the index before deleting it, if it exists. </param>
        /// <returns> whether or not an archive was created. </returns>
        /// <exception cref="IOException"> on I/O error. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static boolean deleteIndex(org.neo4j.io.fs.FileSystemAbstraction fs, org.neo4j.kernel.api.index.IndexDirectoryStructure directoryStructure, long indexId, boolean archiveIfExists) throws java.io.IOException
        public static bool DeleteIndex(FileSystemAbstraction fs, IndexDirectoryStructure directoryStructure, long indexId, bool archiveIfExists)
        {
            File rootIndexDirectory = directoryStructure.DirectoryForIndex(indexId);

            if (archiveIfExists && fs.IsDirectory(rootIndexDirectory) && fs.FileExists(rootIndexDirectory) && fs.ListFiles(rootIndexDirectory).Length > 0)
            {
                ZipUtils.zip(fs, rootIndexDirectory, new File(rootIndexDirectory.Parent, "archive-" + rootIndexDirectory.Name + "-" + DateTimeHelper.CurrentUnixTimeMillis() + ".zip"));
                return(true);
            }
            int attempt = 0;

            while (attempt < 5)
            {
                attempt++;
                try
                {
                    fs.DeleteRecursively(rootIndexDirectory);
                    break;
                }
                catch (Exception concurrentModificationException) when(concurrentModificationException is DirectoryNotEmptyException || concurrentModificationException is NoSuchFileException)
                {
                    // Looks like someone was poking around in our directory while we where deleting.
                    // Let's sleep for a bit and try again.
                    try
                    {
                        Thread.Sleep(100);
                    }
                    catch (InterruptedException)
                    {
                        // Let's abandon this attempt to clean up.
                        Thread.CurrentThread.Interrupt();
                        break;
                    }
                }
            }
            return(false);
        }