Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 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> |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 25 | #include <cstring> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 26 | #include <errno.h> |
Adam Lesinski | 4bf5810 | 2014-11-03 11:21:19 -0800 | [diff] [blame] | 27 | #include <cstdio> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 28 | |
| 29 | using namespace android; |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | /* |
| 34 | * Get a file's type. |
| 35 | */ |
| 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; |
Elliott Hughes | 1bf2481 | 2015-01-12 14:33:04 -0800 | [diff] [blame] | 59 | #if defined(S_ISLNK) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 60 | else if (S_ISLNK(sb.st_mode)) |
| 61 | return kFileTypeSymlink; |
Elliott Hughes | 1bf2481 | 2015-01-12 14:33:04 -0800 | [diff] [blame] | 62 | #endif |
| 63 | #if defined(S_ISSOCK) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 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 | */ |
| 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 |