Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.perl.misc > #8759

Re: the fastest way to create a directory

From Rainer Weikusat <rweikusat@mssgmbh.com>
Newsgroups comp.lang.perl.misc
Subject Re: the fastest way to create a directory
Date 2013-07-19 12:09 +0100
Message-ID <87bo5yst5f.fsf@sapphire.mobileactivedefense.com> (permalink)
References (3 earlier) <87mwplo6sj.fsf@sapphire.mobileactivedefense.com> <ks71fk$2rgk$1@news.ntua.gr> <87oba0yiu5.fsf@sapphire.mobileactivedefense.com> <ks9l5b$1jcm$1@news.ntua.gr> <87fvva2882.fsf@sapphire.mobileactivedefense.com>

Show all headers | View raw


Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> "George Mpouras"
> <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam>
> writes:
>> Believe it or the following makes 18 uneccassery disk accesses
>
> [...]
>
>> use Errno qw(EEXIST ENOTDIR);
>> my $access=0;
>> mkdir_p("/opt/d1/d2/d3/d4/d5") or die("$!");
>> mkdir_p("/opt/d1/d2/d3/d4/d5") or die("$!");
>> mkdir_p("/opt/d1/d2/d3/d4/d5") or die("$!");
>> print "disk touches : $access\n";
>
> If it does, you're kernel is seriously disabled. Leaving this aside,
> that's the expected result when you call it uselessly three times in a
> row with an argument like that.

Other branch of the tree: No matter if the code was written to work
well if most directories already exist (yours) or most directories
don't exist (mine), the stat isn't needed because mkdir already checks
for that. The only downside of that is that the longest pathname needs
special-case treatment because EEXIST can refer to any kind of
filesystem object.

Since this is sort-of a cute little problem, here's another iterative
one, this time biased in the other direction:

use Errno qw(EEXIST ENOENT ENOTDIR);

sub mkdir_p
{
    my ($prefix, $suffix, @tbc);

    $prefix = $_[0];
    mkdir($prefix) and return 1;
    unless ($! == ENOENT) {
	if ($! == EEXIST) {
	    return 1 if -d($prefix);
	    $! = ENOTDIR;
	}
	
	return;
    }

    while (($prefix, $suffix) = $prefix =~ /(.*?)([^\/]+\/*)$/) {
	push(@tbc, $suffix);

	last if mkdir($prefix) or $! == EEXIST;
	return unless $! == ENOENT;
    }

    $prefix .= pop(@tbc), mkdir($prefix) or return
	while @tbc;
    
    return 1;
}

mkdir_p($ARGV[0]) or die($!);

Back to comp.lang.perl.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

the fastest way to create a directory George Mpouras <nospam.gravitalsun.noadsplease@hotmail.noads.com> - 2013-07-15 14:09 +0300
  Re: the fastest way to create a directory Henry Law <news@lawshouse.org> - 2013-07-15 15:44 +0100
    Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-15 18:45 +0300
      Re: the fastest way to create a directory Charlton Wilbur <cwilbur@chromatico.net> - 2013-07-15 15:36 -0400
        Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-16 00:07 +0300
      Re: the fastest way to create a directory "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-15 23:55 +0200
  Re: the fastest way to create a directory tmcd@panix.com (Tim McDaniel) - 2013-07-15 20:27 +0000
    Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-16 00:04 +0300
      Re: the fastest way to create a directory "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-16 00:02 +0200
        Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-18 00:16 +0300
      Re: the fastest way to create a directory tmcd@panix.com (Tim McDaniel) - 2013-07-15 22:16 +0000
      Re: the fastest way to create a directory Charlton Wilbur <cwilbur@chromatico.net> - 2013-07-18 15:49 -0400
        Re: the fastest way to create a directory Henry Law <news@lawshouse.org> - 2013-07-18 21:47 +0100
          Re: the fastest way to create a directory "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-19 15:25 +0200
            Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-19 14:51 +0100
            Re: the fastest way to create a directory Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> - 2013-07-19 08:39 -0700
          Re: the fastest way to create a directory Charlton Wilbur <cwilbur@chromatico.net> - 2013-07-21 23:19 -0400
            Re: the fastest way to create a directory Henry Law <news@lawshouse.org> - 2013-07-22 07:10 +0100
              Re: the fastest way to create a directory Charlton Wilbur <cwilbur@chromatico.net> - 2013-07-22 17:31 -0400
        Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-19 16:40 +0100
  Re: the fastest way to create a directory Ben Morrow <ben@morrow.me.uk> - 2013-07-15 23:40 +0100
    Re: the fastest way to create a directory "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-16 23:44 +0200
      Re: the fastest way to create a directory George Mpouras <nospam.gravitalsun.noadsplease@hotmail.noads.com> - 2013-07-17 11:09 +0300
        Re: the fastest way to create a directory "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-17 10:24 +0200
      Re: the fastest way to create a directory George Mpouras <nospam.gravitalsun.noadsplease@hotmail.noads.com> - 2013-07-17 11:10 +0300
      Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-17 10:49 +0100
        Re: the fastest way to create a directory "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-17 22:37 +0200
          Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-17 22:22 +0100
          Re: the fastest way to create a directory Ben Morrow <ben@morrow.me.uk> - 2013-07-19 16:21 +0100
            Re: the fastest way to create a directory "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-19 22:11 +0200
        Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-18 00:14 +0300
          Re: the fastest way to create a directory tmcd@panix.com (Tim McDaniel) - 2013-07-17 21:21 +0000
          Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-17 22:31 +0100
            Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-19 00:02 +0300
              Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-19 10:46 +0100
                Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-19 10:51 +0100
                Re: the fastest way to create a directory Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-19 12:09 +0100
          Re: the fastest way to create a directory Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us> - 2013-07-17 22:37 -0700
            Re: the fastest way to create a directory Justin C <justin.1303@purestblue.com> - 2013-07-18 09:15 +0100
  Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-19 01:01 +0300
    Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-19 01:07 +0300
  Re: the fastest way to create a directory "George Mpouras" <nospam.gravitalsun.antispam@spamno.hotmail.anispam.com.nospam> - 2013-07-23 09:33 +0300

csiph-web