5 years, 5 months ago.

SD code question

Hi, everyone thx for watching my question~ i have a question about SD card code just like below.

#include "mbed.h"
#include "SDFileSystem.h"
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
int main() {
    printf("Hello World!\n");   
    mkdir("/sd/mydir", 0777);
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
    if(fp == NULL) {
        error("Could not open file for write\n");
    fprintf(fp, "Hello fun SD Card World!");
    printf("Goodbye World!\n");

the question is about mkdir("/sd/mydir", 0777); i know mkdir is create a mydir project in SD card. But i dont know what is 0777 meaning? Can i change the number ? i do a test. i try to create two project. mkdir("/sd/mydir", 0777); mkdir("/sd/mydir1", 0777); And when i look the SD card . It really show two projects. Also i change 0777 to 0776. It also can create two projects. So i would like to know what is 0777 this term means? Thx very much!!!!

1 Answer

5 years, 5 months ago.

Short version:

Don't worry about it.

Long version:

The 0777 is setting the permissions for the directory.

In unix all files and directories have 3 possible options: read, write, and execute and three access levels: owner, group and public.

When setting the file access permissions read is given a value of 4, write is 2 and execute is 1. The access rights are then given as 3 numbers between 0 and 7 for the three access groups e.g. 750 = owner can do anything, people in the same group as the owner can read and execute the file but can't write to it, other people can't do anything.

In that notation 0777 means that anyone can do anything to the file/directory. 0700 would mean only the owner can do anything to it.

For a new directory the first number will always be 7, there is little point in having an empty directory you can't read, write or access. The other numbers would depend on the situation.

That leading 0 can also be anything from 0-7 and is used to set some extra behaviors that allow you to automatically change file ownerships under certain situations.

But that is all for unix based system, the filesystem on the SD card is some version of FAT which doesn't support multi user file access control so this is all a little redundant, everything other than the owners permissions will be ignored.

So why require a parameter which is mostly ignored and the bit that isn't ignored only has one sensible setting? Because mkdir is a standard c function, it is the same no matter what the underlying file system or operating system is and so needs to be able to cope with all eventualities.

Accepted Answer

Thx very much~~~

posted by jajn HA 05 Dec 2016