From: ······@heslin.eclipse.co.uk
Subject: How to get the parent directory of a pathname?
Date: 
Message-ID: <873bhxqfjn.fsf@heslin.eclipse.co.uk>
Given a directory name represented as a pathname or namestring, is there
a platform- and implementation-independent way to get the parent
directory of that directory?

If not, is there a way that mostly works on modern platforms?

Thanks,

-- 
Peter Heslin (http://www.dur.ac.uk/p.j.heslin)

From: Pascal Bourguignon
Subject: Re: How to get the parent directory of a pathname?
Date: 
Message-ID: <87hd6ddqy9.fsf@thalassa.informatimago.com>
······@heslin.eclipse.co.uk writes:

> Given a directory name represented as a pathname or namestring, is there
> a platform- and implementation-independent way to get the parent
> directory of that directory?
>
> If not, is there a way that mostly works on modern platforms?

(merge-pathnames (make-pathname :directory '(:relative :up) :defaults dirpath)
                 dirpath)

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: Rob Warnock
Subject: Re: How to get the parent directory of a pathname?
Date: 
Message-ID: <Q--dnYxar9Bpk5DZRVn-sw@speakeasy.net>
Pascal Bourguignon  <······@informatimago.com> wrote:
+---------------
| ······@heslin.eclipse.co.uk writes:
| > Given a directory name represented as a pathname or namestring, is there
| > a platform- and implementation-independent way to get the parent
| > directory of that directory?
| >
| > If not, is there a way that mostly works on modern platforms?
| 
| (merge-pathnames (make-pathname :directory '(:relative :up)
|                                 :defaults dirpath)
|                  dirpath)
+---------------

While technically correct, what he might get on some implementations
is, I suspect, not quite what he was hoping for, e.g., on CMUCL-19c:

    > (default-directory)

    #p"/usr/u/rpw3/src/cmd/misc/"
    > (let ((dirpath (default-directory)))
	(merge-pathnames (make-pathname :directory '(:relative :up)
					:defaults dirpath)
			 dirpath))

    #p"/usr/u/rpw3/src/cmd/misc/../"
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Pascal Bourguignon
Subject: Re: How to get the parent directory of a pathname?
Date: 
Message-ID: <87slpuvooz.fsf@thalassa.informatimago.com>
····@rpw3.org (Rob Warnock) writes:

> Pascal Bourguignon  <······@informatimago.com> wrote:
> +---------------
> | ······@heslin.eclipse.co.uk writes:
> | > Given a directory name represented as a pathname or namestring, is there
> | > a platform- and implementation-independent way to get the parent
> | > directory of that directory?
> | >
> | > If not, is there a way that mostly works on modern platforms?
> | 
> | (merge-pathnames (make-pathname :directory '(:relative :up)
> |                                 :defaults dirpath)
> |                  dirpath)
> +---------------
>
> While technically correct, what he might get on some implementations
> is, I suspect, not quite what he was hoping for, e.g., on CMUCL-19c:
>
>     > (default-directory)
>
>     #p"/usr/u/rpw3/src/cmd/misc/"
>     > (let ((dirpath (default-directory)))
> 	(merge-pathnames (make-pathname :directory '(:relative :up)
> 					:defaults dirpath)
> 			 dirpath))
>
>     #p"/usr/u/rpw3/src/cmd/misc/../"

Then you can use truename:

* (let ((dirpath (ext:default-directory)))
        (values dirpath
                (truename 
                 (merge-pathnames (make-pathname :directory '(:relative :up)
                                                 :defaults dirpath)
                                  dirpath))))

#p"/local/users/pjb/src/"
#p"/local/users/pjb/"


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

WARNING: This product warps space and time in its vicinity.
From: ··············@hotmail.com
Subject: Re: How to get the parent directory of a pathname?
Date: 
Message-ID: <1141747943.208272.206330@j33g2000cwa.googlegroups.com>
Pascal Bourguignon wrote:
> ······@heslin.eclipse.co.uk writes:
>
> > Given a directory name represented as a pathname or namestring, is there
> > a platform- and implementation-independent way to get the parent
> > directory of that directory?
> >
> > If not, is there a way that mostly works on modern platforms?
>
> (merge-pathnames (make-pathname :directory '(:relative :up) :defaults dirpath)
>                  dirpath)

I don't mean to be dense, but can you explain why :defaults dirpath and
the dirpath argument to merge-pathnames are not redundant?

i.e. can I not simply use

(merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)
From: Pascal Bourguignon
Subject: Re: How to get the parent directory of a pathname?
Date: 
Message-ID: <877j76rzi7.fsf@thalassa.informatimago.com>
···············@hotmail.com" <············@gmail.com> writes:

> Pascal Bourguignon wrote:
>> ······@heslin.eclipse.co.uk writes:
>>
>> > Given a directory name represented as a pathname or namestring, is there
>> > a platform- and implementation-independent way to get the parent
>> > directory of that directory?
>> >
>> > If not, is there a way that mostly works on modern platforms?
>>
>> (merge-pathnames (make-pathname :directory '(:relative :up) :defaults dirpath)
>>                  dirpath)
>
> I don't mean to be dense, but can you explain why :defaults dirpath and
> the dirpath argument to merge-pathnames are not redundant?
>
> i.e. can I not simply use
>
> (merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)

In general on unix, it'll be the same.
However, with other file systems, notably when there is a host or a device,
on some implementations, it can make a difference.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: Richard M Kreuter
Subject: Re: How to get the parent directory of a pathname?
Date: 
Message-ID: <87veuo4azo.fsf@progn.net>
Pascal Bourguignon <······@informatimago.com> writes:
> ···············@hotmail.com" <············@gmail.com> writes:

>> I don't mean to be dense, but can you explain why :defaults dirpath
>> and the dirpath argument to merge-pathnames are not redundant?
>>
>> i.e. can I not simply use
>>
>> (merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)
>
> In general on unix, it'll be the same.
> However, with other file systems, notably when there is a host or a
> device, on some implementations, it can make a difference.

Are such implementations conformant?  From the dictionary entry for
MERGE-PATHNAMES:

| If PATHNAME does not specify a host, device, directory, name, or
| type, each such component is copied from DEFAULT-PATHNAME.

I suppose one could read the condition as "if PATHNAME specifies none
of host, device, directory, name and type", but I thought it meant
"for each component of host, device, directory, name, and type, if
PATHNAME does not specify the component...", since it seems pretty
odd to have a pathname with just a version.

--
RmK
From: Pascal Bourguignon
Subject: Re: How to get the parent directory of a pathname?
Date: 
Message-ID: <877j74w215.fsf@thalassa.informatimago.com>
Richard M Kreuter <·······@progn.net> writes:

> Pascal Bourguignon <······@informatimago.com> writes:
>> ···············@hotmail.com" <············@gmail.com> writes:
>
>>> I don't mean to be dense, but can you explain why :defaults dirpath
>>> and the dirpath argument to merge-pathnames are not redundant?
>>>
>>> i.e. can I not simply use
>>>
>>> (merge-pathnames (make-pathname :directory '(:relative :up)) dirpath)
>>
>> In general on unix, it'll be the same.
>> However, with other file systems, notably when there is a host or a
>> device, on some implementations, it can make a difference.
>
> Are such implementations conformant?  From the dictionary entry for
> MERGE-PATHNAMES:
>
> | If PATHNAME does not specify a host, device, directory, name, or
> | type, each such component is copied from DEFAULT-PATHNAME.
>
> I suppose one could read the condition as "if PATHNAME specifies none
> of host, device, directory, name and type", but I thought it meant
> "for each component of host, device, directory, name, and type, if
> PATHNAME does not specify the component...", since it seems pretty
> odd to have a pathname with just a version.

Perhaps I've been overly cautious.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Remember, Information is not knowledge; Knowledge is not Wisdom;
Wisdom is not truth; Truth is not beauty; Beauty is not love;
Love is not music; Music is the best." -- Frank Zappa