xz5487 发表于 2012-08-22 23:54

文件传输中如何判断路径有效性且根据输入路径存在与否来创建新的路径或直接保存

如题,小弟正在做一个断点续传的功能实现,现在客户端将文件传过来之后服务端会提示输入需要保存的路径,当时做的很快,也没多想,就用了一个mkdir()直接就将文件放这了,但是后来一想,应该要判断输入的路径合法性和是否存在,需要达到以下功能才行1:文件是否存在(存在,则直接存在此目录下;不存在,但是路径正确,则创建,且存在此目录下;不存在,路径错误,报错);2:每次判断是否存在是判断输入的路径是否合法。小弟不才,刚毕业,很多东西还在学习,请各位大神赐教如何实现。谢谢啦!

xz5487 发表于 2012-08-23 20:21

难道就这样沉了?

xiyoulaoyuanjia 发表于 2012-08-23 23:37

有效性 根据 mkdir 函数返回值可以判断吧~~:em03:

dengxiayehu 发表于 2012-08-25 13:32

简单写了个,看看能否满足:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <errno.h>

// 777, please consider about umask
#define DIR_MODE    (S_IRWXU | S_IRWXG | S_IRWXO)

#define MY_PATH_MAX 1024

typedef enum {
    NOT_EXISTS,
    IS_FILE,
    IS_DIR
} file_state;

static long get_path_max();
static file_state get_file_state(const char *filepath);

int main(int argc, const char *argv[])
{
    long pathmax;
    char *dirpath = NULL;
    file_state state;

    pathmax = get_path_max();
    if (NULL == (dirpath = malloc(pathmax))) {
      perror("malloc error");
      goto err_ret_a;
    }

    printf("Enter dir-path: ");
    fgets(dirpath, pathmax, stdin);
    dirpath = '\0';
    // printf("dirpath is: %s\n", dirpath);

    state = get_file_state(dirpath);
    if (IS_DIR == state) {
      // what we wanted, save it there
      // printf("%s is a dir\n", dirpath);
    } else if (IS_FILE == state) {
      // printf("%s is a file, remove first\n", dirpath);
      // remove the file and create the directory instead
      if (-1 == unlink(dirpath)) {
            perror("unlink file error");
            goto err_ret_b;
      }

      // create the dir now
      if (-1 == mkdir(dirpath, DIR_MODE)) {
            perror("mkdir error");
            goto err_ret_b;
      }
      // printf("create dir %s ok.\n", dirpath);
    } else {
      // not exists, create the dir now
      // printf("we will create dir %s\n", dirpath);
      if (-1 == mkdir(dirpath, DIR_MODE)) {
            perror("mkdir error");
            goto err_ret_b;
      }
    }

    free(dirpath);
    return 0;

err_ret_b:
    free(dirpath);
err_ret_a:
    exit(EXIT_FAILURE);
}

static long get_path_max()
{
    long pathmax;

    errno = 0;
    if (-1 == (pathmax = sysconf(_PC_PATH_MAX))) {
      if (errno != 0) {
            perror("sysconf _PC_PATH_MAX error");
            exit(1);
      } else {
            pathmax = MY_PATH_MAX;
      }
    }
    return pathmax;
}

static file_state get_file_state(const char *filepath)
{
    struct stat buf;

    if (stat(filepath, &buf) != -1) {
      if (S_ISDIR(buf.st_mode)) {
            return IS_DIR;
      } else {
            return IS_FILE;
      }
    }
    return NOT_EXISTS;
}

dengxiayehu 发表于 2012-08-25 13:36

对了,还得考虑如下情况:
想要创建目录 aaa/bbb/ccc/ddd,但前面的aaa, aaa/bbb, aaa/bbb/ccc等目录若不存在的话,
创建目录也会失败,所以可以考虑类似于java中的mkdirs()方法。
总之,自己处理下逻辑咯~

xz5487 发表于 2012-08-29 22:31

谢谢你,我会逐步完善我的项目的,刚刚毕业,每一天都在收获,呵呵!回复 5# dengxiayehu


   
页: [1]
查看完整版本: 文件传输中如何判断路径有效性且根据输入路径存在与否来创建新的路径或直接保存