blob: 52854205207c03dd242d2f074b98ecb7ca1ae3f7 [file] [log] [blame]
Adam Lesinski16c4d152014-01-24 13:27:13 -08001/*
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>
Adam Lesinski4bf58102014-11-03 11:21:19 -080025#include <cstring>
Adam Lesinski16c4d152014-01-24 13:27:13 -080026#include <errno.h>
Adam Lesinski4bf58102014-11-03 11:21:19 -080027#include <cstdio>
Adam Lesinski16c4d152014-01-24 13:27:13 -080028
29using namespace android;
30
31namespace android {
32
33/*
34 * Get a file's type.
35 */
36FileType 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;
Elliott Hughes1bf24812015-01-12 14:33:04 -080059#if defined(S_ISLNK)
Adam Lesinski16c4d152014-01-24 13:27:13 -080060 else if (S_ISLNK(sb.st_mode))
61 return kFileTypeSymlink;
Elliott Hughes1bf24812015-01-12 14:33:04 -080062#endif
63#if defined(S_ISSOCK)
Adam Lesinski16c4d152014-01-24 13:27:13 -080064 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 */
75time_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