The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | abf945f | 2010-06-03 19:55:20 -0700 | [diff] [blame] | 18 | #define LOG_TAG "File" |
| 19 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 20 | #include "JNIHelp.h" |
Elliott Hughes | a9f5c16 | 2010-06-16 16:32:18 -0700 | [diff] [blame] | 21 | #include "JniConstants.h" |
Elliott Hughes | fb85a3a | 2011-01-16 16:06:11 -0800 | [diff] [blame] | 22 | #include "JniException.h" |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 23 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 24 | #include "ScopedUtfChars.h" |
Elliott Hughes | 95101d3 | 2010-11-05 10:49:14 -0700 | [diff] [blame] | 25 | #include "readlink.h" |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 26 | #include "toStringArray.h" |
Elliott Hughes | 95101d3 | 2010-11-05 10:49:14 -0700 | [diff] [blame] | 27 | |
| 28 | #include <string> |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 29 | #include <vector> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 31 | #include <dirent.h> |
| 32 | #include <errno.h> |
Elliott Hughes | 08ec8fd | 2010-03-12 14:46:47 -0800 | [diff] [blame] | 33 | #include <fcntl.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/stat.h> |
Elliott Hughes | 08ec8fd | 2010-03-12 14:46:47 -0800 | [diff] [blame] | 37 | #include <sys/types.h> |
| 38 | #include <time.h> |
| 39 | #include <unistd.h> |
| 40 | #include <utime.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 42 | static jstring File_readlink(JNIEnv* env, jclass, jstring javaPath) { |
| 43 | ScopedUtfChars path(env, javaPath); |
| 44 | if (path.c_str() == NULL) { |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 45 | return NULL; |
| 46 | } |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 47 | |
Elliott Hughes | 95101d3 | 2010-11-05 10:49:14 -0700 | [diff] [blame] | 48 | std::string result; |
| 49 | if (!readlink(path.c_str(), result)) { |
| 50 | jniThrowIOException(env, errno); |
| 51 | return NULL; |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 52 | } |
Elliott Hughes | 95101d3 | 2010-11-05 10:49:14 -0700 | [diff] [blame] | 53 | return env->NewStringUTF(result.c_str()); |
| 54 | } |
| 55 | |
| 56 | static jstring File_realpath(JNIEnv* env, jclass, jstring javaPath) { |
| 57 | ScopedUtfChars path(env, javaPath); |
| 58 | if (path.c_str() == NULL) { |
| 59 | return NULL; |
| 60 | } |
| 61 | |
| 62 | extern bool realpath(const char* path, std::string& resolved); |
| 63 | std::string result; |
| 64 | if (!realpath(path.c_str(), result)) { |
| 65 | jniThrowIOException(env, errno); |
| 66 | return NULL; |
| 67 | } |
| 68 | return env->NewStringUTF(result.c_str()); |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 71 | static jboolean File_setLastModifiedImpl(JNIEnv* env, jclass, jstring javaPath, jlong ms) { |
| 72 | ScopedUtfChars path(env, javaPath); |
| 73 | if (path.c_str() == NULL) { |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 74 | return JNI_FALSE; |
| 75 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 77 | // We want to preserve the access time. |
| 78 | struct stat sb; |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 79 | if (stat(path.c_str(), &sb) == -1) { |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 80 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 81 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 82 | |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 83 | // TODO: we could get microsecond resolution with utimes(3), "legacy" though it is. |
| 84 | utimbuf times; |
| 85 | times.actime = sb.st_atime; |
| 86 | times.modtime = static_cast<time_t>(ms / 1000); |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 87 | return (utime(path.c_str(), ×) == 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 90 | // Iterates over the filenames in the given directory. |
| 91 | class ScopedReaddir { |
| 92 | public: |
| 93 | ScopedReaddir(const char* path) { |
| 94 | mDirStream = opendir(path); |
| 95 | mIsBad = (mDirStream == NULL); |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 96 | } |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 97 | |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 98 | ~ScopedReaddir() { |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 99 | if (mDirStream != NULL) { |
| 100 | closedir(mDirStream); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 103 | |
| 104 | // Returns the next filename, or NULL. |
| 105 | const char* next() { |
Elliott Hughes | e940661 | 2012-04-16 15:38:05 -0700 | [diff] [blame] | 106 | if (mIsBad) { |
| 107 | return NULL; |
| 108 | } |
Elliott Hughes | 46cbaab | 2012-10-26 15:13:02 -0700 | [diff] [blame] | 109 | errno = 0; |
| 110 | dirent* result = readdir(mDirStream); |
| 111 | if (result != NULL) { |
| 112 | return result->d_name; |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 113 | } |
Elliott Hughes | 46cbaab | 2012-10-26 15:13:02 -0700 | [diff] [blame] | 114 | if (errno != 0) { |
| 115 | mIsBad = true; |
| 116 | } |
| 117 | return NULL; |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 118 | } |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 119 | |
| 120 | // Has an error occurred on this stream? |
| 121 | bool isBad() const { |
| 122 | return mIsBad; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | DIR* mDirStream; |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 127 | bool mIsBad; |
Elliott Hughes | 7ca6fd0 | 2010-03-29 20:51:48 -0700 | [diff] [blame] | 128 | |
| 129 | // Disallow copy and assignment. |
| 130 | ScopedReaddir(const ScopedReaddir&); |
| 131 | void operator=(const ScopedReaddir&); |
Elliott Hughes | 1326cfc | 2009-11-20 23:02:07 -0800 | [diff] [blame] | 132 | }; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 133 | |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 134 | typedef std::vector<std::string> DirEntries; |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 135 | |
| 136 | // Reads the directory referred to by 'pathBytes', adding each directory entry |
| 137 | // to 'entries'. |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 138 | static bool readDirectory(JNIEnv* env, jstring javaPath, DirEntries& entries) { |
| 139 | ScopedUtfChars path(env, javaPath); |
| 140 | if (path.c_str() == NULL) { |
Elliott Hughes | 6410112 | 2010-07-12 09:53:54 -0700 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 144 | ScopedReaddir dir(path.c_str()); |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 145 | const char* filename; |
| 146 | while ((filename = dir.next()) != NULL) { |
| 147 | if (strcmp(filename, ".") != 0 && strcmp(filename, "..") != 0) { |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 148 | // TODO: this hides allocation failures from us. Push directory iteration up into Java? |
| 149 | entries.push_back(filename); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 150 | } |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 151 | } |
Elliott Hughes | e940661 | 2012-04-16 15:38:05 -0700 | [diff] [blame] | 152 | return !dir.isBad(); |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 153 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 154 | |
Elliott Hughes | aaacdb0 | 2010-07-21 16:32:07 -0700 | [diff] [blame] | 155 | static jobjectArray File_listImpl(JNIEnv* env, jclass, jstring javaPath) { |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 156 | // Read the directory entries into an intermediate form. |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 157 | DirEntries entries; |
| 158 | if (!readDirectory(env, javaPath, entries)) { |
Elliott Hughes | f226fd4 | 2009-12-10 13:18:35 -0800 | [diff] [blame] | 159 | return NULL; |
| 160 | } |
| 161 | // Translate the intermediate form into a Java String[]. |
Elliott Hughes | ddafeb1 | 2011-02-18 15:14:29 -0800 | [diff] [blame] | 162 | return toStringArray(env, entries); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 163 | } |
| 164 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 165 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 166 | NATIVE_METHOD(File, listImpl, "(Ljava/lang/String;)[Ljava/lang/String;"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 167 | NATIVE_METHOD(File, readlink, "(Ljava/lang/String;)Ljava/lang/String;"), |
Elliott Hughes | 95101d3 | 2010-11-05 10:49:14 -0700 | [diff] [blame] | 168 | NATIVE_METHOD(File, realpath, "(Ljava/lang/String;)Ljava/lang/String;"), |
Elliott Hughes | e22935d | 2010-08-12 17:27:27 -0700 | [diff] [blame] | 169 | NATIVE_METHOD(File, setLastModifiedImpl, "(Ljava/lang/String;J)Z"), |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 170 | }; |
Elliott Hughes | 7cd6760 | 2012-05-03 17:21:04 -0700 | [diff] [blame] | 171 | void register_java_io_File(JNIEnv* env) { |
| 172 | jniRegisterNativeMethods(env, "java/io/File", gMethods, NELEM(gMethods)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 173 | } |