Skip to content

Latest commit

 

History

History
583 lines (473 loc) · 16.1 KB

unix.c

File metadata and controls

583 lines (473 loc) · 16.1 KB
 
Jul 7, 2001
Jul 7, 2001
1
2
3
/*
* Unix support routines for PhysicsFS.
*
Mar 11, 2007
Mar 11, 2007
4
* Please see the file LICENSE.txt in the source's root directory.
Jul 7, 2001
Jul 7, 2001
5
6
7
8
*
* This file written by Ryan C. Gordon.
*/
Mar 11, 2007
Mar 11, 2007
9
10
11
12
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_UNIX
Jun 29, 2002
Jun 29, 2002
13
Jul 7, 2001
Jul 7, 2001
14
15
#include <stdio.h>
#include <stdlib.h>
Jul 8, 2001
Jul 8, 2001
16
17
18
19
20
21
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/stat.h>
Jul 16, 2001
Jul 16, 2001
22
#include <sys/param.h>
Jul 8, 2001
Jul 8, 2001
23
24
25
#include <dirent.h>
#include <time.h>
#include <errno.h>
Jul 20, 2002
Jul 20, 2002
26
#include <sys/mount.h>
Mar 5, 2002
Mar 5, 2002
27
Mar 11, 2007
Mar 11, 2007
28
29
30
31
32
33
34
#ifdef PHYSFS_PLATFORM_MACOSX
# include <CoreFoundation/CoreFoundation.h>
# include <CoreServices/CoreServices.h>
# include <IOKit/IOKitLib.h>
# include <IOKit/storage/IOMedia.h>
# include <IOKit/storage/IOCDMedia.h>
# include <IOKit/storage/IODVDMedia.h>
May 24, 2003
May 24, 2003
35
36
#endif
Jul 25, 2002
Jul 25, 2002
37
38
39
40
#if (!defined PHYSFS_NO_PTHREADS_SUPPORT)
#include <pthread.h>
#endif
Jul 20, 2002
Jul 20, 2002
41
42
43
44
45
#ifdef PHYSFS_HAVE_SYS_UCRED_H
# ifdef PHYSFS_HAVE_MNTENT_H
# undef PHYSFS_HAVE_MNTENT_H /* don't do both... */
# endif
# include <sys/ucred.h>
Mar 5, 2002
Mar 5, 2002
46
#endif
Jul 7, 2001
Jul 7, 2001
47
Jul 20, 2002
Jul 20, 2002
48
49
50
#ifdef PHYSFS_HAVE_MNTENT_H
#include <mntent.h>
#endif
Apr 6, 2002
Apr 6, 2002
51
Jul 7, 2001
Jul 7, 2001
52
53
#include "physfs_internal.h"
Mar 14, 2005
Mar 14, 2005
54
55
56
57
/* Seems to get defined in some system header... */
#ifdef Free
#undef Free
#endif
Jul 7, 2001
Jul 7, 2001
58
59
60
const char *__PHYSFS_platformDirSeparator = "/";
Mar 5, 2002
Mar 5, 2002
61
Mar 24, 2002
Mar 24, 2002
62
63
64
65
66
67
68
69
70
71
72
73
int __PHYSFS_platformInit(void)
{
return(1); /* always succeed. */
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
return(1); /* always succeed. */
} /* __PHYSFS_platformDeinit */
Jul 25, 2002
Jul 25, 2002
74
75
76
#ifdef PHYSFS_NO_CDROM_SUPPORT
/* Stub version for platforms without CD-ROM support. */
Sep 29, 2004
Sep 29, 2004
77
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Jul 25, 2002
Jul 25, 2002
78
79
80
81
{
} /* __PHYSFS_platformDetectAvailableCDs */
Mar 11, 2007
Mar 11, 2007
82
#elif (defined PHYSFS_PLATFORM_MACOSX) /* "Big Nasty." */
May 24, 2003
May 24, 2003
83
84
85
86
87
88
89
90
91
/*
* Code based on sample from Apple Developer Connection:
* http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
*/
static int darwinIsWholeMedia(io_service_t service)
{
int retval = 0;
CFTypeRef wholeMedia;
Mar 24, 2002
Mar 24, 2002
92
May 24, 2003
May 24, 2003
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
if (!IOObjectConformsTo(service, kIOMediaClass))
return(0);
wholeMedia = IORegistryEntryCreateCFProperty(service,
CFSTR(kIOMediaWholeKey),
kCFAllocatorDefault, 0);
if (wholeMedia == NULL)
return(0);
retval = CFBooleanGetValue(wholeMedia);
CFRelease(wholeMedia);
return retval;
} /* darwinIsWholeMedia */
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort)
{
int retval = 0;
CFMutableDictionaryRef matchingDict;
kern_return_t rc;
io_iterator_t iter;
io_service_t service;
if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL)
return(0);
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
if ((rc != KERN_SUCCESS) || (!iter))
return(0);
service = IOIteratorNext(iter);
IOObjectRelease(iter);
if (!service)
return(0);
rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
if (!iter)
return(0);
if (rc != KERN_SUCCESS)
{
IOObjectRelease(iter);
return(0);
} /* if */
IOObjectRetain(service); /* add an extra object reference... */
do
{
if (darwinIsWholeMedia(service))
{
if ( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
(IOObjectConformsTo(service, kIODVDMediaClass)) )
{
retval = 1;
} /* if */
} /* if */
IOObjectRelease(service);
} while ((service = IOIteratorNext(iter)) && (!retval));
IOObjectRelease(iter);
IOObjectRelease(service);
return(retval);
} /* darwinIsMountedDisc */
Sep 29, 2004
Sep 29, 2004
163
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
May 24, 2003
May 24, 2003
164
165
166
167
168
169
170
171
{
const char *devPrefix = "/dev/";
int prefixLen = strlen(devPrefix);
mach_port_t masterPort = 0;
struct statfs *mntbufp;
int i, mounts;
if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS)
Mar 13, 2005
Mar 13, 2005
172
BAIL_MACRO(ERR_OS_ERROR, /*return void*/);
May 24, 2003
May 24, 2003
173
174
175
176
177
178
179
180
181
182
183
mounts = getmntinfo(&mntbufp, MNT_WAIT); /* NOT THREAD SAFE! */
for (i = 0; i < mounts; i++)
{
char *dev = mntbufp[i].f_mntfromname;
char *mnt = mntbufp[i].f_mntonname;
if (strncmp(dev, devPrefix, prefixLen) != 0) /* a virtual device? */
continue;
dev += prefixLen;
if (darwinIsMountedDisc(dev, masterPort))
Sep 29, 2004
Sep 29, 2004
184
cb(data, mnt);
May 24, 2003
May 24, 2003
185
186
187
188
} /* for */
} /* __PHYSFS_platformDetectAvailableCDs */
#elif (defined PHYSFS_HAVE_SYS_UCRED_H)
Mar 5, 2002
Mar 5, 2002
189
Sep 29, 2004
Sep 29, 2004
190
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Mar 5, 2002
Mar 5, 2002
191
{
Jul 25, 2002
Jul 25, 2002
192
int i;
Sep 29, 2004
Sep 29, 2004
193
194
struct statfs *mntbufp = NULL;
int mounts = getmntinfo(&mntbufp, MNT_WAIT);
Mar 5, 2002
Mar 5, 2002
195
Jul 25, 2002
Jul 25, 2002
196
197
for (i = 0; i < mounts; i++)
{
Mar 5, 2002
Mar 5, 2002
198
199
int add_it = 0;
Jul 25, 2002
Jul 25, 2002
200
if (strcmp(mntbufp[i].f_fstypename, "iso9660") == 0)
Mar 5, 2002
Mar 5, 2002
201
add_it = 1;
Jul 25, 2002
Jul 25, 2002
202
else if (strcmp( mntbufp[i].f_fstypename, "cd9660") == 0)
Apr 6, 2002
Apr 6, 2002
203
add_it = 1;
May 21, 2002
May 21, 2002
204
205
/* add other mount types here */
Mar 5, 2002
Mar 5, 2002
206
207
if (add_it)
Sep 29, 2004
Sep 29, 2004
208
cb(data, mntbufp[i].f_mntonname);
Jul 25, 2002
Jul 25, 2002
209
} /* for */
Mar 5, 2002
Mar 5, 2002
210
211
} /* __PHYSFS_platformDetectAvailableCDs */
May 24, 2003
May 24, 2003
212
#elif (defined PHYSFS_HAVE_MNTENT_H)
Mar 5, 2002
Mar 5, 2002
213
Sep 29, 2004
Sep 29, 2004
214
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Jul 7, 2001
Jul 7, 2001
215
{
Aug 7, 2001
Aug 7, 2001
216
217
218
219
FILE *mounts = NULL;
struct mntent *ent = NULL;
mounts = setmntent("/etc/mtab", "r");
Mar 13, 2005
Mar 13, 2005
220
BAIL_IF_MACRO(mounts == NULL, ERR_IO_ERROR, /*return void*/);
Aug 7, 2001
Aug 7, 2001
221
222
223
224
225
226
while ( (ent = getmntent(mounts)) != NULL )
{
int add_it = 0;
if (strcmp(ent->mnt_type, "iso9660") == 0)
add_it = 1;
May 21, 2002
May 21, 2002
227
228
/* add other mount types here */
Aug 7, 2001
Aug 7, 2001
229
230
if (add_it)
Sep 29, 2004
Sep 29, 2004
231
cb(data, ent->mnt_dir);
Aug 7, 2001
Aug 7, 2001
232
233
234
} /* while */
endmntent(mounts);
Jul 8, 2001
Jul 8, 2001
235
Mar 5, 2002
Mar 5, 2002
236
237
238
} /* __PHYSFS_platformDetectAvailableCDs */
#endif
Jul 7, 2001
Jul 7, 2001
239
240
May 24, 2002
May 24, 2002
241
242
/* this is in posix.c ... */
extern char *__PHYSFS_platformCopyEnvironmentVariable(const char *varname);
Jul 8, 2001
Jul 8, 2001
243
244
May 21, 2002
May 21, 2002
245
246
247
248
249
250
/*
* See where program (bin) resides in the $PATH specified by (envr).
* returns a copy of the first element in envr that contains it, or NULL
* if it doesn't exist or there were other problems. PHYSFS_SetError() is
* called if we have a problem.
*
Mar 14, 2005
Mar 14, 2005
251
* (envr) will be scribbled over, and you are expected to allocator.Free() the
May 21, 2002
May 21, 2002
252
253
254
* return value when you're done with it.
*/
static char *findBinaryInPath(const char *bin, char *envr)
Jul 8, 2001
Jul 8, 2001
255
{
May 21, 2002
May 21, 2002
256
257
258
size_t alloc_size = 0;
char *exe = NULL;
char *start = envr;
Jul 8, 2001
Jul 8, 2001
259
260
char *ptr;
May 21, 2002
May 21, 2002
261
262
BAIL_IF_MACRO(bin == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(envr == NULL, ERR_INVALID_ARGUMENT, NULL);
Jul 8, 2001
Jul 8, 2001
263
264
265
do
{
May 21, 2002
May 21, 2002
266
267
size_t size;
ptr = strchr(start, ':'); /* find next $PATH separator. */
Jul 8, 2001
Jul 8, 2001
268
269
270
if (ptr)
*ptr = '\0';
May 21, 2002
May 21, 2002
271
272
size = strlen(start) + strlen(bin) + 2;
if (size > alloc_size)
Jul 8, 2001
Jul 8, 2001
273
{
Mar 14, 2005
Mar 14, 2005
274
char *x = (char *) allocator.Realloc(exe, size);
May 21, 2002
May 21, 2002
275
276
277
if (x == NULL)
{
if (exe != NULL)
Mar 14, 2005
Mar 14, 2005
278
allocator.Free(exe);
May 21, 2002
May 21, 2002
279
280
281
282
283
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
alloc_size = size;
exe = x;
Jul 8, 2001
Jul 8, 2001
284
} /* if */
May 21, 2002
May 21, 2002
285
286
/* build full binary path... */
Jul 8, 2001
Jul 8, 2001
287
strcpy(exe, start);
Nov 30, 2002
Nov 30, 2002
288
if ((exe[0] == '\0') || (exe[strlen(exe) - 1] != '/'))
Aug 29, 2001
Aug 29, 2001
289
strcat(exe, "/");
May 21, 2002
May 21, 2002
290
291
292
strcat(exe, bin);
if (access(exe, X_OK) == 0) /* Exists as executable? We're done. */
Jul 8, 2001
Jul 8, 2001
293
{
May 21, 2002
May 21, 2002
294
295
296
strcpy(exe, start); /* i'm lazy. piss off. */
return(exe);
} /* if */
Jul 8, 2001
Jul 8, 2001
297
May 21, 2002
May 21, 2002
298
start = ptr + 1; /* start points to beginning of next element. */
Jul 8, 2001
Jul 8, 2001
299
300
} while (ptr != NULL);
May 21, 2002
May 21, 2002
301
if (exe != NULL)
Mar 14, 2005
Mar 14, 2005
302
allocator.Free(exe);
May 21, 2002
May 21, 2002
303
304
305
306
307
308
309
return(NULL); /* doesn't exist in path. */
} /* findBinaryInPath */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
Mar 19, 2007
Mar 19, 2007
310
311
312
313
314
315
316
317
const char *PROC_SELF_EXE = "/proc/self/exe";
char *retval = NULL;
char *envr = NULL;
struct stat stbuf;
/* fast path: default behaviour can handle this. */
if ( (argv0 != NULL) && (strchr(argv0, '/') != NULL) )
return(NULL); /* higher level will parse out real path from argv0. */
May 21, 2002
May 21, 2002
318
Mar 19, 2007
Mar 19, 2007
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Try to avoid using argv0 unless forced to. If there's a Linux-like
* /proc filesystem, you can get the full path to the current process from
* the /proc/self/exe symlink.
*/
if ((lstat(PROC_SELF_EXE, &stbuf) != -1) && (S_ISLNK(stbuf.st_mode)))
{
const size_t len = stbuf.st_size;
char *buf = (char *) allocator.Malloc(len+1);
if (buf != NULL) /* if NULL, maybe you'll get lucky later. */
{
if (readlink(PROC_SELF_EXE, buf, len) != len)
allocator.Free(buf);
else
{
buf[len] = '\0'; /* readlink doesn't null-terminate. */
retval = buf; /* we're good to go. */
} /* else */
} /* if */
} /* if */
May 21, 2002
May 21, 2002
339
Mar 19, 2007
Mar 19, 2007
340
341
342
343
344
345
346
347
if ((retval == NULL) && (argv0 != NULL))
{
/* If there's no dirsep on argv0, then look through $PATH for it. */
envr = __PHYSFS_platformCopyEnvironmentVariable("PATH");
BAIL_IF_MACRO(!envr, NULL, NULL);
retval = findBinaryInPath(argv0, envr);
allocator.Free(envr);
} /* if */
May 21, 2002
May 21, 2002
348
Jul 8, 2001
Jul 8, 2001
349
350
351
352
return(retval);
} /* __PHYSFS_platformCalcBaseDir */
Mar 14, 2007
Mar 14, 2007
353
#ifdef PHYSFS_PLATFORM_MACOSX
Jan 4, 2003
Jan 4, 2003
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* This function is only for OSX. The problem is that Apple's applications
* can actually be directory structures with the actual executable nested
* several levels down. PhysFS computes the base directory from the Unix
* executable, but this may not be the correct directory. Apple tries to
* hide everything from the user, so from Finder, the user never sees the
* Unix executable, and the directory package (bundle) is considered the
* "executable". This means that the correct base directory is at the
* level where the directory structure starts.
* A typical bundle seems to look like this:
* MyApp.app/ <-- top level...this is what the user sees in Finder
* Contents/
* MacOS/
* MyApp <-- the actual executable
*
* Since anything below the app folder is considered hidden, most
* application files need to be at the top level if you intend to
* write portable software. Thus if the application resides in:
* /Applications/MyProgram
* and the executable is the bundle MyApp.app,
* PhysFS computes the following as the base directory:
* /Applications/MyProgram/MyApp.app/Contents/MacOS/
* We need to strip off the MyApp.app/Contents/MacOS/
*
* However, there are corner cases. OSX applications can be traditional
* Unix executables without the bundle. Also, it is not entirely clear
* to me what kinds of permutations bundle structures can have.
*
* For now, this is a temporary hack until a better solution
* can be made. This function will try to find a "/Contents/MacOS"
* inside the path. If it succeeds, then the path will be truncated
* to correct the directory. If it is not found, the path will be
* left alone and will presume it is a traditional Unix execuatable.
* Most programs also include the .app extention in the top level
* folder, but it doesn't seem to be a requirement (Acrobat doesn't
* have it). MacOS looks like it can also be MacOSClassic.
* This function will test for MacOS and hope it captures any
* other permutations.
*/
static void stripAppleBundle(char *path)
{
char *sub_str = "/contents/macos";
char *found_ptr = NULL;
char *tempbuf = NULL;
Mar 14, 2005
Mar 14, 2005
398
size_t len = strlen(path) + 1;
Jan 4, 2003
Jan 4, 2003
399
400
int i;
May 24, 2003
May 24, 2003
401
/* !!! FIXME: Can we stack-allocate this? --ryan. */
Mar 14, 2005
Mar 14, 2005
402
403
404
405
tempbuf = (char *) allocator.Malloc(len);
if (!tempbuf) return;
memset(tempbuf, '\0', len);
Jan 4, 2003
Jan 4, 2003
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* Unlike other Unix filesystems, HFS is case insensitive
* It wouldn be nice to use strcasestr, but it doesn't seem
* to be available in the OSX gcc library right now.
* So we should make a lower case copy of the path to
* compare against
*/
for(i=0; i<strlen(path); i++)
{
/* convert to lower case */
tempbuf[i] = tolower(path[i]);
}
/* See if we can find "/contents/macos" in the path */
found_ptr = strstr(tempbuf, sub_str);
if(NULL == found_ptr)
{
/* It doesn't look like a bundle so we can keep the
* original path. Just return */
Mar 14, 2005
Mar 14, 2005
423
allocator.Free(tempbuf);
Jan 4, 2003
Jan 4, 2003
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
return;
}
/* We have a bundle, so let's backstep character by character
* to erase the extra parts of the path. Quit when we hit
* the preceding '/' character.
*/
for(i=strlen(path)-strlen(found_ptr)-1; i>=0; i--)
{
if('/' == path[i])
{
break;
}
}
/* Safety check */
if(i<1)
{
/* This probably shouldn't happen. */
path[0] = '\0';
}
else
{
/* Back up one more to remove trailing '/' and set the '\0' */
path[i] = '\0';
}
Mar 14, 2005
Mar 14, 2005
448
allocator.Free(tempbuf);
Jan 4, 2003
Jan 4, 2003
449
450
return;
}
Mar 14, 2007
Mar 14, 2007
451
#endif
Jan 4, 2003
Jan 4, 2003
452
453
Jul 16, 2001
Jul 16, 2001
454
455
456
457
458
459
460
char *__PHYSFS_platformRealPath(const char *path)
{
char resolved_path[MAXPATHLEN];
char *retval = NULL;
errno = 0;
BAIL_IF_MACRO(!realpath(path, resolved_path), strerror(errno), NULL);
Mar 14, 2005
Mar 14, 2005
461
retval = (char *) allocator.Malloc(strlen(resolved_path) + 1);
Jul 16, 2001
Jul 16, 2001
462
463
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, resolved_path);
Jan 4, 2003
Jan 4, 2003
464
Mar 19, 2007
Mar 19, 2007
465
/* !!! FIXME: this shouldn't be here. */
Mar 14, 2007
Mar 14, 2007
466
#ifdef PHYSFS_PLATFORM_MACOSX
Jan 4, 2003
Jan 4, 2003
467
stripAppleBundle(retval);
Mar 14, 2007
Mar 14, 2007
468
#endif
Jan 4, 2003
Jan 4, 2003
469
Jul 16, 2001
Jul 16, 2001
470
471
472
return(retval);
} /* __PHYSFS_platformRealPath */
Aug 23, 2001
Aug 23, 2001
473
Mar 20, 2007
Mar 20, 2007
474
475
476
477
478
479
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
{
return(0); /* just use malloc() and friends. */
} /* __PHYSFS_platformSetDefaultAllocator */
Jul 25, 2002
Jul 25, 2002
480
#if (defined PHYSFS_NO_PTHREADS_SUPPORT)
Jul 25, 2002
Jul 25, 2002
481
482
483
484
485
486
487
488
489
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) { return(0x0001); }
void *__PHYSFS_platformCreateMutex(void) { return((void *) 0x0001); }
void __PHYSFS_platformDestroyMutex(void *mutex) {}
int __PHYSFS_platformGrabMutex(void *mutex) { return(1); }
void __PHYSFS_platformReleaseMutex(void *mutex) {}
#else
Sep 20, 2005
Sep 20, 2005
490
491
492
493
494
495
496
typedef struct
{
pthread_mutex_t mutex;
pthread_t owner;
PHYSFS_uint32 count;
} PthreadMutex;
Aug 9, 2002
Aug 9, 2002
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* Just in case; this is a panic value. */
#if ((!defined SIZEOF_INT) || (SIZEOF_INT <= 0))
# define SIZEOF_INT 4
#endif
#if (SIZEOF_INT == 4)
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint32) (thr)) )
#elif (SIZEOF_INT == 2)
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint16) (thr)) )
#elif (SIZEOF_INT == 1)
# define PHTREAD_TO_UI64(thr) ( (PHYSFS_uint64) ((PHYSFS_uint8) (thr)) )
#else
# define PHTREAD_TO_UI64(thr) ((PHYSFS_uint64) (thr))
#endif
Jul 25, 2002
Jul 25, 2002
511
Jul 25, 2002
Jul 25, 2002
512
513
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
{
Jul 25, 2002
Jul 25, 2002
514
return(PHTREAD_TO_UI64(pthread_self()));
Jul 25, 2002
Jul 25, 2002
515
516
517
} /* __PHYSFS_platformGetThreadID */
Mar 30, 2002
Mar 30, 2002
518
519
520
void *__PHYSFS_platformCreateMutex(void)
{
int rc;
Sep 20, 2005
Sep 20, 2005
521
PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
Mar 30, 2002
Mar 30, 2002
522
BAIL_IF_MACRO(m == NULL, ERR_OUT_OF_MEMORY, NULL);
Sep 20, 2005
Sep 20, 2005
523
rc = pthread_mutex_init(&m->mutex, NULL);
Mar 30, 2002
Mar 30, 2002
524
525
if (rc != 0)
{
Mar 14, 2005
Mar 14, 2005
526
allocator.Free(m);
Mar 30, 2002
Mar 30, 2002
527
528
529
BAIL_MACRO(strerror(rc), NULL);
} /* if */
Sep 20, 2005
Sep 20, 2005
530
531
m->count = 0;
m->owner = (pthread_t) 0xDEADBEEF;
Mar 30, 2002
Mar 30, 2002
532
533
534
535
536
537
return((void *) m);
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
Sep 20, 2005
Sep 20, 2005
538
539
540
541
542
543
544
545
PthreadMutex *m = (PthreadMutex *) mutex;
/* Destroying a locked mutex is a bug, but we'll try to be helpful. */
if ((m->owner == pthread_self()) && (m->count > 0))
pthread_mutex_unlock(&m->mutex);
pthread_mutex_destroy(&m->mutex);
allocator.Free(m);
Mar 30, 2002
Mar 30, 2002
546
547
548
549
550
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
Sep 20, 2005
Sep 20, 2005
551
552
553
554
555
556
557
558
559
560
561
PthreadMutex *m = (PthreadMutex *) mutex;
pthread_t tid = pthread_self();
if (m->owner != tid)
{
if (pthread_mutex_lock(&m->mutex) != 0)
return(0);
m->owner = tid;
} /* if */
m->count++;
return(1);
Mar 30, 2002
Mar 30, 2002
562
563
564
565
566
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
Sep 20, 2005
Sep 20, 2005
567
568
569
570
571
572
573
574
575
PthreadMutex *m = (PthreadMutex *) mutex;
if (m->owner == pthread_self())
{
if (--m->count == 0)
{
m->owner = (pthread_t) 0xDEADBEEF;
pthread_mutex_unlock(&m->mutex);
} /* if */
} /* if */
Mar 30, 2002
Mar 30, 2002
576
577
} /* __PHYSFS_platformReleaseMutex */
Jul 25, 2002
Jul 25, 2002
578
579
#endif /* !PHYSFS_NO_PTHREADS_SUPPORT */
Mar 11, 2007
Mar 11, 2007
580
#endif /* PHYSFS_PLATFORM_UNIX */
May 24, 2002
May 24, 2002
581
Jul 7, 2001
Jul 7, 2001
582
/* end of unix.c ... */