1 /*
   2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.webkit;
  27 
  28 import com.sun.javafx.logging.PlatformLogger;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import static java.lang.String.format;
  32 import java.nio.file.Files;
  33 import java.nio.file.InvalidPathException;
  34 import java.nio.file.Paths;
  35 
  36 final class FileSystem {
  37 
  38     // File type should match native FileMetadata Type
  39     private static final int TYPE_UNKNOWN = 0;
  40     private static final int TYPE_FILE = 1;
  41     private static final int TYPE_DIRECTORY = 2;
  42 
  43     private final static PlatformLogger logger =
  44             PlatformLogger.getLogger(FileSystem.class.getName());
  45 
  46 
  47     private FileSystem() {
  48         throw new AssertionError();
  49     }
  50 
  51     private static boolean fwkFileExists(String path) {
  52         return new File(path).exists();
  53     }
  54 
  55     private static long fwkGetFileSize(String path) {
  56         try {
  57             File file = new File(path);
  58             if (file.exists()) {
  59                 return file.length();
  60             }
  61         } catch (SecurityException ex) {
  62             logger.fine(format("Error determining size of file [%s]", path), ex);
  63         }
  64         return -1;
  65     }
  66 
  67     private static boolean fwkGetFileMetadata(String path, long[] metadataArray) {
  68         try {
  69             File file = new File(path);
  70             if (file.exists()) {
  71                 metadataArray[0] = file.lastModified();
  72                 metadataArray[1] = file.length();
  73                 if (file.isDirectory()) {
  74                     metadataArray[2] = TYPE_DIRECTORY;
  75                 } else if (file.isFile()) {
  76                     metadataArray[2] = TYPE_FILE;
  77                 } else {
  78                     metadataArray[2] = TYPE_UNKNOWN;
  79                 }
  80                 return true;
  81             }
  82         } catch (SecurityException ex) {
  83             logger.fine(format("Error determining Metadata for file [%s]", path), ex);
  84         }
  85         return false;
  86     }
  87 
  88     private static String fwkPathByAppendingComponent(String path,
  89                                                       String component)
  90     {
  91         return new File(path, component).getPath();
  92     }
  93 
  94     private static boolean fwkMakeAllDirectories(String path) {
  95         try {
  96             Files.createDirectories(Paths.get(path));
  97             return true;
  98         } catch (InvalidPathException|IOException ex) {
  99             logger.fine(format("Error creating directory [%s]", path), ex);
 100             return false;
 101         }
 102     }
 103 
 104     private static String fwkPathGetFileName(String path) {
 105         return new File(path).getName();
 106     }
 107 }