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
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;
59#ifdef HAVE_SYMLINKS
60        else if (S_ISLNK(sb.st_mode))
61            return kFileTypeSymlink;
62        else if (S_ISSOCK(sb.st_mode))
63            return kFileTypeSocket;
64#endif
65        else
66            return kFileTypeUnknown;
67    }
68}
69
70/*
71 * Get a file's modification date.
72 */
73time_t getFileModDate(const char* fileName)
74{
75    struct stat sb;
76
77    if (stat(fileName, &sb) < 0)
78        return (time_t) -1;
79
80    return sb.st_mtime;
81}
82
83}; // namespace android
84