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