1 /*
   2  * Copyright (c) 2011, 2018, 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 #include "config.h"
  27 #include "NotImplemented.h"
  28 #include "FileSystem.h"
  29 #include "FileMetadata.h"
  30 #include <wtf/java/JavaEnv.h>
  31 #include <wtf/text/CString.h>
  32 
  33 
  34 namespace WebCore {
  35 
  36 namespace FileSystem {
  37 
  38 static jclass GetFileSystemClass(JNIEnv* env)
  39 {
  40     static JGClass clazz(env->FindClass("com/sun/webkit/FileSystem"));
  41     ASSERT(clazz);
  42     return clazz;
  43 }
  44 
  45 bool fileExists(const String& path)
  46 {
  47     JNIEnv* env = WebCore_GetJavaEnv();
  48 
  49     static jmethodID mid = env->GetStaticMethodID(
  50             GetFileSystemClass(env),
  51             "fwkFileExists",
  52             "(Ljava/lang/String;)Z");
  53     ASSERT(mid);
  54 
  55     jboolean result = env->CallStaticBooleanMethod(
  56             GetFileSystemClass(env),
  57             mid,
  58             (jstring)path.toJavaString(env));
  59     CheckAndClearException(env);
  60 
  61     return jbool_to_bool(result);
  62 }
  63 
  64 bool deleteFile(const String&)
  65 {
  66     notImplemented();
  67     return false;
  68 }
  69 
  70 bool deleteEmptyDirectory(String const &)
  71 {
  72     notImplemented();
  73     return false;
  74 }
  75 
  76 bool getFileSize(const String& path, long long& result)
  77 {
  78     JNIEnv* env = WebCore_GetJavaEnv();
  79 
  80     static jmethodID mid = env->GetStaticMethodID(
  81             GetFileSystemClass(env),
  82             "fwkGetFileSize",
  83             "(Ljava/lang/String;)J");
  84     ASSERT(mid);
  85 
  86     jlong size = env->CallStaticLongMethod(
  87             GetFileSystemClass(env),
  88             mid,
  89             (jstring) path.toJavaString(env));
  90     CheckAndClearException(env);
  91 
  92     if (size >= 0) {
  93         result = size;
  94         return true;
  95     } else {
  96         return false;
  97     }
  98 }
  99 
 100 bool getFileModificationTime(const String& path, time_t& result)
 101 {
 102   std::optional<FileMetadata> metadata = fileMetadata(path);
 103     if (metadata) {
 104         result = metadata->modificationTime;
 105         return true;
 106     } else {
 107         return false;
 108     }
 109 }
 110 
 111 bool getFileCreationTime(const String&, time_t&)
 112 {
 113     notImplemented(); // todo tav
 114     return false;
 115 }
 116 
 117 String pathByAppendingComponents(StringView path, const Vector<StringView>& components)
 118 {
 119     String result = path.toString();
 120     // FIXME-java: Use nio.file.Paths.get(...)
 121     for (const auto& component : components) {
 122         result = pathByAppendingComponent(result, component.toString());
 123     }
 124 
 125     return result;
 126 }
 127 
 128 String pathByAppendingComponent(const String& path, const String& component)
 129 {
 130     JNIEnv* env = WebCore_GetJavaEnv();
 131 
 132     static jmethodID mid = env->GetStaticMethodID(
 133             GetFileSystemClass(env),
 134             "fwkPathByAppendingComponent",
 135             "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
 136     ASSERT(mid);
 137 
 138     JLString result = static_cast<jstring>(env->CallStaticObjectMethod(
 139             GetFileSystemClass(env),
 140             mid,
 141             (jstring)path.toJavaString(env),
 142             (jstring)component.toJavaString(env)));
 143     CheckAndClearException(env);
 144 
 145     return String(env, result);
 146 }
 147 
 148 bool makeAllDirectories(const String& path)
 149 {
 150     JNIEnv* env = WebCore_GetJavaEnv();
 151 
 152     static jmethodID mid = env->GetStaticMethodID(
 153             GetFileSystemClass(env),
 154             "fwkMakeAllDirectories",
 155             "(Ljava/lang/String;)Z");
 156     ASSERT(mid);
 157 
 158     jboolean result = env->CallStaticBooleanMethod(
 159             GetFileSystemClass(env),
 160             mid,
 161             (jstring)path.toJavaString(env));
 162     CheckAndClearException(env);
 163 
 164     return jbool_to_bool(result);
 165 }
 166 
 167 String homeDirectoryPath()
 168 {
 169     notImplemented();
 170     return "";
 171 }
 172 
 173 String directoryName(String const &)
 174 {
 175     notImplemented();
 176     return String();
 177 }
 178 
 179 std::optional<FileMetadata> fileMetadata(const String& path)
 180 {
 181     JNIEnv* env = WebCore_GetJavaEnv();
 182 
 183     static jmethodID mid = env->GetStaticMethodID(
 184             GetFileSystemClass(env),
 185             "fwkGetFileMetadata",
 186             "(Ljava/lang/String;[J)Z");
 187     ASSERT(mid);
 188 
 189     JLocalRef<jlongArray> lArray(env->NewLongArray(3));
 190 
 191     jboolean result = env->CallStaticBooleanMethod(
 192             GetFileSystemClass(env),
 193             mid,
 194             (jstring)path.toJavaString(env), (jlongArray)lArray);
 195     CheckAndClearException(env);
 196 
 197     if (result) {
 198         jlong* metadataResults = env->GetLongArrayElements(lArray, 0);
 199         FileMetadata metadata {};
 200         metadata.modificationTime = metadataResults[0] / 1000.0;
 201         metadata.length = metadataResults[1];
 202         metadata.type = static_cast<FileMetadata::Type>(metadataResults[2]);
 203         env->ReleaseLongArrayElements(lArray, metadataResults, 0);
 204         return metadata;
 205     }
 206     return {};
 207 }
 208 
 209 std::optional<FileMetadata> fileMetadataFollowingSymlinks(const String& path)
 210 {
 211     // TODO-java: Use nio Files to avoid sym link traversal
 212     return fileMetadata(path);
 213 }
 214 
 215 Vector<String> listDirectory(const String&, const String&)
 216 {
 217     Vector<String> entities;
 218     notImplemented();
 219     return entities;
 220 }
 221 
 222 CString fileSystemRepresentation(const String& s)
 223 {
 224     notImplemented();
 225     return CString(s.latin1().data());
 226 }
 227 
 228 String openTemporaryFile(const String&, PlatformFileHandle& handle)
 229 {
 230     notImplemented();
 231     handle = invalidPlatformFileHandle;
 232     return String();
 233 }
 234 
 235 PlatformFileHandle openFile(const String&, FileOpenMode)
 236 {
 237     notImplemented();
 238     return invalidPlatformFileHandle;
 239 }
 240 
 241 void closeFile(PlatformFileHandle&)
 242 {
 243     notImplemented();
 244 }
 245 
 246 int readFromFile(PlatformFileHandle, char*, int)
 247 {
 248     notImplemented();
 249     return -1;
 250 }
 251 
 252 int writeToFile(PlatformFileHandle, const char*, int)
 253 {
 254     notImplemented();
 255     return -1;
 256 }
 257 
 258 String pathGetFileName(const String& path)
 259 {
 260     JNIEnv* env = WebCore_GetJavaEnv();
 261 
 262     static jmethodID mid = env->GetStaticMethodID(
 263             GetFileSystemClass(env),
 264             "fwkPathGetFileName",
 265             "(Ljava/lang/String;)Ljava/lang/String;");
 266     ASSERT(mid);
 267 
 268     JLString result = static_cast<jstring>(env->CallStaticObjectMethod(
 269             GetFileSystemClass(env),
 270             mid,
 271             (jstring) path.toJavaString(env)));
 272     CheckAndClearException(env);
 273 
 274     return String(env, result);
 275 }
 276 
 277 long long seekFile(PlatformFileHandle, long long, FileSeekOrigin)
 278 {
 279     notImplemented();
 280     return (long long)(-1);
 281 }
 282 
 283 std::optional<int32_t> getFileDeviceId(const CString&)
 284 {
 285     notImplemented();
 286     return {};
 287 }
 288 
 289 } // namespace FileSystem
 290 
 291 } // namespace WebCore