Beispiel #1
0
        /**
         * Copies {@code sourceFiles} to the {@code destDir} directory.
         *
         * @param sourceFiles the list of source files.
         * @param destDir the directory to copy the files to.
         * @throws IOException if the copy fails.
         */
        public static void Copy(ImmutableArray <SystemPath> sourceFiles, SystemPath destDir)
        {
            foreach (SystemPath sourceFile in sourceFiles)
            {
                PathConsumer copyPathConsumer =
                    path =>
                {
                    // Creates the same path in the destDir.
                    SystemPath destPath = destDir.Resolve(sourceFile.GetParent().Relativize(path));
                    if (Files.IsDirectory(path))
                    {
                        Files.CreateDirectories(destPath);
                    }
                    else
                    {
                        Files.Copy(path, destPath);
                    }
                };

                if (Files.IsDirectory(sourceFile))
                {
                    new DirectoryWalker(sourceFile).Walk(copyPathConsumer);
                }
                else
                {
                    copyPathConsumer.Accept(sourceFile);
                }
            }
        }
Beispiel #2
0
 /**
  * Initialize with a root directory to walk.
  *
  * @param rootDir the root directory.
  * @throws NotDirectoryException if the root directory is not a directory.
  */
 public DirectoryWalker(SystemPath rootDir)
 {
     rootDir = rootDir ?? throw new ArgumentNullException(nameof(rootDir));
     if (!Files.IsDirectory(rootDir))
     {
         throw new ArgumentException(rootDir + " is not a directory", nameof(rootDir));
     }
     this.rootDir = rootDir;
 }
Beispiel #3
0
 internal void MoveIfDoesNotExist(SystemPath destination)
 {
     if (Directory.Exists(destination) || File.Exists(destination))
     {
         return;
     }
     try
     {
         Directory.Move(path, destination);
     }
     catch (IOException)
     {
         if (!Directory.Exists(destination))
         {
             throw;
         }
     }
 }
Beispiel #4
0
        internal void MoveIfDoesNotExist(SystemPath destination)
        {
            // If the file already exists, we skip renaming and use the existing file. This happens if a
            // new layer happens to have the same content as a previously-cached layer.
            if (File.Exists(destination))
            {
                return;
            }

            try
            {
                File.Move(Path, destination);
            }
            catch (IOException)
            {
                if (!File.Exists(destination))
                {
                    // TODO to log that the destination exists
                    throw;
                }
            }
        }
Beispiel #5
0
        /**
         * Returns {@code $XDG_CACHE_HOME}, if available, or resolves the OS-specific user cache home
         * based.
         *
         * <p>For Linus, this is {@code $HOME/.cache/}.
         *
         * <p>For Windows, this is {@code %LOCALAPPDATA%}.
         *
         * <p>For macOS, this is {@code $HOME/Library/Application Support/}.
         */

        public static SystemPath GetCacheHome(IEnvironment environment)
        {
            environment = environment ?? throw new ArgumentNullException(nameof(environment));
            // Use environment variable $XDG_CACHE_HOME if set and not empty.
            string xdgCacheHome = environment.GetEnvironmentVariable("XDG_CACHE_HOME");

            if (!string.IsNullOrWhiteSpace(xdgCacheHome))
            {
                return(Paths.Get(xdgCacheHome));
            }

            // Next, try using localAppData.
            string localAppData = environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            if (!string.IsNullOrWhiteSpace(localAppData))
            {
                return(Paths.Get(localAppData));
            }

            string userHome = environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

            if (environment.IsOsx())
            {
                // Use '~/Library/Application Support/' for macOS.
                SystemPath applicationSupport = Paths.Get(userHome, "Library", "Application Support");
                if (Files.Exists(applicationSupport))
                {
                    return(applicationSupport);
                }
            }

            if (!string.IsNullOrWhiteSpace(userHome))
            {
                return(Paths.Get(userHome, ".cache"));
            }

            throw new InvalidOperationException(Resources.UserCacheHomeMissingUserProfileExceptionMessage);
        }
Beispiel #6
0
 public TemporaryFile()
 {
     Path = new SystemPath(System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()));
 }
Beispiel #7
0
 public TemporaryFile(SystemPath path)
 {
     Path = path;
 }
Beispiel #8
0
 /**
  * Creates a lock file.
  *
  * @param lockFile the path of the lock file
  * @return a new {@link LockFile} that can be released later
  * @throws IOException if creating the lock file fails
  */
 public static LockFile Create(SystemPath lockFile)
 {
     lockFile = lockFile ?? throw new ArgumentNullException(nameof(lockFile));
     Files.CreateDirectories(lockFile.GetParent());
     return(new LockFile(lockFile.ToFile().Create()));
 }
Beispiel #9
0
 /**
  * Acquires an exclusive {@link FileLock} on the {@code file} and opens an {@link OutputStream} to
  * write to it. The file will be created if it does not exist, or truncated to length 0 if it does
  * exist. The {@link OutputStream} must be closed to release the lock.
  *
  * <p>The locking mechanism should not be used as a concurrency management feature. Rather, this
  * should be used as a way to prevent concurrent writes to {@code file}. Concurrent attempts to
  * lock {@code file} will result in {@link OverlappingFileLockException}s.
  *
  * @param file the file to write to
  * @return an {@link OutputStream} that writes to the file
  * @throws IOException if an I/O exception occurs
  */
 public static Stream NewLockingOutputStream(SystemPath file)
 {
     file = file ?? throw new ArgumentNullException(nameof(file));
     return(file.ToFile().Create());
 }
Beispiel #10
0
 public static void Accept(this PathConsumer c, SystemPath path)
 {
     c = c ?? throw new ArgumentNullException(nameof(c));
     c(path);
 }
Beispiel #11
0
 /**
  * Creates a new temporary directory under an existing {@code parentDirectory}.
  *
  * @param parentDirectory the directory to create the temporary directory within
  * @throws IOException if an I/O exception occurs
  */
 public TemporaryDirectory(string parentDirectory)
 {
     path = Files.CreateTempDirectory(parentDirectory, null);
 }
Beispiel #12
0
 /**
  * Creates a new temporary directory under an existing {@code parentDirectory}.
  *
  * @param parentDirectory the directory to create the temporary directory within
  * @throws IOException if an I/O exception occurs
  */
 public TemporaryDirectory(SystemPath parentDirectory)
 {
     parentDirectory = parentDirectory ?? throw new ArgumentNullException(nameof(parentDirectory));
     path            = Files.CreateTempDirectory(parentDirectory, null);
 }