/判断路径是目录or文件

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h> 
//判断路径是 目录还是文件 0:文件 1:目录
int CheckIsFile(char *InPath, char OutPath[256])
{
    struct stat info;
    //LOGI("g_DataPath===%s", g_DataPath);
    //判断是相对路径还是绝对路径
    if (InPath[0] == '/')
    {
        LOGI("absolute InPath--->OutPat!\n");
        snprintf(OutPath, 256, "%s", InPath); 
    }
    else
    {
        snprintf(OutPath, 256, "%s/%s", g_DataPath, InPath); //g_DataPath由java端传递进来的路径 
    }


    //LOGI("g_DataPath_len==%d InPath_len==%d OutPath_len==%d ", sizeof(g_DataPath),sizeof(InPath),sizeof(*OutPath));
    //LOGI("InPath==%s OutPath==%s", InPath, OutPath);
    //路径还是文件?
    stat(OutPath, &info);
    
    if (S_ISREG(info.st_mode))//file
    {
        LOGI("This is a file!");
       // LOGI("Flag==%d %d %d ",access(OutPath, R_OK ),access(OutPath,  W_OK ), access(OutPath, F_OK));
        
        //是否是文件且可读写 
        if(access(OutPath, R_OK | W_OK | F_OK) == 0)
        {
             LOGI("file exsit and W&R!\n");
             return 0;
        }
        else
        {
            return -1;
        } 
    }
    else if(S_ISDIR(info.st_mode))//目录
    {
        LOGI("This is a directory\n");
        return 1;
    }
    else
    {
        LOGI("Others\n");
        return -2;
    } 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值