summaryrefslogtreecommitdiffstats
path: root/macosx/tkMacOSXUtil.c
blob: 1e4e856757887b566dd58615d454811665c0bddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <Carbon/Carbon.h>

#include "tkMacOSXUtil.h"

#define DIR_SEP_CHAR ':'

/*****************************************************************************/
pascal  OSErr   FSMakeFSSpecCompat(short vRefNum,
                                                                   long dirID, 
                                                                   ConstStr255Param fileName,
                                                                   FSSpec *spec)
{
        OSErr   result;
  
        {
                /* Let the file system create the FSSpec if it can since it does the job */
                /* much more efficiently than I can. */
                result = FSMakeFSSpec(vRefNum, dirID, fileName, spec);
                /* Fix a bug in Macintosh PC Exchange's MakeFSSpec code where 0 is */
                /* returned in the parID field when making an FSSpec to the volume's */
                /* root directory by passing a full pathname in MakeFSSpec's */
                /* fileName parameter. Fixed in Mac OS 8.1 */
                if ( (result == noErr) && (spec->parID == 0) )
                        spec->parID = fsRtParID;
        }
        return ( result );
}


/*****************************************************************************/
pascal  OSErr GetCatInfoNoName(short vRefNum,
                                                           long dirID,
                                                           ConstStr255Param name,
                                                           CInfoPBPtr pb)
{                                                        
        Str31 tempName; 
        OSErr error;            
                                        
        /* Protection against File Sharing problem */           
        if ( (name == NULL) || (name[0] == 0) )
        {
                tempName[0] = 0;
                pb->dirInfo.ioNamePtr = tempName;
                pb->dirInfo.ioFDirIndex = -1;   /* use ioDirID */
        }
        else
        {
                pb->dirInfo.ioNamePtr = (StringPtr)name;
                pb->dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
        }
        pb->dirInfo.ioVRefNum = vRefNum;
        pb->dirInfo.ioDrDirID = dirID;
        error = PBGetCatInfoSync(pb);
        pb->dirInfo.ioNamePtr = NULL;
        return ( error );
}
/*****************************************************************************/
pascal  OSErr   GetDirectoryID(short vRefNum,
                                                           long dirID,
                                                           ConstStr255Param name,
                                                           long *theDirID,
                                                           Boolean *isDirectory)
{                       
        CInfoPBRec pb;  
        OSErr error;    
        error = GetCatInfoNoName(vRefNum, dirID, name, &pb);
        if ( error == noErr )   
        {           
                *isDirectory = (pb.hFileInfo.ioFlAttrib & kioFlAttribDirMask) != 0;
                if ( *isDirectory )
                {               
                        *theDirID = pb.dirInfo.ioDrDirID;
                }       
                else            
                {                       
                        *theDirID = pb.hFileInfo.ioFlParID;
                }
        }

        return ( error );
}

/*****************************************************************************/
pascal  OSErr   FSpGetDirectoryID(const FSSpec *spec,
                                                                  long *theDirID,
                                                                  Boolean *isDirectory)
{       
        return ( GetDirectoryID(spec->vRefNum, spec->parID, spec->name,
                         theDirID, isDirectory) );
}

/*
 *----------------------------------------------------------------------
 *
 * FSpPathFromLocation --
 *
 *	This function obtains a full path name for a given macintosh
 *	FSSpec.  Unlike the More Files function FSpGetFullPath, this
 *	function will return a C string in the Handle.  It also will
 *	create paths for FSSpec that do not yet exist.
 *
 * Results:
 *	OSErr code.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

OSErr
FSpPathFromLocation(
    FSSpec *spec,		/* The location we want a path for. */
    int *length,		/* Length of the resulting path. */
    Handle *fullPath)		/* Handle to path. */
{
    OSErr err;
    FSSpec tempSpec;
    CInfoPBRec pb;
	
    *fullPath = NULL;
	
    /* 
     * Make a copy of the input FSSpec that can be modified.
     */
    BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
	
    if (tempSpec.parID == fsRtParID) {
	/* 
	 * The object is a volume.  Add a colon to make it a full 
	 * pathname.  Allocate a handle for it and we are done.
	 */
	tempSpec.name[0] += 2;
	tempSpec.name[tempSpec.name[0] - 1] = DIR_SEP_CHAR;
	tempSpec.name[tempSpec.name[0]] = '\0';
		
	err = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
    } else {
	/* 
	 * The object isn't a volume.  Is the object a file or a directory? 
	 */
	pb.dirInfo.ioNamePtr = tempSpec.name;
	pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
	pb.dirInfo.ioDrDirID = tempSpec.parID;
	pb.dirInfo.ioFDirIndex = 0;
	err = PBGetCatInfoSync(&pb);

	if ((err == noErr) || (err == fnfErr)) {
	    /* 
	     * If the file doesn't currently exist we start over.  If the
	     * directory exists everything will work just fine.  Otherwise we
	     * will just fail later.  If the object is a directory, append a
	     * colon so full pathname ends with colon, but only if the name is
	     * not empty.  NavServices returns FSSpec's with the parent ID set,
	     * but the name empty...
	     */
	    if (err == fnfErr) {
		BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
	    } else if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 ) {
	        if (tempSpec.name[0] > 0) {
		    tempSpec.name[0] += 1;
		    tempSpec.name[tempSpec.name[0]] = DIR_SEP_CHAR;
		}
	    }
			
	    /* 
	     * Create a new Handle for the object - make it a C string.
	     */
	    tempSpec.name[0] += 1;
	    tempSpec.name[tempSpec.name[0]] = '\0';
	    err = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
	    if (err == noErr) {
		/* 
		 * Get the ancestor directory names - loop until we have an 
		 * error or find the root directory.
		 */
		pb.dirInfo.ioNamePtr = tempSpec.name;
		pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
		pb.dirInfo.ioDrParID = tempSpec.parID;
		do {
		    pb.dirInfo.ioFDirIndex = -1;
		    pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
		    err = PBGetCatInfoSync(&pb);
		    if (err == noErr) {
			/* 
			 * Append colon to directory name and add 
			 * directory name to beginning of fullPath.
			 */
			++tempSpec.name[0];
			tempSpec.name[tempSpec.name[0]] = DIR_SEP_CHAR;
						
			(void) Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1],
				tempSpec.name[0]);
                        fprintf(stderr,"mem\n");
			err = MemError();
		    }
		} while ( (err == noErr) &&
			(pb.dirInfo.ioDrDirID != fsRtDirID) );
	    }
	}
    }
    
    /*
     * On error Dispose the handle, set it to NULL & return the err.
     * Otherwise, set the length & return.
     */
    if (err == noErr) {
	*length = GetHandleSize(*fullPath) - 1;
    } else {
	if ( *fullPath != NULL ) {
	    DisposeHandle(*fullPath);
	}
	*fullPath = NULL;
	*length = 0;
    }

    return err;
}

/*
 *----------------------------------------------------------------------
 *
 * FSpLocationFromPath --
 *
 *	This function obtains an FSSpec for a given macintosh path.
 *	Unlike the More Files function FSpLocationFromFullPath, this
 *	function will also accept partial paths and resolve any aliases
 *	along the path.  
 *
 * Results:
 *	OSErr code.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

OSErr
FSpLocationFromPath(
    int length,			/* Length of path. */
    const char *path,		/* The path to convert. */
    FSSpecPtr fileSpecPtr)	/* On return the spec for the path. */
{
    Str255 fileName;
    OSErr err;
    short vRefNum;
    long dirID;
    int pos, cur;
    Boolean isDirectory;
    Boolean wasAlias;

    /*
     * Check to see if this is a full path.  If partial
     * we assume that path starts with the current working
     * directory.  (Ie. volume & dir = 0)
     */
    vRefNum = 0;
    dirID = 0;
    cur = 0;
    if (length == 0) {
        return fnfErr;
    }
    if (path[cur] == DIR_SEP_CHAR) {
	cur++;
	if (cur >= length) {
	    /*
	     * If path = ":", just return current directory.
	     */
	    FSMakeFSSpecCompat(0, 0, NULL, fileSpecPtr);
	    return noErr;
	}
    } else {
	while (path[cur] != DIR_SEP_CHAR && cur < length) {
	    cur++;
	}
	if (cur > 255) {
	    return bdNamErr;
	}
	if (cur < length) {
	    /*
	     * This is a full path
	     */
	    cur++;
	    strncpy((char *) fileName + 1, path, cur);
	    fileName[0] = cur;
	    err = FSMakeFSSpecCompat(0, 0, fileName, fileSpecPtr);
	    if (err != noErr) return err;
	    FSpGetDirectoryID(fileSpecPtr, &dirID, &isDirectory);
	    vRefNum = fileSpecPtr->vRefNum;
	} else {
	    cur = 0;
	}
    }
    
    isDirectory = 1;
    while (cur < length) {
	if (!isDirectory) {
	    return dirNFErr;
	}
	pos = cur;
	while (path[pos] != DIR_SEP_CHAR && pos < length) {
	    pos++;
	}
	if (pos == cur) {
	    /* Move up one dir */
	    /* cur++; */
            fileName[1] = DIR_SEP_CHAR;
            fileName[2] = DIR_SEP_CHAR;
	    fileName[0] = 2;
	} else if (pos - cur > 255) {
	    return bdNamErr;
	} else {
	    strncpy((char *) fileName + 1, &path[cur], pos - cur);
	    fileName[0] = pos - cur;
	}
	err = FSMakeFSSpecCompat(vRefNum, dirID, fileName, fileSpecPtr);
	if (err != noErr) return err;
	err = ResolveAliasFile(fileSpecPtr, true, &isDirectory, &wasAlias);
	if (err != noErr) return err;
	FSpGetDirectoryID(fileSpecPtr, &dirID, &isDirectory);
	vRefNum = fileSpecPtr->vRefNum;
	cur = pos;
	if (path[cur] == DIR_SEP_CHAR) {
	    cur++;
	}
    }
    
    return noErr;
}