1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <sys/stat.h>
18 #include <string>
19 
20 #include <jni.h>
21 #include "JNIHelp.h"
22 #include "ScopedUtfChars.h"
23 
Java_libcore_java_io_FileTest_nativeTestFilesWithSurrogatePairs(JNIEnv * env,jobject,jstring baseDir)24 extern "C" void Java_libcore_java_io_FileTest_nativeTestFilesWithSurrogatePairs(
25     JNIEnv* env, jobject /* clazz */, jstring baseDir) {
26   ScopedUtfChars baseDirUtf(env, baseDir);
27 
28   std::string base(baseDirUtf.c_str());
29   std::string subDir = base + "/dir_\xF0\x93\x80\x80";
30   std::string subFile = subDir + "/file_\xF0\x93\x80\x80";
31 
32   struct stat sb;
33   int ret = stat(subDir.c_str(), &sb);
34   if (ret == -1) {
35       jniThrowIOException(env, errno);
36   }
37   if (!S_ISDIR(sb.st_mode)) {
38       jniThrowException(env, "java/lang/IllegalStateException", "expected dir");
39   }
40 
41   ret = stat(subFile.c_str(), &sb);
42   if (ret == -1) {
43       jniThrowIOException(env, errno);
44   }
45 
46   if (!S_ISREG(sb.st_mode)) {
47       jniThrowException(env, "java/lang/IllegalStateException", "expected file");
48   }
49 }
50