From 279dfdc141bb3fc23c8fe25ea133c021b3a586ac Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Tue, 31 Dec 2002 09:21:12 +0000 Subject: [PATCH] Merge some Mac-specific changes from meeroh's branch: * profile.hin: Don't test MACINTOSH any more. On Mac OS X, include TargetConditionals.h, set some pragmas, define COPY_RESOURCE_FORK, and error out if TARGET_RT_MAC_CFM is defined; don't set the old m68k CFM pragmas. Always use PROFILE_USES_PATHS code, don't test or define it. (FSp_profile_init, FSp_profile_init_path): Declare, on Mac OS X. * prof_int.h (NO_SYS_TYPES_H, NO_SYS_STAT_H) [macintosh]: Don't define these. * prof_file.c: Always inclued sys/types.h and sys/stat.h. (GetMacOSTempFilespec): Deleted. (profile_flush_file_data) [COPY_RESOURCE_FORK]: Copy Mac resource fork from old file to new before renaming. (rw_access, profile_update_file_data, profile_flush_file_data, profile_free_file_data): Assume PROFILE_USES_PATHS, don't test. * prof_init.c (profile_ser_size, profile_ser_externalize, profile_ser_internalize): Likewise. (FSp_profile_init, FSp_profile_init_path): Define, on MacOS X. * profile.exp: Add FSp_* functions. * prof_file.c (r_access): New function. (profile_open_file): Use it. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@15073 dc483132-0cff-0310-8789-dd5450dbe970 --- src/util/profile/ChangeLog | 24 ++++++++ src/util/profile/prof_file.c | 105 ++++++++++++++--------------------- src/util/profile/prof_init.c | 88 +++++++++++++++++++++++------ src/util/profile/prof_int.h | 9 --- src/util/profile/prof_tree.c | 1 + src/util/profile/profile.exp | 4 ++ src/util/profile/profile.hin | 60 +++++++++++--------- 7 files changed, 176 insertions(+), 115 deletions(-) diff --git a/src/util/profile/ChangeLog b/src/util/profile/ChangeLog index d641fc5c0..3b72fdb46 100644 --- a/src/util/profile/ChangeLog +++ b/src/util/profile/ChangeLog @@ -1,3 +1,27 @@ +2002-12-31 Ken Raeburn + + * prof_file.c (r_access): New function. + (profile_open_file): Use it. + + * profile.hin: Don't test MACINTOSH any more. On Mac OS X, + include TargetConditionals.h, set some pragmas, define + COPY_RESOURCE_FORK, and error out if TARGET_RT_MAC_CFM is + defined; don't set the old m68k CFM pragmas. Always use + PROFILE_USES_PATHS code, don't test or define it. + (FSp_profile_init, FSp_profile_init_path): Declare, on Mac OS X. + * prof_int.h (NO_SYS_TYPES_H, NO_SYS_STAT_H) [macintosh]: Don't + define these. + * prof_file.c: Always inclued sys/types.h and sys/stat.h. + (GetMacOSTempFilespec): Deleted. + (profile_flush_file_data) [COPY_RESOURCE_FORK]: Copy Mac resource + fork from old file to new before renaming. + (rw_access, profile_update_file_data, profile_flush_file_data, + profile_free_file_data): Assume PROFILE_USES_PATHS, don't test. + * prof_init.c (profile_ser_size, profile_ser_externalize, + profile_ser_internalize): Likewise. + (FSp_profile_init, FSp_profile_init_path): Define, on MacOS X. + * profile.exp: Add FSp_* functions. + 2002-12-23 Ezra Peisach * prof_file.c: Cast return from prof_mutex_lock and diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c index d2022da35..749a7550d 100644 --- a/src/util/profile/prof_file.c +++ b/src/util/profile/prof_file.c @@ -13,12 +13,8 @@ #include "prof_int.h" -#ifndef NO_SYS_TYPES_H #include -#endif -#ifndef NO_SYS_STAT_H #include -#endif #include @@ -34,14 +30,6 @@ struct global_shared_profile_data krb5int_profile_shared_data = { }; #endif -#ifndef PROFILE_USES_PATHS -#include - -static OSErr GetMacOSTempFilespec ( - const FSSpec* inFilespec, - FSSpec* outFilespec); -#endif - static void profile_free_file_data(prf_data_t); static int rw_access(filespec) @@ -60,11 +48,32 @@ static int rw_access(filespec) */ FILE *f; -#ifdef PROFILE_USES_PATHS f = fopen(filespec, "r+"); -#else - f = FSp_fopen(&filespec, "r+"); + if (f) { + fclose(f); + return 1; + } + return 0; #endif +} + +static int r_access(filespec) + profile_filespec_t filespec; +{ +#ifdef HAVE_ACCESS + if (access(filespec, R_OK) == 0) + return 1; + else + return 0; +#else + /* + * We're on a substandard OS that doesn't support access. So + * we kludge a test using stdio routines, and hope fopen + * checks the r/w permissions. + */ + FILE *f; + + f = fopen(filespec, "r"); if (f) { fclose(f); return 1; @@ -94,7 +103,7 @@ errcode_t profile_open_file(filespec, ret_prof) for (data = g_shared_trees; data; data = data->next) { if (!strcmp(data->filespec, filespec) /* Check that current uid has read access. */ - && access(data->filespec, R_OK) == 0) + && r_access(data->filespec) == 0) break; } if (data) { @@ -187,11 +196,7 @@ errcode_t profile_update_file_data(prf_data_t data) return 0; #endif errno = 0; -#ifdef PROFILE_USES_PATHS f = fopen(data->filespec, "r"); -#else - f = FSp_fopen (&data->filespec, "r"); -#endif if (f == NULL) { retval = errno; if (retval == 0) @@ -212,24 +217,6 @@ errcode_t profile_update_file_data(prf_data_t data) return 0; } -#ifndef PROFILE_USES_PATHS -OSErr GetMacOSTempFilespec ( - const FSSpec* inFileSpec, - FSSpec* outFileSpec) -{ - OSErr err; - - err = FindFolder (inFileSpec -> vRefNum, kTemporaryFolderType, - kCreateFolder, &(outFileSpec -> vRefNum), &(outFileSpec -> parID)); - if (err != noErr) - return err; - - BlockMoveData (&(inFileSpec -> name), &(outFileSpec -> name), StrLength (inFileSpec -> name) + 1); - return noErr; -} -#endif - - errcode_t profile_flush_file_data(data) prf_data_t data; { @@ -246,7 +233,6 @@ errcode_t profile_flush_file_data(data) retval = ENOMEM; -#ifdef PROFILE_USES_PATHS new_file = old_file = 0; new_file = malloc(strlen(data->filespec) + 5); if (!new_file) @@ -261,13 +247,6 @@ errcode_t profile_flush_file_data(data) errno = 0; f = fopen(new_file, "w"); -#else - /* On MacOS, we do this by writing to a new file and then atomically - swapping the files with a file system call */ - GetMacOSTempFilespec (&data->filespec, &new_file); - f = FSp_fopen (&new_file, "w"); -#endif - if (!f) { retval = errno; if (retval == 0) @@ -281,7 +260,24 @@ errcode_t profile_flush_file_data(data) goto errout; } -#ifdef PROFILE_USES_PATHS +#ifdef COPY_RESOURCE_FORK + { + FSSpec from; + FSSpec to; + OSErr err = FSpLocationFromFullPOSIXPath (data -> filespec, &from); + if (err == noErr) { + err = FSpLocationFromFullPOSIXPath (new_file, &to); + } + if (err == noErr) { + err = FSpResourceForkCopy (&from, &to); + } + if (err != noErr) { + retval = ENOENT; + goto end; + } + } +#endif + unlink(old_file); if (rename(data->filespec, old_file)) { retval = errno; @@ -292,17 +288,6 @@ errcode_t profile_flush_file_data(data) rename(old_file, data->filespec); /* back out... */ goto errout; } -#else - { - OSErr err = FSpExchangeFiles (&data->filespec, &new_file); - if (err != noErr) { - retval = ENFILE; - goto errout; - } - FSpDelete (&new_file); - } -#endif - data->flags = 0; if (rw_access(data->filespec)) @@ -310,12 +295,10 @@ errcode_t profile_flush_file_data(data) retval = 0; errout: -#ifdef PROFILE_USES_PATHS if (new_file) free(new_file); if (old_file) free(old_file); -#endif return retval; } @@ -364,10 +347,8 @@ static void profile_free_file_data(data) } } #endif -#ifdef PROFILE_USES_PATHS if (data->filespec) free(data->filespec); -#endif if (data->root) profile_free_node(data->root); if (data->comment) diff --git a/src/util/profile/prof_init.c b/src/util/profile/prof_init.c index 46ebf2dbd..af7c6a3ec 100644 --- a/src/util/profile/prof_init.c +++ b/src/util/profile/prof_init.c @@ -10,6 +10,10 @@ #endif #include +#if TARGET_OS_MAC +#include +#endif + #include "prof_int.h" /* Find a 4-byte integer type */ @@ -125,6 +129,72 @@ profile_init_path(filepath, ret_profile) return retval; } +#if TARGET_OS_MAC +KRB5_DLLIMP errcode_t KRB5_CALLCONV +FSp_profile_init(files, ret_profile) + const FSSpec* files; + profile_t *ret_profile; +{ + UInt32 fileCount = 0; + const FSSpec* nextSpec; + char** pathArray = NULL; + UInt32 i; + errcode_t retval = 0; + + for (nextSpec = files; ; nextSpec++) { + if ((nextSpec -> vRefNum == 0) && + (nextSpec -> parID == 0) && + (StrLength (nextSpec -> name) == 0)) + break; + fileCount++; + } + + pathArray = malloc ((fileCount + 1) * sizeof (char*)); + if (pathArray == NULL) { + retval = ENOMEM; + } + + if (retval == 0) { + for (i = 0; i < fileCount + 1; i++) { + pathArray [i] = NULL; + } + + for (i = 0; i < fileCount; i++) { + OSErr err = FSpGetFullPOSIXPath (&files [i], &pathArray [i]); + if (err == memFullErr) { + retval = ENOMEM; + break; + } else if (err != noErr) { + retval = ENOENT; + break; + } + } + } + + if (retval == 0) { + retval = profile_init (pathArray, ret_profile); + } + + if (pathArray != NULL) { + for (i = 0; i < fileCount; i++) { + if (pathArray [i] != 0) + free (pathArray [i]); + } + free (pathArray); + } + + return retval; +} + +KRB5_DLLIMP errcode_t KRB5_CALLCONV +FSp_profile_init_path(files, ret_profile) + const FSSpec* files; + profile_t *ret_profile; +{ + return FSp_profile_init (files, ret_profile); +} +#endif /* TARGET_OS_MAC */ + errcode_t KRB5_CALLCONV profile_flush(profile) profile_t profile; @@ -186,12 +256,8 @@ errcode_t profile_ser_size(unused, profile, sizep) required = 3*sizeof(prof_int32); for (pfp = profile->first_file; pfp; pfp = pfp->next) { required += sizeof(prof_int32); -#ifdef PROFILE_USES_PATHS if (pfp->data->filespec) required += strlen(pfp->data->filespec); -#else - required += sizeof (profile_filespec_t); -#endif } *sizep += required; return 0; @@ -237,7 +303,6 @@ errcode_t profile_ser_externalize(unused, profile, bufpp, remainp) pack_int32(PROF_MAGIC_PROFILE, &bp, &remain); pack_int32(fcount, &bp, &remain); for (pfp = profile->first_file; pfp; pfp = pfp->next) { -#ifdef PROFILE_USES_PATHS slen = (pfp->data->filespec) ? (prof_int32) strlen(pfp->data->filespec) : 0; pack_int32(slen, &bp, &remain); @@ -246,13 +311,6 @@ errcode_t profile_ser_externalize(unused, profile, bufpp, remainp) bp += slen; remain -= (size_t) slen; } -#else - slen = sizeof (FSSpec); - pack_int32(slen, &bp, &remain); - memcpy (bp, &(pfp->data->filespec), (size_t) slen); - bp += slen; - remain -= (size_t) slen; -#endif } pack_int32(PROF_MAGIC_PROFILE, &bp, &remain); retval = 0; @@ -317,15 +375,11 @@ errcode_t profile_ser_internalize(unused, profilep, bufpp, remainp) memset(flist, 0, sizeof(char *) * (fcount+1)); for (i=0; ifile))) { if (retval == ENOENT || retval == EACCES) { + /* XXX memory leak? */ iter->file = iter->file->next; skip_num = 0; retval = 0; diff --git a/src/util/profile/profile.exp b/src/util/profile/profile.exp index ade672adc..eaf720cd2 100644 --- a/src/util/profile/profile.exp +++ b/src/util/profile/profile.exp @@ -29,3 +29,7 @@ profile_add_relation profile_ser_internalize profile_ser_externalize profile_ser_size + +# Mac only +FSp_profile_init +FSp_profile_init_path diff --git a/src/util/profile/profile.hin b/src/util/profile/profile.hin index 2f11524af..75ed03d50 100644 --- a/src/util/profile/profile.hin +++ b/src/util/profile/profile.hin @@ -5,10 +5,26 @@ #ifndef _KRB5_PROFILE_H #define _KRB5_PROFILE_H -#if defined(_WIN32) || defined(MACINTOSH) +#if defined(_WIN32) #include #endif +#if defined(macintosh) || (defined(__MACH__) && defined(__APPLE__)) + #include + #if TARGET_RT_MAC_CFM + #error "Use KfM 4.0 SDK headers for CFM compilation." + #endif +#endif +#if TARGET_OS_MAC + #if defined(__MWERKS__) + #pragma import on + #pragma enumsalwaysint on + #endif + #pragma options align=mac68k + /* We use file paths as unique file identifiers */ + #define COPY_RESOURCE_FORK 1 +#endif + #ifndef KRB5_CALLCONV #define KRB5_CALLCONV #define KRB5_CALLCONV_C @@ -23,34 +39,14 @@ typedef struct _profile_t *profile_t; #define PROFILE_ITER_SECTIONS_ONLY 0x0002 #define PROFILE_ITER_RELATIONS_ONLY 0x0004 -/* Macintoh CFM-68K magic incantation */ -#if defined(macintosh) && defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) -#pragma import on -#endif - #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -/* On everything but MacOS, we use file paths as unique file identifiers */ -#ifndef macintosh -#define PROFILE_USES_PATHS -#endif - -#ifdef PROFILE_USES_PATHS typedef char* profile_filespec_t; /* path as C string */ typedef char* profile_filespec_list_t; /* list of : separated paths, C string */ typedef const char * const_profile_filespec_t; /* path as C string */ typedef const char * const_profile_filespec_list_t; /* list of : separated paths, C string */ -#else -/* On MacOS, we use native file specifiers as unique file identifiers */ -#include -typedef FSSpec profile_filespec_t; -typedef FSSpec* profile_filespec_list_t; -/* array should be terminated with {0, 0, ""} */ -typedef FSSpec const_profile_filespec_t; -typedef FSSpec* const_profile_filespec_list_t; -#endif long KRB5_CALLCONV profile_init (const_profile_filespec_t *files, profile_t *ret_profile); @@ -58,6 +54,15 @@ long KRB5_CALLCONV profile_init long KRB5_CALLCONV profile_init_path (const_profile_filespec_list_t filelist, profile_t *ret_profile); +/* On Mac Carbon, also provide FSSpec variants */ +#if TARGET_OS_MAC && defined(__FILES__) +long KRB5_CALLCONV FSp_profile_init + (const FSSpec* files, profile_t *ret_profile); + +long KRB5_CALLCONV FSp_profile_init_path + (const FSSpec* files, profile_t *ret_profile); +#endif + long KRB5_CALLCONV profile_flush (profile_t profile); @@ -120,13 +125,16 @@ long KRB5_CALLCONV profile_add_relation (profile_t profile, const char **names, const char *new_value); +#if TARGET_OS_MAC + #if defined(__MWERKS__) + #pragma enumsalwaysint reset + #pragma import reset + #endif + #pragma options align=reset +#endif + #ifdef __cplusplus } #endif /* __cplusplus */ -/* Macintoh CFM-68K magic incantation */ -#if defined(macintosh) && defined(__CFM68K__) && !defined(__USING_STATIC_LIBS__) -#pragma import reset -#endif - #endif /* _KRB5_PROFILE_H */ -- 2.26.2