1 /*
2  * Copyright (C) 2005 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 #define LOG_TAG "misc"
18 
19 //
20 // Miscellaneous utility functions.
21 //
22 #include <androidfw/misc.h>
23 
24 #include <sys/stat.h>
25 #include <cstring>
26 #include <errno.h>
27 #include <cstdio>
28 
29 using namespace android;
30 
31 namespace android {
32 
33 /*
34  * Get a file's type.
35  */
getFileType(const char * fileName)36 FileType getFileType(const char* fileName)
37 {
38     struct stat sb;
39 
40     if (stat(fileName, &sb) < 0) {
41         if (errno == ENOENT || errno == ENOTDIR)
42             return kFileTypeNonexistent;
43         else {
44             fprintf(stderr, "getFileType got errno=%d on '%s'\n",
45                 errno, fileName);
46             return kFileTypeUnknown;
47         }
48     } else {
49         if (S_ISREG(sb.st_mode))
50             return kFileTypeRegular;
51         else if (S_ISDIR(sb.st_mode))
52             return kFileTypeDirectory;
53         else if (S_ISCHR(sb.st_mode))
54             return kFileTypeCharDev;
55         else if (S_ISBLK(sb.st_mode))
56             return kFileTypeBlockDev;
57         else if (S_ISFIFO(sb.st_mode))
58             return kFileTypeFifo;
59 #if defined(S_ISLNK)
60         else if (S_ISLNK(sb.st_mode))
61             return kFileTypeSymlink;
62 #endif
63 #if defined(S_ISSOCK)
64         else if (S_ISSOCK(sb.st_mode))
65             return kFileTypeSocket;
66 #endif
67         else
68             return kFileTypeUnknown;
69     }
70 }
71 
72 /*
73  * Get a file's modification date.
74  */
getFileModDate(const char * fileName)75 time_t getFileModDate(const char* fileName)
76 {
77     struct stat sb;
78 
79     if (stat(fileName, &sb) < 0)
80         return (time_t) -1;
81 
82     return sb.st_mtime;
83 }
84 
85 }; // namespace android
86