Create new directory

This code example demonstrates how to create a directory with specific permissions using the mkdir function. The permissions are specified using symbolic constants provided by the system headers. The program checks if the directory creation was successful and provides appropriate feedback.

Permission Constants

In Unix-like operating systems, file and directory permissions are represented by specific constants that are defined in system headers. These permissions control the ability of users to read, write, and execute files or directories. Here is a detailed explanation of these constants:

User Permissions

  • Read (S_IRUSR): Allows the user to read the file.
  • Write (S_IWUSR): Allows the user to write to the file.
  • Execute (S_IXUSR): Allows the user to execute the file.

Group Permissions

  • Read (S_IRGRP): Allows members of the file’s group to read the file.
  • Write (S_IWGRP): Allows members of the file’s group to write to the file.
  • Execute (S_IXGRP): Allows members of the file’s group to execute the file.

Others Permissions

  • Read (S_IROTH): Allows others to read the file.
  • Write (S_IWOTH): Allows others to write to the file.
  • Execute (S_IXOTH): Allows others to execute the file.

Composite Permission Constants

  • ACCESSPERMS: Equivalent to 0777 (rwxrwxrwx). Grants read, write, and execute permissions to user, group, and others.
  • DEFFILEMODE: Equivalent to 0666 (rw-rw-rw-). Grants read and write permissions to user, group, and others, but no execute permissions.

Code Example

Here is the detailed C++ code with explanations:

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;

int main() {
    // Define the directory path to be created
    char* dir = "/path/directory-name";
    
    // Create the directory with read, write, and execute permissions for user, group, and others
    int created = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
    // The above line is equivalent to: int created = mkdir(dir, 0777);
    
    // Check if the directory was created successfully
    if (!created) {
        // If the return value is 0, the directory was created successfully
        printf("Directory created successfully\n");
    } else {
        // If the return value is not 0, directory creation failed
        printf("Directory creation failed\n");
        exit(1);
    }

    return 0;
}

Code Explanation

Main Function

int main() {
    // Define the directory path to be created
    char* dir = "/path/directory-name";
    
    // Create the directory with read, write, and execute permissions for user, group, and others
    int created = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
    // The above line is equivalent to: int created = mkdir(dir, 0777);
  • int main() {: The main function where the program execution begins.
  • char* dir = "/path/directory-name";: Defines a character pointer dir to hold the path of the directory to be created.
  • int created = mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);: Calls the mkdir function to create a directory with specified permissions. S_IRWXU, S_IRWXG, and S_IRWXO are constants that grant read, write, and execute permissions to the user, group, and others, respectively.

Checking Directory Creation

    // Check if the directory was created successfully
    if (!created) {
        // If the return value is 0, the directory was created successfully
        printf("Directory created successfully\n");
    } else {
        // If the return value is not 0, directory creation failed
        printf("Directory creation failed\n");
        exit(1);
    }

    return 0;
}
  • if (!created) {: Checks if the mkdir function returned 0, indicating success.
  • printf("Directory created successfully\n");: Prints a success message if the directory was created.
  • } else {: Executes if mkdir did not return 0, indicating failure.
  • printf("Directory creation failed\n");: Prints a failure message.
  • exit(1);: Exits the program with a non-zero status indicating an error.

Output

When the program is executed, the output will be one of the following based on the success or failure of the directory creation:

Success:

Directory created successfully

Failure:

Directory creation failed