1 /*
2  * Copyright (C) 2011 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 package com.android.compatibility.common.util;
18 
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 
25 /** Bits and pieces copied from hidden API of android.os.FileUtils. */
26 public class FileUtils {
27 
28     public static final int S_IFMT  = 0170000;
29     public static final int S_IFSOCK = 0140000;
30     public static final int S_IFLNK = 0120000;
31     public static final int S_IFREG = 0100000;
32     public static final int S_IFBLK = 0060000;
33     public static final int S_IFDIR = 0040000;
34     public static final int S_IFCHR = 0020000;
35     public static final int S_IFIFO = 0010000;
36 
37     public static final int S_ISUID = 0004000;
38     public static final int S_ISGID = 0002000;
39     public static final int S_ISVTX = 0001000;
40 
41     public static final int S_IRWXU = 00700;
42     public static final int S_IRUSR = 00400;
43     public static final int S_IWUSR = 00200;
44     public static final int S_IXUSR = 00100;
45 
46     public static final int S_IRWXG = 00070;
47     public static final int S_IRGRP = 00040;
48     public static final int S_IWGRP = 00020;
49     public static final int S_IXGRP = 00010;
50 
51     public static final int S_IRWXO = 00007;
52     public static final int S_IROTH = 00004;
53     public static final int S_IWOTH = 00002;
54     public static final int S_IXOTH = 00001;
55 
56     static {
57         try {
58             // Required only for the native methods.
59             System.loadLibrary("cts_jni");
60         } catch (UnsatisfiedLinkError e) {
61             System.out.println("JNI not loaded");
62         }
63     }
64 
65     public static class FileStatus {
66 
67         public int dev;
68         public int ino;
69         public int mode;
70         public int nlink;
71         public int uid;
72         public int gid;
73         public int rdev;
74         public long size;
75         public int blksize;
76         public long blocks;
77         public long atime;
78         public long mtime;
79         public long ctime;
80 
hasModeFlag(int flag)81         public boolean hasModeFlag(int flag) {
82             if (((S_IRWXU | S_IRWXG | S_IRWXO) & flag) != flag) {
83                 throw new IllegalArgumentException("Inappropriate flag " + flag);
84             }
85             return (mode & flag) == flag;
86         }
87 
isOfType(int type)88         public boolean isOfType(int type) {
89             if ((type & S_IFMT) != type) {
90                 throw new IllegalArgumentException("Unknown type " + type);
91             }
92             return (mode & S_IFMT) == type;
93         }
94     }
95 
96     /**
97      * @param path of the file to stat
98      * @param status object to set the fields on
99      * @param statLinks or don't stat links (lstat vs stat)
100      * @return whether or not we were able to stat the file
101      *
102      * If you call this method, make sure to link in the libcts_jni library.
103      */
getFileStatus(String path, FileStatus status, boolean statLinks)104     public native static boolean getFileStatus(String path, FileStatus status, boolean statLinks);
105 
106     /** If you call this method, make sure to link in the libcts_jni library. */
getUserName(int uid)107     public native static String getUserName(int uid);
108 
109     /** If you call this method, make sure to link in the libcts_jni library. */
getGroupName(int gid)110     public native static String getGroupName(int gid);
111 
112     /** If you call this method, make sure to link in the libcts_jni library. */
setPermissions(String file, int mode)113     public native static int setPermissions(String file, int mode);
114 
115     /**
116      * Copy data from a source stream to destFile.
117      * Return true if succeed, return false if failed.
118      */
copyToFile(InputStream inputStream, File destFile)119     public static boolean copyToFile(InputStream inputStream, File destFile) {
120         try {
121             if (destFile.exists()) {
122                 destFile.delete();
123             }
124             FileOutputStream out = new FileOutputStream(destFile);
125             try {
126                 byte[] buffer = new byte[4096];
127                 int bytesRead;
128                 while ((bytesRead = inputStream.read(buffer)) >= 0) {
129                     out.write(buffer, 0, bytesRead);
130                 }
131             } finally {
132                 out.flush();
133                 try {
134                     out.getFD().sync();
135                 } catch (IOException e) {
136                 }
137                 out.close();
138             }
139             return true;
140         } catch (IOException e) {
141             return false;
142         }
143     }
144 
createFile(File file, int numBytes)145     public static void createFile(File file, int numBytes) throws IOException {
146         File parentFile = file.getParentFile();
147         if (parentFile != null) {
148             parentFile.mkdirs();
149         }
150         byte[] buffer = new byte[numBytes];
151         FileOutputStream output = new FileOutputStream(file);
152         try {
153             output.write(buffer);
154         } finally {
155             output.close();
156         }
157     }
158 
readInputStreamFully(InputStream is)159     public static byte[] readInputStreamFully(InputStream is) {
160         ByteArrayOutputStream os = new ByteArrayOutputStream();
161         byte[] buffer = new byte[32768];
162         int count;
163         try {
164             while ((count = is.read(buffer)) != -1) {
165                 os.write(buffer, 0, count);
166             }
167             is.close();
168         } catch (IOException e) {
169             throw new RuntimeException(e);
170         }
171         return os.toByteArray();
172     }
173 }
174