创建文件系统在C目录 [英] Create a directory in filesystem in C

查看:119
本文介绍了创建文件系统在C目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做在C项目,我被困在一件事上,我需要检查directoy/无功/日志/ PROJECT的存在,如果没有,我的程序必须创建它的aplication总会超级用户运行,这是即时通讯做:

I am doing a project in C, and I'm stuck on one thing, i need to check if the directoy "/var/log/PROJECT " exist, if not, my program must create it, the aplication will always run on super user, this is what im doing:

    struct stat st = {0};

  if (stat("/var/log/project/PROJECT", &st) == -1) {
    printf("im creating the folder\n");
    mode_t process_mask = umask(0);
    int result_code = mkdir("/var/log/project/PROJECT", 0777);
    umask(process_mask);

}else{
    printf("exist\n");

}

对不起,问到做功课,但IM真的卡住了...

Sorry for asking to "do my homework" but im really stuck...

推荐答案

好了,我要与我的怀疑运行。如果问题是,你试图创建不存在的目录的父目录,解决办法是在它之前创建父目录。这并不难做到用递归,谢天谢地。试试这个:

Well, I'm going to run with my suspicion. If the problem is that the parent directory of the directory you're trying to create does not exist, the solution is to create the parent directory before it. This is not terribly difficult to do with recursion, thankfully. Try this:

#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int create_directory_with_parents(char const *pathname, mode_t modus) {
  struct stat st;

  if(stat(pathname, &st) == -1) {
    // If the directory does not yet exist:
    //
    // Make sure the parent directory is there. dirname() gives us the name of
    // the parent directory, then we call this very function recursively because
    // we are, after all, in a function that makes sure directories exist.
    char *path_cpy = strdup(pathname);
    char *dir      = dirname(path_cpy);
    int   err      = create_directory_with_parents(dir, modus);
    free(path_cpy);

    // If we were able to make sure the parent directory exists,
    // make the directory we really want.
    if(err == 0) {
      mode_t process_mask = umask(0);
      int err = mkdir(pathname, modus);
      umask(process_mask);
    }

    // err is 0 if everything went well.
    return err;
  } else if(!S_ISDIR(st.st_mode)) {
    // If the "directory" exists but is not a directory, complain.
    errno = ENOTDIR;
    return -1;
  }

  // If the directory exists and is a directory, there's nothing to do and
  // nothing to complain about.
  return 0;
}

int main(void) {
  if(0 != create_directory_with_parents("/var/log/project/PROJECT", 0777)) {
    perror("Could not create directory or parent of directory: ");
  }
}

在第一个父目录中找到存在的递归结束;这是 / 在最新的。

The recursion ends when the first parent directory is found that exists; that is with / at the latest.

这个实现的一个局限是,所有的父目录将拥有相同的访问权限叶目录,这可能是也可能不是你想要的是。如果这不是你想要的,你必须在递归调用更改作案参数 create_directory_with_parents 。如何通过几个作案参数的父目录几层,可能必须建立是一个设计问题,它取决于你究竟需要什么东西,所以我不能给出一个统一的答案吧。

One limitation of this implementation is that all parent directories will have the same access rights as the leaf directory, which may or may not be what you want. If this is not what you want, you'll have to change the modus parameter in the recursive call to create_directory_with_parents. How to pass several modus parameters for several layers of parent directories that may have to be created is something of a design question that depends on what exactly your needs are, so I cannot give a general answer to it.

这篇关于创建文件系统在C目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆