1,mkdir

Problem: You want to use the mkdir() function from the sys/stat.h POSIX header, but you don’t know what the mode_t argument should look like.

Solution:

For a detailed reference, see the Opengroup page on mkdir

The first argument should be obvious - just enter the path name of the directory you intend to create. If you use a std::string (in C++); use it’s c_str() member function to get a C string.

The second argument defines the permissions the newly created directory shall have. This How-to assumes you’re already familiar with Unix file permissions. If you are not, please read the corresponding Wikipedia Page.

First, decide which rights the directory shall have. This boils down to these 9 questions:

  • Shall the owner be able to read/write/execute?
  • Shall the group be able to read/write/execute?
  • Shall everyone else (= others) be able to read/write/execute? The second argument has the type mode_t, but this is basically just an alias for any type of integer.

sys/stat.h provides you with several integers you can bytewise-OR (|) together to create your mode_t:

  • User: S_IRUSR (read), S_IWUSR (write), S_IXUSR (execute)
  • Group: S_IRGRP (read), S_IWGRP (write), S_IXGRP (execute)
  • Others: S_IROTH (read), S_IWOTH (write), S_IXOTH (execute)

Additionally, some shortcuts are provided (basically a bitwise-OR combination of the above

  • Read + Write + Execute: S_IRWXU (User), S_IRWXG (Group), S_IRWXO (Others)
  • DEFFILEMODE: Equivalent of 0666 = rw-rw-rw-
  • ACCESSPERMS: Equivalent of 0777 = rwxrwxrwx Therefore, to give only the user rwx (read+write+execute) rights whereas group members and others may not do anything, you can use any of the following mkdir() calls equivalently:
mkdir("mydir", S_IRUSR | S_IWUSR | S_IXUSR);
mkdir("mydir", S_IRWXU);

In order to give anyone any rights (mode 0777 = rwxrwxrwx), you can use any of the following calls equivalently:

mkdir("mydir", S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
mkdir("mydir", S_IRWXU | S_IRWXG | S_IRWXO);
mkdir("mydir", ACCESSPERMS);

mkdir的更多相关文章

  1. 编译Openwrt的log

    Openwrt配置: Target System (Ralink RT288x/RT3xxx) ---> Subtarget (MT7688 based boards) ---> Targ ...

  2. linux常用命令(3)mkdir命令

    mkdir命令1 命令格式:mkdir [选项]目录名2 命令功能:通过 mkdir 命令可以实现在指定位置创建以 DirName(指定的文件名)命名的文件夹或目录.要创建文件夹或目录的用户必须对所创 ...

  3. mkdir,rmdir,cp,rm,mv,cat,touch用法

    一.mkdir新建目录 1.进入tmp目录,查看该目录下面的子目录 [root@localhost ~]# cd /tmp[root@localhost tmp]# lshsperfdata_root ...

  4. mkdir创建目录

    mkdir:make directories(创建目录) 创建目录的首要条件:在当前目录或者欲创建目录下,该用户具有写入权限,mkdir详细功能如下: 1.mkdir不接任何参数时,即mkdir di ...

  5. mkdir命令

    [mkdir]   创建目录     mkdir ===make directory 命令格式: mkdir [OPTION]... DIRECTORY 命令功能: 通过 mkdir 命令可以实现在指 ...

  6. PHP mkdir 0777权限问题

    在linux系统中,即使我们使用root帐号去手工执行php命令: mkdir('test', 0777); 结果文件的权限依然为: drwxr-xr-x 2 root root 4096 Jun 1 ...

  7. Linux mkdir 创建文件夹命令

    介绍: 该命令创建指定的目录名,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录 语法: mkdir [-m] [-p] 目录名 选项介绍: -m: 对新建目录设置 ...

  8. 【YEOMAN】执行yo命令,报EACCES: permission denied, mkdir '/root/.config/configstore'

    基础环境:CentOS7.Nodejs6.0之上,yo:1.8.4 在执行yo初始化webapp时,报错,错误内容如下: Error: EACCES: permission denied, mkdir ...

  9. Linux_用户级_常用命令(3):mkdir

    Linux常用命令之mkdir 开篇语:懒是人类进步的源动力 本文原创,专为光荣之路公众号所有,欢迎转发,但转发请务必写出处! Linux常用命令第3集包含命令:mkdir (附赠tree命令,日期时 ...

  10. java mkdir()和mkdirs()的区别

    boolean mkdir()    创建此抽象路径名指定的目录.   boolean mkdirs()    创建此抽象路径名指定的目录,包括创建必需但不存在的父目录. 也就是说,mkdir只能创建 ...

随机推荐

  1. iOS下json的解析 NSJSONSerialization

      - (IBAction)JOSNButtonPressed:(id)sender { NSString *str=[@"http://douban.fm/j/mine/playlist? ...

  2. Swift Internal Parameter and External Parameter 外部参数和内部参数

    今天跟大神又学习了些关于IOS开发Swift语言的外部参数和内部参数 func doSomething(num1: Int, num2: Int) -> Int { return num1 + ...

  3. GC、LOH和Performance相关

    Performance Now that we have a basic model for how things are working, let's consider some things th ...

  4. Careercup | Chapter 7

    7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only ...

  5. redis pool config的配置参数

    .获取jedis实例时,实际上可能有两类错误.一类是pool.getReource(),得不到可用的jedis实例:另一类是jedis.set/get时出错也会抛出异常:为了实现区分,所以根据inst ...

  6. 开源top100

    1.SwitchyOmega 项目简介:SwitchyOmega 是 SwitchySharp 的新版本.这是一个 Chrome 浏览器用来切换不同代理的插件.SwitchyOmega 初次安装时会检 ...

  7. Yii2 验证码不显示

    siteController 中,要将captcha 列为任意用户可以访问

  8. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  9. Responsive布局技巧

    在Responsive布局中,可以毫无保留的丢弃: 第一, 尽量少用无关紧要的div: 第二,不要使用内联元素(inline): 第三,尽量少用JS或flash: 第四,丢弃没用的绝对定位和浮动样式: ...

  10. Jquery&JS简单选项卡

    结果,如图 为了明显,用绿色+红色 第一步:静态布局 先写html代码和css样式 要点一:  ul li都为块级元素,可以用height,但是里面字体不居中,我们用line-height,即达到居中 ...