Coverage Report - org.owasp.dependencycheck.org.apache.tools.ant.util.SymbolicLinkUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SymbolicLinkUtils
29%
5/17
0%
0/10
1.444
SymbolicLinkUtils$1
0%
0/2
N/A
1.444
 
 1  
 /*
 2  
  *  Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  *  contributor license agreements.  See the NOTICE file distributed with
 4  
  *  this work for additional information regarding copyright ownership.
 5  
  *  The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  *  (the "License"); you may not use this file except in compliance with
 7  
  *  the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  *  Unless required by applicable law or agreed to in writing, software
 12  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 13  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  *  See the License for the specific language governing permissions and
 15  
  *  limitations under the License.
 16  
  *
 17  
  */
 18  
 package org.owasp.dependencycheck.org.apache.tools.ant.util;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.FilenameFilter;
 22  
 import java.io.IOException;
 23  
 //import org.apache.tools.ant.Task;
 24  
 //import org.apache.tools.ant.taskdefs.Execute;
 25  
 
 26  
 /**
 27  
  * Contains methods related to symbolic links - or what Ant thinks is a symbolic link based on the absent support for
 28  
  * them in Java.
 29  
  *
 30  
  * @since Ant 1.8.0
 31  
  */
 32  
 public class SymbolicLinkUtils {
 33  
 
 34  1
     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
 35  
 
 36  
     /**
 37  
      * Shared instance.
 38  
      */
 39  1
     private static final SymbolicLinkUtils PRIMARY_INSTANCE
 40  
             = new SymbolicLinkUtils();
 41  
 
 42  
     /**
 43  
      * Method to retrieve The SymbolicLinkUtils, which is shared by all users of this method.
 44  
      *
 45  
      * @return an instance of SymbolicLinkUtils.
 46  
      */
 47  
     public static SymbolicLinkUtils getSymbolicLinkUtils() {
 48  
         // keep the door open for Java X.Y specific subclass if symbolic
 49  
         // links ever become supported in the classlib
 50  2
         return PRIMARY_INSTANCE;
 51  
     }
 52  
 
 53  
     /**
 54  
      * Empty constructor.
 55  
      */
 56  1
     protected SymbolicLinkUtils() {
 57  1
     }
 58  
 
 59  
     /**
 60  
      * Checks whether a given file is a symbolic link.
 61  
      *
 62  
      * <p>
 63  
      * It doesn't really test for symbolic links but whether the canonical and absolute paths of the file are
 64  
      * identical--this may lead to false positives on some platforms.</p>
 65  
      *
 66  
      * @param file the file to test. Must not be null.
 67  
      *
 68  
      * @return true if the file is a symbolic link.
 69  
      * @throws IOException on error.
 70  
      */
 71  
     public boolean isSymbolicLink(File file) throws IOException {
 72  0
         return isSymbolicLink(file.getParentFile(), file.getName());
 73  
     }
 74  
 
 75  
     /**
 76  
      * Checks whether a given file is a symbolic link.
 77  
      *
 78  
      * <p>
 79  
      * It doesn't really test for symbolic links but whether the canonical and absolute paths of the file are
 80  
      * identical--this may lead to false positives on some platforms.</p>
 81  
      *
 82  
      * @param name the name of the file to test.
 83  
      *
 84  
      * @return true if the file is a symbolic link.
 85  
      * @throws IOException on error.
 86  
      */
 87  
     public boolean isSymbolicLink(String name) throws IOException {
 88  0
         return isSymbolicLink(new File(name));
 89  
     }
 90  
 
 91  
     /**
 92  
      * Checks whether a given file is a symbolic link.
 93  
      *
 94  
      * <p>
 95  
      * It doesn't really test for symbolic links but whether the canonical and absolute paths of the file are
 96  
      * identical--this may lead to false positives on some platforms.</p>
 97  
      *
 98  
      * @param parent the parent directory of the file to test
 99  
      * @param name the name of the file to test.
 100  
      *
 101  
      * @return true if the file is a symbolic link.
 102  
      * @throws IOException on error.
 103  
      */
 104  
     public boolean isSymbolicLink(File parent, String name)
 105  
             throws IOException {
 106  0
         File toTest = parent != null
 107  
                 ? new File(parent.getCanonicalPath(), name)
 108  
                 : new File(name);
 109  0
         return !toTest.getAbsolutePath().equals(toTest.getCanonicalPath());
 110  
     }
 111  
 
 112  
     /**
 113  
      * Checks whether a given file is a broken symbolic link.
 114  
      *
 115  
      * <p>
 116  
      * It doesn't really test for symbolic links but whether Java reports that the File doesn't exist but its parent's
 117  
      * child list contains it--this may lead to false positives on some platforms.</p>
 118  
      *
 119  
      * <p>
 120  
      * Note that #isSymbolicLink returns false if this method returns true since Java won't produce a canonical name
 121  
      * different from the abolute one if the link is broken.</p>
 122  
      *
 123  
      * @param name the name of the file to test.
 124  
      *
 125  
      * @return true if the file is a broken symbolic link.
 126  
      * @throws IOException on error.
 127  
      */
 128  
     public boolean isDanglingSymbolicLink(String name) throws IOException {
 129  0
         return isDanglingSymbolicLink(new File(name));
 130  
     }
 131  
 
 132  
     /**
 133  
      * Checks whether a given file is a broken symbolic link.
 134  
      *
 135  
      * <p>
 136  
      * It doesn't really test for symbolic links but whether Java reports that the File doesn't exist but its parent's
 137  
      * child list contains it--this may lead to false positives on some platforms.</p>
 138  
      *
 139  
      * <p>
 140  
      * Note that #isSymbolicLink returns false if this method returns true since Java won't produce a canonical name
 141  
      * different from the abolute one if the link is broken.</p>
 142  
      *
 143  
      * @param file the file to test.
 144  
      *
 145  
      * @return true if the file is a broken symbolic link.
 146  
      * @throws IOException on error.
 147  
      */
 148  
     public boolean isDanglingSymbolicLink(File file) throws IOException {
 149  0
         return isDanglingSymbolicLink(file.getParentFile(), file.getName());
 150  
     }
 151  
 
 152  
     /**
 153  
      * Checks whether a given file is a broken symbolic link.
 154  
      *
 155  
      * <p>
 156  
      * It doesn't really test for symbolic links but whether Java reports that the File doesn't exist but its parent's
 157  
      * child list contains it--this may lead to false positives on some platforms.</p>
 158  
      *
 159  
      * <p>
 160  
      * Note that #isSymbolicLink returns false if this method returns true since Java won't produce a canonical name
 161  
      * different from the abolute one if the link is broken.</p>
 162  
      *
 163  
      * @param parent the parent directory of the file to test
 164  
      * @param name the name of the file to test.
 165  
      *
 166  
      * @return true if the file is a broken symbolic link.
 167  
      * @throws IOException on error.
 168  
      */
 169  
     public boolean isDanglingSymbolicLink(File parent, String name)
 170  
             throws IOException {
 171  0
         File f = new File(parent, name);
 172  0
         if (!f.exists()) {
 173  0
             final String localName = f.getName();
 174  0
             String[] c = parent.list(new FilenameFilter() {
 175  
                 public boolean accept(File d, String n) {
 176  0
                     return localName.equals(n);
 177  
                 }
 178  
             });
 179  0
             return c != null && c.length > 0;
 180  
         }
 181  0
         return false;
 182  
     }
 183  
 //
 184  
 //    /**
 185  
 //     * Delete a symlink (without deleting the associated resource).
 186  
 //     *
 187  
 //     * <p>This is a utility method that removes a unix symlink without
 188  
 //     * removing the resource that the symlink points to. If it is
 189  
 //     * accidentally invoked on a real file, the real file will not be
 190  
 //     * harmed, but silently ignored.</p>
 191  
 //     *
 192  
 //     * <p>Normally this method works by
 193  
 //     * getting the canonical path of the link, using the canonical path to
 194  
 //     * rename the resource (breaking the link) and then deleting the link.
 195  
 //     * The resource is then returned to its original name inside a finally
 196  
 //     * block to ensure that the resource is unharmed even in the event of
 197  
 //     * an exception.</p>
 198  
 //     *
 199  
 //     * <p>There may be cases where the algorithm described above doesn't work,
 200  
 //     * in that case the method tries to use the native "rm" command on
 201  
 //     * the symlink instead.</p>
 202  
 //     *
 203  
 //     * @param link A <code>File</code> object of the symlink to delete.
 204  
 //     * @param task An Ant Task required if "rm" needs to be invoked.
 205  
 //     *
 206  
 //     * @throws IOException If calls to <code>File.rename</code>,
 207  
 //     * <code>File.delete</code> or <code>File.getCanonicalPath</code>
 208  
 //     * fail.
 209  
 //     * @throws BuildException if the execution of "rm" failed.
 210  
 //     */
 211  
 //    public void deleteSymbolicLink(File link, Task task)
 212  
 //        throws IOException {
 213  
 //        if (isDanglingSymbolicLink(link)) {
 214  
 //            if (!link.delete()) {
 215  
 //                throw new IOException("failed to remove dangling symbolic link "
 216  
 //                                      + link);
 217  
 //            }
 218  
 //            return;
 219  
 //        }
 220  
 //
 221  
 //        if (!isSymbolicLink(link)) {
 222  
 //            // plain file, not a link
 223  
 //            return;
 224  
 //        }
 225  
 //
 226  
 //        if (!link.exists()) {
 227  
 //            throw new FileNotFoundException("No such symbolic link: " + link);
 228  
 //        }
 229  
 //
 230  
 //        // find the resource of the existing link:
 231  
 //        File target = link.getCanonicalFile();
 232  
 //
 233  
 //        // no reason to try the renaming algorithm if we aren't allowed to
 234  
 //        // write to the target's parent directory.  Let's hope that
 235  
 //        // File.canWrite works on all platforms.
 236  
 //
 237  
 //        if (task == null || target.getParentFile().canWrite()) {
 238  
 //
 239  
 //            // rename the resource, thus breaking the link:
 240  
 //            File temp = FILE_UTILS.createTempFile("symlink", ".tmp",
 241  
 //                                                  target.getParentFile(), false,
 242  
 //                                                  false);
 243  
 //
 244  
 //            if (FILE_UTILS.isLeadingPath(target, link)) {
 245  
 //                // link points to a parent directory, renaming the parent
 246  
 //                // will rename the file
 247  
 //                link = new File(temp,
 248  
 //                                FILE_UTILS.removeLeadingPath(target, link));
 249  
 //            }
 250  
 //
 251  
 //            boolean renamedTarget = false;
 252  
 //            try {
 253  
 //                try {
 254  
 //                    FILE_UTILS.rename(target, temp);
 255  
 //                    renamedTarget = true;
 256  
 //                } catch (IOException e) {
 257  
 //                    throw new IOException("Couldn't rename resource when "
 258  
 //                                          + "attempting to delete '" + link
 259  
 //                                          + "'.  Reason: " + e.getMessage());
 260  
 //                }
 261  
 //                // delete the (now) broken link:
 262  
 //                if (!link.delete()) {
 263  
 //                    throw new IOException("Couldn't delete symlink: "
 264  
 //                                          + link
 265  
 //                                          + " (was it a real file? is this "
 266  
 //                                          + "not a UNIX system?)");
 267  
 //                }
 268  
 //            } finally {
 269  
 //                if (renamedTarget) {
 270  
 //                    // return the resource to its original name:
 271  
 //                    try {
 272  
 //                        FILE_UTILS.rename(temp, target);
 273  
 //                    } catch (IOException e) {
 274  
 //                        throw new IOException("Couldn't return resource "
 275  
 //                                              + temp
 276  
 //                                              + " to its original name: "
 277  
 //                                              + target.getAbsolutePath()
 278  
 //                                              + ". Reason: " + e.getMessage()
 279  
 //                                              + "\n THE RESOURCE'S NAME ON DISK"
 280  
 //                                              + " HAS BEEN CHANGED BY THIS"
 281  
 //                                              + " ERROR!\n");
 282  
 //                    }
 283  
 //                }
 284  
 //            }
 285  
 //        } else {
 286  
 //            Execute.runCommand(task,
 287  
 //                               new String[] {"rm", link.getAbsolutePath()});
 288  
 //        }
 289  
 //    }
 290  
 
 291  
 }