xref: /third_party/openssl/util/mkdir-p.pl (revision e1051a39)
1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ci# On some systems, the -p option to mkdir (= also create any missing parent
10e1051a39Sopenharmony_ci# directories) is not available.
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_cimy $arg;
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_ciforeach $arg (@ARGV) {
15e1051a39Sopenharmony_ci  $arg =~ tr|\\|/|;
16e1051a39Sopenharmony_ci  &do_mkdir_p($arg);
17e1051a39Sopenharmony_ci}
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_cisub do_mkdir_p {
21e1051a39Sopenharmony_ci  local($dir) = @_;
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ci  $dir =~ s|/*\Z(?!\n)||s;
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ci  if (-d $dir) {
26e1051a39Sopenharmony_ci    return;
27e1051a39Sopenharmony_ci  }
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci  if ($dir =~ m|[^/]/|s) {
30e1051a39Sopenharmony_ci    local($parent) = $dir;
31e1051a39Sopenharmony_ci    $parent =~ s|[^/]*\Z(?!\n)||s;
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci    do_mkdir_p($parent);
34e1051a39Sopenharmony_ci  }
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci  unless (mkdir($dir, 0777)) {
37e1051a39Sopenharmony_ci    local($err) = $!;
38e1051a39Sopenharmony_ci    if (-d $dir) {
39e1051a39Sopenharmony_ci      # We raced against another instance doing the same thing.
40e1051a39Sopenharmony_ci      return;
41e1051a39Sopenharmony_ci    }
42e1051a39Sopenharmony_ci    die "Cannot create directory $dir: $err\n";
43e1051a39Sopenharmony_ci  }
44e1051a39Sopenharmony_ci  print "created directory `$dir'\n";
45e1051a39Sopenharmony_ci}
46