Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/filesystem/create directory"

From cppreference.com
(Remove CreateDirectoryExW note. First, on some OSes, that API isn't available, so we (MSVC++) never called that there. Second, it turns out that for a symlink CreateDirectoryExW has copy_symlink behavior, not create_directory behavior)
(Clarify return value, +filesystem hyperlinks)
 
(13 intermediate revisions by 5 users not shown)
Line 2: Line 2:
 
{{cpp/filesystem/navbar}}
 
{{cpp/filesystem/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | filesystem}}
+
{{dcl header|filesystem}}
{{dcl | num=1 | since=c++17 | 1=
+
{{dcl|num=1|since=c++17|1=
 
bool create_directory( const std::filesystem::path& p );
 
bool create_directory( const std::filesystem::path& p );
 +
}}
 +
{{dcl|num=2|since=c++17|1=
 
bool create_directory( const std::filesystem::path& p, std::error_code& ec ) noexcept;
 
bool create_directory( const std::filesystem::path& p, std::error_code& ec ) noexcept;
 
}}
 
}}
{{dcl | num=2 | since=c++17 | 1=
+
{{dcl|num=3|since=c++17|1=
 
bool create_directory( const std::filesystem::path& p,
 
bool create_directory( const std::filesystem::path& p,
 
                       const std::filesystem::path& existing_p );
 
                       const std::filesystem::path& existing_p );
 +
}}
 +
{{dcl|num=4|since=c++17|1=
 
bool create_directory( const std::filesystem::path& p,
 
bool create_directory( const std::filesystem::path& p,
 
                       const std::filesystem::path& existing_p,
 
                       const std::filesystem::path& existing_p,
 
                       std::error_code& ec ) noexcept;
 
                       std::error_code& ec ) noexcept;
 
}}
 
}}
{{dcl | num=3 | since=c++17 | 1=
+
{{dcl|num=5|since=c++17|1=
 
bool create_directories( const std::filesystem::path& p );
 
bool create_directories( const std::filesystem::path& p );
 +
}}
 +
{{dcl|num=6|since=c++17|1=
 
bool create_directories( const std::filesystem::path& p, std::error_code& ec );
 
bool create_directories( const std::filesystem::path& p, std::error_code& ec );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Creates the directory {{tt|p}} as if by POSIX [http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html mkdir()] with a second argument of {{c|static_cast<int>(std::filesystem::perms::all)}} (the parent directory must already exist). If {{tt|p}} already exists, the function does nothing (this condition is not treated as an error).  
+
@1,2@ Creates the directory {{c|p}} as if by POSIX [https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html {{tt|mkdir()}}] with a second argument of {{c|static_cast<int>(std::filesystem::perms::all)}} (the parent directory must already exist). If the function fails because {{c|p}} resolves to an existing directory, no error is reported. Otherwise on failure an error is reported.
@2@ Same as {{v|1}}, except that the attributes of the new directory are copied from {{tt|existing_p}} (which must be a directory that exists). It is OS-dependent which attributes are copied: on POSIX systems, the attributes are copied as if by
+
@3,4@ Same as {{v|1,2}}, except that the attributes of the new directory are copied from {{c|existing_p}} (which must be a directory that exists). It is OS-dependent which attributes are copied: on POSIX systems, the attributes are copied as if by
 
{{source|1=
 
{{source|1=
 
stat(existing_p.c_str(), &attributes_stat)
 
stat(existing_p.c_str(), &attributes_stat)
 
mkdir(p.c_str(), attributes_stat.st_mode)
 
mkdir(p.c_str(), attributes_stat.st_mode)
}}On Windows OS, no attributes of {{tt|existing_p}} are copied.
+
}}
@3@ Executes {{v|1}} for every element of {{tt|p}} that does not already exist. If {{tt|p}} already exists, the function does nothing (this condition is not treated as an error).
+
On Windows OS, no attributes of {{c|existing_p}} are copied.
 
+
@5,6@ Executes {{v|1,2}} for every element of {{c|p}} that does not already exist. If {{c|p}} already exists, the function does nothing (this condition is not treated as an error).
The non-throwing overloads return {{c|false}} if any error occurs.
+
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | p | the path to the new directory to create}}
+
{{par|p|the path to the new directory to create}}
{{par | existing_p | the path to a directory to copy the attributes from}}
+
{{par|existing_p|the path to a directory to copy the attributes from}}
{{par | ec | out-parameter for error reporting in the non-throwing overload }}
+
{{par|ec|out-parameter for error reporting in the non-throwing overload}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
{{c|true}} if a directory was created for the directory {{tt|p}} resolves to, {{c|false}} otherwise.
+
{{c|true}} if a directory was newly created for the directory {{c|p}} resolves to, {{c|false}} otherwise.
  
 
===Exceptions===
 
===Exceptions===
@1,3@ {{cpp/filesystem/error_handling|p}}
+
{{cpp/filesystem/error_handling|p|throw=1,5/2,6}}
@2@ {{cpp/filesystem/error_handling|p|existing_p}}
+
{{cpp/filesystem/error_handling|p|existing_p|throw=3/4|noexcept=no}}
  
 
===Notes===
 
===Notes===
The attribute-preserving overload {{v|2}} is implicitly invoked by {{ltt|cpp/filesystem/copy|copy()}} when recursively copying directories. Its equivalent in boost.filesystem is [http://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html#copy_directory copy_directory] (with argument order reversed)
+
The attribute-preserving overload {{v|3,4}} is implicitly invoked by {{ltf|cpp/filesystem/copy}} when recursively copying directories. Its equivalent in boost.filesystem is [https://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html#copy_directory {{tt|copy_directory}}] (with argument order reversed).
 
+
===Defect reports===
+
{{dr list begin}}
+
{{dr list item|wg=lwg|dr=2935|std=C++17|before=error if target already exists but isn't a directory|after=not error}}
+
{{dr list item|wg=lwg|dr=3014|std=C++17|before={{tt|error_code}} overload of {{tt|create_directories}} marked noexcept but can allocate memory|after=noexcept removed}}
+
{{dr list end}}
+
  
 
===Example===
 
===Example===
{{example|code=
+
{{example
#include <iostream>
+
|code=
#include <fstream>
+
#include <cassert>
 
#include <cstdlib>
 
#include <cstdlib>
 
#include <filesystem>
 
#include <filesystem>
namespace fs = std::filesystem;
+
 
+
 
int main()
 
int main()
 
{
 
{
     fs::create_directories("sandbox/1/2/a");
+
     std::filesystem::current_path(std::filesystem::temp_directory_path());
     fs::create_directory("sandbox/1/2/b");
+
 
     fs::permissions("sandbox/1/2/b", fs::perms::remove_perms {{!}} fs::perms::others_all);
+
    // Basic usage
     fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
+
    std::filesystem::create_directories("sandbox/1/2/a");
 +
     std::filesystem::create_directory("sandbox/1/2/b");
 +
      
 +
    // Directory already exists (false returned, no error)
 +
    assert(!std::filesystem::create_directory("sandbox/1/2/b"));
 +
   
 +
    // Permissions copying usage
 +
    std::filesystem::permissions(
 +
        "sandbox/1/2/b",
 +
        std::filesystem::perms::others_all,
 +
        std::filesystem::perm_options::remove
 +
    );
 +
     std::filesystem::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
 +
   
 
     std::system("ls -l sandbox/1/2");
 
     std::system("ls -l sandbox/1/2");
     fs::remove_all("sandbox");
+
     std::system("tree sandbox");
 +
    std::filesystem::remove_all("sandbox");
 
}
 
}
 
|p=true
 
|p=true
Line 75: Line 87:
 
drwxr-x--- 2 user group 4096 Apr 15 09:33 b
 
drwxr-x--- 2 user group 4096 Apr 15 09:33 b
 
drwxr-x--- 2 user group 4096 Apr 15 09:33 c
 
drwxr-x--- 2 user group 4096 Apr 15 09:33 c
 +
sandbox
 +
└── 1
 +
    └── 2
 +
        ├── a
 +
        ├── b
 +
        └── c
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=2935|std=C++17|before=error if target already exists but is not a directory|after=not error}}
 +
{{dr list item|wg=lwg|dr=3014|std=C++17|before={{tt|error_code}} overload of {{tt|create_directories}} marked noexcept but can allocate memory|after=noexcept removed}}
 +
{{dr list item|paper=P1164R1|std=C++17|before=creation failure caused by an existing non-directory file is not an error|after=made error}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/filesystem/dsc create_symlink}}
+
{{dsc inc|cpp/filesystem/dsc create_symlink}}
{{dsc inc | cpp/filesystem/dsc copy}}
+
{{dsc inc|cpp/filesystem/dsc copy}}
{{dsc inc | cpp/filesystem/dsc perms}}
+
{{dsc inc|cpp/filesystem/dsc perms}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|es|ja|ru|zh}}

Latest revision as of 05:04, 24 April 2024

 
 
 
Defined in header <filesystem>
bool create_directory( const std::filesystem::path& p );
(1) (since C++17)
bool create_directory( const std::filesystem::path& p, std::error_code& ec ) noexcept;
(2) (since C++17)
bool create_directory( const std::filesystem::path& p,
                       const std::filesystem::path& existing_p );
(3) (since C++17)
bool create_directory( const std::filesystem::path& p,

                       const std::filesystem::path& existing_p,

                       std::error_code& ec ) noexcept;
(4) (since C++17)
bool create_directories( const std::filesystem::path& p );
(5) (since C++17)
bool create_directories( const std::filesystem::path& p, std::error_code& ec );
(6) (since C++17)
1,2) Creates the directory p as if by POSIX mkdir() with a second argument of static_cast<int>(std::filesystem::perms::all) (the parent directory must already exist). If the function fails because p resolves to an existing directory, no error is reported. Otherwise on failure an error is reported.
3,4) Same as (1,2), except that the attributes of the new directory are copied from existing_p (which must be a directory that exists). It is OS-dependent which attributes are copied: on POSIX systems, the attributes are copied as if by
stat(existing_p.c_str(), &attributes_stat)
mkdir(p.c_str(), attributes_stat.st_mode)
On Windows OS, no attributes of existing_p are copied.
5,6) Executes (1,2) for every element of p that does not already exist. If p already exists, the function does nothing (this condition is not treated as an error).

Contents

[edit] Parameters

p - the path to the new directory to create
existing_p - the path to a directory to copy the attributes from
ec - out-parameter for error reporting in the non-throwing overload

[edit] Return value

true if a directory was newly created for the directory p resolves to, false otherwise.

[edit] Exceptions

Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.

1,5) Throws std::filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument and the OS error code as the error code argument.
2,6) Sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur.
3) Throws std::filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument, existing_p as the second path argument, and the OS error code as the error code argument.
4) Sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur.

[edit] Notes

The attribute-preserving overload (3,4) is implicitly invoked by copy() when recursively copying directories. Its equivalent in boost.filesystem is copy_directory (with argument order reversed).

[edit] Example

#include <cassert>
#include <cstdlib>
#include <filesystem>
 
int main()
{
    std::filesystem::current_path(std::filesystem::temp_directory_path());
 
    // Basic usage
    std::filesystem::create_directories("sandbox/1/2/a");
    std::filesystem::create_directory("sandbox/1/2/b");
 
    // Directory already exists (false returned, no error)
    assert(!std::filesystem::create_directory("sandbox/1/2/b"));
 
    // Permissions copying usage
    std::filesystem::permissions(
        "sandbox/1/2/b",
        std::filesystem::perms::others_all,
        std::filesystem::perm_options::remove
    );
    std::filesystem::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
 
    std::system("ls -l sandbox/1/2");
    std::system("tree sandbox");
    std::filesystem::remove_all("sandbox");
}

Possible output:

drwxr-xr-x 2 user group 4096 Apr 15 09:33 a
drwxr-x--- 2 user group 4096 Apr 15 09:33 b
drwxr-x--- 2 user group 4096 Apr 15 09:33 c
sandbox
└── 1
    └── 2
        ├── a
        ├── b
        └── c

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2935 C++17 error if target already exists but is not a directory not error
LWG 3014 C++17 error_code overload of create_directories marked noexcept but can allocate memory noexcept removed
P1164R1 C++17 creation failure caused by an existing non-directory file is not an error made error

[edit] See also

creates a symbolic link
(function) [edit]
(C++17)
copies files or directories
(function) [edit]
(C++17)
identifies file system permissions
(enum) [edit]