From owner-freebsd-questions@FreeBSD.ORG Wed Feb 2 15:19:01 2005 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3A45C16A4CE for ; Wed, 2 Feb 2005 15:19:01 +0000 (GMT) Received: from ei.bzerk.org (ei.xs4all.nl [213.84.67.5]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0900943D1F for ; Wed, 2 Feb 2005 15:18:56 +0000 (GMT) (envelope-from mail25@bzerk.org) Received: from ei.bzerk.org (BOFH@localhost [127.0.0.1]) by ei.bzerk.org (8.13.1/8.13.1) with ESMTP id j12FN7cj062454 for ; Wed, 2 Feb 2005 16:23:07 +0100 (CET) (envelope-from mail25@bzerk.org) Received: (from bulk@localhost) by ei.bzerk.org (8.13.1/8.13.1/Submit) id j12FN7U3062453 for freebsd-questions@freebsd.org; Wed, 2 Feb 2005 16:23:07 +0100 (CET) (envelope-from mail25@bzerk.org) Date: Wed, 2 Feb 2005 16:23:07 +0100 From: Ruben de Groot To: FreeBSD Questions Message-ID: <20050202152307.GA62361@ei.bzerk.org> Mail-Followup-To: Ruben de Groot , FreeBSD Questions References: <20050201193507.GD71726@keyslapper.net> <200502011258.59704.reso3w83@verizon.net> <20050201213920.GE71726@keyslapper.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050201213920.GE71726@keyslapper.net> User-Agent: Mutt/1.4.2.1i X-Spam-Status: No, score=-1.7 required=5.0 tests=ALL_TRUSTED, FROM_ENDS_IN_NUMS,J_CHICKENPOX_64 autolearn=failed version=3.0.1 X-Spam-Checker-Version: SpamAssassin 3.0.1 (2004-10-22) on ei.bzerk.org Subject: Re: library call for directory path creation? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Feb 2005 15:19:01 -0000 On Tue, Feb 01, 2005 at 04:39:21PM -0500, Louis LeBlanc typed: > On 02/01/05 12:58 PM, Michael C. Shultz sat at the `puter and typed: > > On Tuesday 01 February 2005 11:35 am, Louis LeBlanc wrote: > > > I know there might be a better place for this question, but here > > > goes. > > > > > > I'm working on a utility that has to, among many other things, create > > > directory paths, often with a series of parent directories that may > > > not already exist. Solaris has mkdirp(3GEN) in the libgen library, > > > but I can't find a library call that will do this in FreeBSD. Kind > > > of like `mkdir -p` would. > > > > > > I know it would be pretty trivial to roll my own, and if I can't find > > > it I will. I'm just curious if anyone knows of an *existing* library > > > call that would do this. > > > > > > TIA > > > Lou > > > > Assuming your working in C what is wrong with: > > > > char command[] = "mkdir -p /path/to/whatever"; > > > > system( command ); > > Nothing, except that calling a system command from C when you can roll > your own method in about 18 lines of code is usually not ideal. > Particularly when speed is important. And yes, it is definitely > important - disk access can be an insurmountable bottleneck for high > volume systems if it is neglected at the implemenation stage. > > I only wanted a system lib call because I trust FreeBSDs implementation > to be faster than my quick throw together. > > I've already written it. It's not pretty, and probably not as fast as a > system lib would be (it has to make 1 system call per directory in the > path, rather than just one system call for the whole path). It is, > however, much faster than a call to system() would be. Actually, the mkdirp(3GEN) library routine in Solaris probably makes the same amount of system calls as your implementation: > uname -sr SunOS 5.9 > cat mkdirp.c #include #include #define path "/tmp/a/b/c/d" int main(void) { mkdirp(path,S_IRWXU); } > gcc -lgen -o mkdirp mkdirp.c > truss ./mkdirp |& tail -10 mkdir("/tmp/a/b/c/d", 0700) Err#2 ENOENT access("/tmp/a/b/c", 0) Err#2 ENOENT access("/tmp/a/b", 0) Err#2 ENOENT access("/tmp/a", 0) Err#2 ENOENT access("/tmp", 0) = 0 mkdir("/tmp/a", 0700) = 0 mkdir("/tmp/a/b", 0700) = 0 mkdir("/tmp/a/b/c", 0700) = 0 mkdir("/tmp/a/b/c/d", 0700) = 0 _exit(-13163152) cheers, Ruben