Coverage Report - org.codehaus.plexus.util.NioFiles
 
Classes in this File Line Coverage Branch Coverage Complexity
NioFiles
10%
4/38
0%
0/22
2.375
 
 1  
 package org.codehaus.plexus.util;
 2  
 
 3  
 /*
 4  
  * Copyright 2007 The Codehaus Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 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  
 import java.nio.file.StandardCopyOption;
 25  
 import java.nio.file.attribute.BasicFileAttributes;
 26  
 import java.nio.file.attribute.PosixFilePermission;
 27  
 import java.util.HashSet;
 28  
 import java.util.Set;
 29  
 
 30  
 /**
 31  
  * Encapsulates use of java7 features, exposing mostly backward compatible types
 32  
  */
 33  
 @SuppressWarnings( "Since15" )
 34  0
 public class NioFiles
 35  
 {
 36  
     public static boolean isSymbolicLink( File file )
 37  
     {
 38  26
         return Files.isSymbolicLink( file.toPath() );
 39  
     }
 40  
 
 41  
     public static void chmod( File file, int mode )
 42  
         throws IOException
 43  
     {
 44  0
         Path path = file.toPath();
 45  0
         if ( !Files.isSymbolicLink( path ) )
 46  
         {
 47  0
             Files.setPosixFilePermissions( path, getPermissions( mode ) );
 48  
         }
 49  0
     }
 50  
 
 51  
     @SuppressWarnings( { "OctalInteger", "MagicNumber" } )
 52  
     private static Set<PosixFilePermission> getPermissions( int mode )
 53  
     {
 54  0
         Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
 55  
         // add owners permission
 56  0
         if ( ( mode & 0400 ) > 0 )
 57  
         {
 58  0
             perms.add( PosixFilePermission.OWNER_READ );
 59  
         }
 60  0
         if ( ( mode & 0200 ) > 0 )
 61  
         {
 62  0
             perms.add( PosixFilePermission.OWNER_WRITE );
 63  
         }
 64  0
         if ( ( mode & 0100 ) > 0 )
 65  
         {
 66  0
             perms.add( PosixFilePermission.OWNER_EXECUTE );
 67  
         }
 68  
         // add group permissions
 69  0
         if ( ( mode & 0040 ) > 0 )
 70  
         {
 71  0
             perms.add( PosixFilePermission.GROUP_READ );
 72  
         }
 73  0
         if ( ( mode & 0020 ) > 0 )
 74  
         {
 75  0
             perms.add( PosixFilePermission.GROUP_WRITE );
 76  
         }
 77  0
         if ( ( mode & 0010 ) > 0 )
 78  
         {
 79  0
             perms.add( PosixFilePermission.GROUP_EXECUTE );
 80  
         }
 81  
         // add others permissions
 82  0
         if ( ( mode & 0004 ) > 0 )
 83  
         {
 84  0
             perms.add( PosixFilePermission.OTHERS_READ );
 85  
         }
 86  0
         if ( ( mode & 0002 ) > 0 )
 87  
         {
 88  0
             perms.add( PosixFilePermission.OTHERS_WRITE );
 89  
         }
 90  0
         if ( ( mode & 0001 ) > 0 )
 91  
         {
 92  0
             perms.add( PosixFilePermission.OTHERS_EXECUTE );
 93  
         }
 94  0
         return perms;
 95  
     }
 96  
 
 97  
     public static long getLastModified( File file )
 98  
         throws IOException
 99  
     {
 100  0
         BasicFileAttributes basicFileAttributes = Files.readAttributes( file.toPath(), BasicFileAttributes.class );
 101  0
         return basicFileAttributes.lastModifiedTime().toMillis();
 102  
     }
 103  
 
 104  
     /**
 105  
      * Reads the target of the symbolic link
 106  
      *
 107  
      * @param symlink A file that is a symlink
 108  
      * @return A file that is the target of the symlink
 109  
      * @throws java.io.IOException
 110  
      */
 111  
 
 112  
     public static File readSymbolicLink( File symlink )
 113  
         throws IOException
 114  
     {
 115  0
         Path path = Files.readSymbolicLink( symlink.toPath() );
 116  0
         return path.toFile();
 117  
     }
 118  
 
 119  
     public static File createSymbolicLink( File symlink, File target )
 120  
         throws IOException
 121  
     {
 122  0
         Path link = symlink.toPath();
 123  0
         if ( Files.exists( link, LinkOption.NOFOLLOW_LINKS ) )
 124  
         {
 125  0
             Files.delete( link );
 126  
         }
 127  0
         link = Files.createSymbolicLink( link, target.toPath() );
 128  0
         return link.toFile();
 129  
     }
 130  
 
 131  
     public static boolean deleteIfExists( File file )
 132  
         throws IOException
 133  
     {
 134  1
         return Files.deleteIfExists( file.toPath() );
 135  
     }
 136  
 
 137  
     public static File copy( File source, File target )
 138  
         throws IOException
 139  
     {
 140  22
         Path copy = Files.copy( source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING,
 141  
                                 StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS );
 142  22
         return copy.toFile();
 143  
     }
 144  
 
 145  
 }