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 java.io.File;
  29 import java.io.IOException;
  30 import static java.lang.String.format;
  31 import java.nio.file.Files;
  32 import java.nio.file.InvalidPathException;
  33 import java.nio.file.Paths;
  34 import java.util.logging.Level;
  35 import java.util.logging.Logger;
  36 
  37 final class FileSystem {
  38 
  39     // File type should match native FileMetadata Type
  40     private static final int TYPE_UNKNOWN = 0;
  41     private static final int TYPE_FILE = 1;
  42     private static final int TYPE_DIRECTORY = 2;
  43 
  44     private final static Logger logger =
  45             Logger.getLogger(FileSystem.class.getName());
  46 
  47 
  48     private FileSystem() {
  49         throw new AssertionError();
  50     }
  51 
  52     private static boolean fwkFileExists(String path) {
  53         return new File(path).exists();
  54     }
  55 
  56     private static long fwkGetFileSize(String path) {
  57         try {
  58             File file = new File(path);
  59             if (file.exists()) {
  60                 return file.length();
  61             }
  62         } catch (SecurityException ex) {
  63             logger.log(Level.FINE, format("Error determining "
  64                     + "size of file [%s]", path), ex);
  65         }
  66         return -1;
  67     }
  68 
  69     private static boolean fwkGetFileMetadata(String path, long[] metadataArray) {
  70         try {
  71             File file = new File(path);
  72             if (file.exists()) {
  73                 metadataArray[0] = file.lastModified();
  74                 metadataArray[1] = file.length();
  75                 if (file.isDirectory()) {
  76                     metadataArray[2] = TYPE_DIRECTORY;
  77                 } else if (file.isFile()) {
  78                     metadataArray[2] = TYPE_FILE;
  79                 } else {
  80                     metadataArray[2] = TYPE_UNKNOWN;
  81                 }
  82                 return true;
  83             }
  84         } catch (SecurityException ex) {
  85             logger.log(Level.FINE, format("Error determining "
  86                     + "Metadata for file [%s]", path), ex);
  87         }
  88         return false;
  89     }
  90 
  91     private static String fwkPathByAppendingComponent(String path,
  92                                                       String component)
  93     {
  94         return new File(path, component).getPath();
  95     }
  96 
  97     private static boolean fwkMakeAllDirectories(String path) {
  98         try {
  99             Files.createDirectories(Paths.get(path));
 100             return true;
 101         } catch (InvalidPathException|IOException ex) {
 102             logger.log(Level.FINE, format("Error creating "
 103                     + "directory [%s]", path), ex);
 104             return false;
 105         }
 106     }
 107 
 108     private static String fwkPathGetFileName(String path) {
 109         return new File(path).getName();
 110     }
 111 }