misc.cpp revision 1bf248123399daf8d4bee9c30ba13b9887e5001e
11f5762e646bed2290934280464832782766ee68eMathias Agopian/*
21f5762e646bed2290934280464832782766ee68eMathias Agopian * Copyright (C) 2005 The Android Open Source Project
31f5762e646bed2290934280464832782766ee68eMathias Agopian *
41f5762e646bed2290934280464832782766ee68eMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
51f5762e646bed2290934280464832782766ee68eMathias Agopian * you may not use this file except in compliance with the License.
61f5762e646bed2290934280464832782766ee68eMathias Agopian * You may obtain a copy of the License at
71f5762e646bed2290934280464832782766ee68eMathias Agopian *
81f5762e646bed2290934280464832782766ee68eMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
91f5762e646bed2290934280464832782766ee68eMathias Agopian *
101f5762e646bed2290934280464832782766ee68eMathias Agopian * Unless required by applicable law or agreed to in writing, software
111f5762e646bed2290934280464832782766ee68eMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
121f5762e646bed2290934280464832782766ee68eMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131f5762e646bed2290934280464832782766ee68eMathias Agopian * See the License for the specific language governing permissions and
141f5762e646bed2290934280464832782766ee68eMathias Agopian * limitations under the License.
151f5762e646bed2290934280464832782766ee68eMathias Agopian */
161f5762e646bed2290934280464832782766ee68eMathias Agopian
171f5762e646bed2290934280464832782766ee68eMathias Agopian#define LOG_TAG "misc"
181f5762e646bed2290934280464832782766ee68eMathias Agopian
191f5762e646bed2290934280464832782766ee68eMathias Agopian//
201f5762e646bed2290934280464832782766ee68eMathias Agopian// Miscellaneous utility functions.
211f5762e646bed2290934280464832782766ee68eMathias Agopian//
221f5762e646bed2290934280464832782766ee68eMathias Agopian#include <androidfw/misc.h>
231f5762e646bed2290934280464832782766ee68eMathias Agopian
241f5762e646bed2290934280464832782766ee68eMathias Agopian#include <sys/stat.h>
251f5762e646bed2290934280464832782766ee68eMathias Agopian#include <string.h>
261f5762e646bed2290934280464832782766ee68eMathias Agopian#include <errno.h>
271f5762e646bed2290934280464832782766ee68eMathias Agopian#include <stdio.h>
281f5762e646bed2290934280464832782766ee68eMathias Agopian
291f5762e646bed2290934280464832782766ee68eMathias Agopianusing namespace android;
301f5762e646bed2290934280464832782766ee68eMathias Agopian
311f5762e646bed2290934280464832782766ee68eMathias Agopiannamespace android {
321f5762e646bed2290934280464832782766ee68eMathias Agopian
331f5762e646bed2290934280464832782766ee68eMathias Agopian/*
341f5762e646bed2290934280464832782766ee68eMathias Agopian * Get a file's type.
351f5762e646bed2290934280464832782766ee68eMathias Agopian */
361f5762e646bed2290934280464832782766ee68eMathias AgopianFileType getFileType(const char* fileName)
371f5762e646bed2290934280464832782766ee68eMathias Agopian{
381f5762e646bed2290934280464832782766ee68eMathias Agopian    struct stat sb;
391f5762e646bed2290934280464832782766ee68eMathias Agopian
401f5762e646bed2290934280464832782766ee68eMathias Agopian    if (stat(fileName, &sb) < 0) {
411f5762e646bed2290934280464832782766ee68eMathias Agopian        if (errno == ENOENT || errno == ENOTDIR)
421f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeNonexistent;
431f5762e646bed2290934280464832782766ee68eMathias Agopian        else {
441f5762e646bed2290934280464832782766ee68eMathias Agopian            fprintf(stderr, "getFileType got errno=%d on '%s'\n",
451f5762e646bed2290934280464832782766ee68eMathias Agopian                errno, fileName);
461f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeUnknown;
471f5762e646bed2290934280464832782766ee68eMathias Agopian        }
481f5762e646bed2290934280464832782766ee68eMathias Agopian    } else {
491f5762e646bed2290934280464832782766ee68eMathias Agopian        if (S_ISREG(sb.st_mode))
501f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeRegular;
511f5762e646bed2290934280464832782766ee68eMathias Agopian        else if (S_ISDIR(sb.st_mode))
521f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeDirectory;
531f5762e646bed2290934280464832782766ee68eMathias Agopian        else if (S_ISCHR(sb.st_mode))
541f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeCharDev;
551f5762e646bed2290934280464832782766ee68eMathias Agopian        else if (S_ISBLK(sb.st_mode))
561f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeBlockDev;
571f5762e646bed2290934280464832782766ee68eMathias Agopian        else if (S_ISFIFO(sb.st_mode))
581f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeFifo;
591bf248123399daf8d4bee9c30ba13b9887e5001eElliott Hughes#if defined(S_ISLNK)
601f5762e646bed2290934280464832782766ee68eMathias Agopian        else if (S_ISLNK(sb.st_mode))
611f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeSymlink;
621bf248123399daf8d4bee9c30ba13b9887e5001eElliott Hughes#endif
631bf248123399daf8d4bee9c30ba13b9887e5001eElliott Hughes#if defined(S_ISSOCK)
641f5762e646bed2290934280464832782766ee68eMathias Agopian        else if (S_ISSOCK(sb.st_mode))
651f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeSocket;
661f5762e646bed2290934280464832782766ee68eMathias Agopian#endif
671f5762e646bed2290934280464832782766ee68eMathias Agopian        else
681f5762e646bed2290934280464832782766ee68eMathias Agopian            return kFileTypeUnknown;
691f5762e646bed2290934280464832782766ee68eMathias Agopian    }
701f5762e646bed2290934280464832782766ee68eMathias Agopian}
711f5762e646bed2290934280464832782766ee68eMathias Agopian
721f5762e646bed2290934280464832782766ee68eMathias Agopian/*
731f5762e646bed2290934280464832782766ee68eMathias Agopian * Get a file's modification date.
741f5762e646bed2290934280464832782766ee68eMathias Agopian */
751f5762e646bed2290934280464832782766ee68eMathias Agopiantime_t getFileModDate(const char* fileName)
761f5762e646bed2290934280464832782766ee68eMathias Agopian{
771f5762e646bed2290934280464832782766ee68eMathias Agopian    struct stat sb;
781f5762e646bed2290934280464832782766ee68eMathias Agopian
791f5762e646bed2290934280464832782766ee68eMathias Agopian    if (stat(fileName, &sb) < 0)
801f5762e646bed2290934280464832782766ee68eMathias Agopian        return (time_t) -1;
811f5762e646bed2290934280464832782766ee68eMathias Agopian
821f5762e646bed2290934280464832782766ee68eMathias Agopian    return sb.st_mtime;
831f5762e646bed2290934280464832782766ee68eMathias Agopian}
841f5762e646bed2290934280464832782766ee68eMathias Agopian
851f5762e646bed2290934280464832782766ee68eMathias Agopian}; // namespace android
86