Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding all mounted volumes



At 5:45 PM -0400 10/3/2002, Jason Boyd wrote:
How does one find all volumes/disks/whatever in a platform-agnostic way?

What's a volume/disk/whatever? I'm not kidding -- on any Unix (including Mac OS X, I believe), you can mount drives pretty much anywhere in the file system, including under other mounted drives. At work, for example, I'll often mount a Windows drive via Samba at ~/windows on FreeBSD. You'd have a tough time handling that by only searching in certain predefined directories.

At the same time, as far as your data is concerned the path to a file always corresponds to the file's true location with the exception of the one case in which your data is an HFS path and your operating system is Mac OS X. I'm not aware of any alternative path syntax for Windows or Linux/Unix paths on any system (with the exception of NTFS Unicode paths, but I don't think you can use those in Java in the first place).

Given that, rather than working really hard to come up with a customized solution that does everything, it makes more sense to just special-case Mac OS X. It's trivial to convert an HFS path to a Unix path on Mac OS X -- you just roundtrip the path through some CoreFoundation CFURL functions. I posted code that does this on the list back in June, but it appears that the online archives are horked for anything before July so you can't find it there. I'll correct that, then, by posting the code again below. This translates from POSIX paths to HFS; it should be trivial for you to figure out how to reverse the process.

Hope this helps,
Eric

----------
At 1:20 PM -0700 6/18/02, James Bucanek wrote:
Ronald Guest wrote on Tuesday, June 18, 2002:
I'm working on a Java app that has to write a preference file for a non-Java
Mac OS X application.

I can think of some not so clean ways to solve this but wanted to see if
there was a clearly "right", and hopefully simple, way to handle it.

I asked the very same question at the WWDC 2001 and was told that a Java accessable API had been written to translate beween Classic Mac OS and OS X pathnames. Immediately after the conference, I went looking for them but didn't find them. I vaugely thought they were being added to the MRJ utilities, but they don't see to be there or they aren't documented.

Does anyone know where this tool/API is, or know for a fact that it never saw the light of day?

The APIs were part of the sample code that Pete Steinauer and I demoed in our WWDC 2001 presentation. Apple hasn't shipped that code yet, though, and at this point I'd guess that it's unlikely to ship.

The following code snippet shows the "right" way to do this, without the pretty OO wrappers for Core Foundation objects that were part of the sample code. This converts a Java String to a CFStringRef, converts that CFStringRef to a CFURLRef with a POSIX path style, gets an HFS-style path as a CFStringRef from the CFURLRef, and finally converts the HFS-style CFStringRef to a Java String.

The error handling here is a bit ugly because I wanted to try to keep everything under 80 columns. Sorry about that. I'll also note that this doesn't use the Carbon lock because the calls used here are thread-safe.

To run it, compile it and run 'java Test /path/to/my/file'. Here's some sample output from my computer:
ithilien:~> javac Test.java
ithilien:~> java Test /Users/ejalbert/test.c
Ithilien:Users:ejalbert:test.c
ithilien:~>

Hope this helps,
Eric

-----
import com.apple.mrj.jdirect.*;

// Converts POSIX-style paths to HFS paths on Mac OS X.
public class Test {

private static final Linker linker = new Linker(Test.class);
private static final String JDirect_MacOSX =
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";

public static void main(String[] args) {
for(int i = 0; i < args.length; i++) {
char[] chars = new char[args[i].length()];
args[i].getChars(0, chars.length, chars, 0);
int posixString = CFStringCreateWithCharacters(0, chars,
chars.length);
if (posixString == 0) {
System.err.println("Invalid string: " + args[i]);
continue;
}
int posixURL = CFURLCreateWithFileSystemPath(0, posixString,
kCFURLPOSIXPathStyle,
false);
if (posixURL == 0) {
System.err.println("Can't convert string to URL!");
CFRelease(posixString);
continue;
}
int hfsString = CFURLCopyFileSystemPath(posixURL,
kCFURLHFSPathStyle);
if (hfsString == 0) {
System.err.println("Can't convert URL to HFS string!");
CFRelease(posixString);
CFRelease(posixURL);
continue;
}
int length = CFStringGetLength(hfsString);
chars = new char[length];
CFStringGetCharacters(hfsString, length, chars);
System.out.println(chars);

CFRelease(posixURL);
CFRelease(hfsString);
CFRelease(posixString);
}
System.exit(0);
}

private static native int CFStringCreateWithCharacters(int alloc,
char[] chars,
int numChars);
private static native int CFURLCreateWithFileSystemPath(int allocator,
int filePath,
int pathStyle,
boolean isDirectory);
private static native int CFURLCopyFileSystemPath(int anURL, int pathStyle);
private static native int CFStringGetLength(int string);
private static native void CFStringGetCharacters(int theString, long range,
char[] buffer);
private static native void CFRelease(int cfType);

private static final int kCFURLPOSIXPathStyle = 0;
private static final int kCFURLHFSPathStyle = 1;
}
-----


--
Eric Albert email@hidden
http://rescomp.stanford.edu/~ejalbert/
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.

References: 
 >Finding all mounted volumes (From: Jason Boyd <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.