View Javadoc
1   /*
2    * Copyright 2014 The Codehaus Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.plexus.components.io.attributes;
17  
18  import javax.annotation.Nonnull;
19  import java.io.File;
20  import java.io.IOException;
21  import java.nio.file.Files;
22  import java.nio.file.LinkOption;
23  import java.nio.file.Path;
24  
25  /**
26   * @author Kristian Rosenvold
27   */
28  public class SymlinkUtils
29  {
30      /**
31       * Reads the target of the symbolic link
32       *
33       * @param symlink A file that is a symlink
34       * @return A file that is the target of the symlink
35       * @throws java.io.IOException
36       */
37  
38      public static @Nonnull
39      File readSymbolicLink( @Nonnull File symlink )
40          throws IOException
41      {
42          final java.nio.file.Path path = java.nio.file.Files.readSymbolicLink( symlink.toPath() );
43          return path.toFile();
44      }
45  
46      public static @Nonnull
47      File createSymbolicLink( @Nonnull File symlink, File target )
48          throws IOException
49      {
50          Path link = symlink.toPath();
51          if ( !Files.exists( link, LinkOption.NOFOLLOW_LINKS ) )
52          {
53              link = java.nio.file.Files.createSymbolicLink( link, target.toPath() );
54          }
55          return link.toFile();
56  
57      }
58  }