|
|
// ===========================================================================
// UAMPrefs.cpp � 1998-2002 Microsoft Corp. All rights reserved.
// ===========================================================================
// Preference file manipulation routines for the Microsoft UAM.
//
#ifdef UAM_TARGET_CARBON
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
#endif
#include <String.h>
#include <macstrsafe.h>
#include "UAMDebug.h"
#include "UAMPrefs.h"
UAM_PREFERENCES gUAMPreferences;
// ---------------------------------------------------------------------------
// � UAM_OpenOrCreateFile()
// ---------------------------------------------------------------------------
// Open or create and open the resource fork of a file.
//
SInt16 UAM_OpenOrCreateFile(
FSSpec* inSpec,
OSType inCreator,
OSType inFileType
)
{
SInt16 theRefNum = -1;
OSErr theError;
theError = FSpOpenDF(inSpec, fsRdWrPerm, &theRefNum);
if (theError == fnfErr)
{
//
//The file does not exist, create a new file.
//
theError = FSpCreate(inSpec, inCreator, inFileType, 0);
if (theError == noErr)
{
theError = FSpOpenDF(inSpec, fsRdWrPerm, &theRefNum);
}
}
if ((theRefNum == -1) || (theError != noErr))
{
DbgPrint_((DBGBUFF, "Failed to open preference file (%d)!", theError));
}
return(theRefNum);
}
// ---------------------------------------------------------------------------
// � UAM_OpenPreferenceFile()
// ---------------------------------------------------------------------------
// Opens the preference file by name.
//
SInt16 UAM_OpenPreferenceFile()
{
FSSpec theSpec;
SInt16 theRefNum = -1; //assume failure
SInt16 theVRefNum = 0;
SInt32 theDirID = 0;
OSErr theError = noErr;
theError = FindFolder(
kOnSystemDisk,
kPreferencesFolderType,
kCreateFolder,
&theVRefNum,
&theDirID
);
if (theError == noErr)
{
//
//Create the file spec that will point to the file.
//
theError = FSMakeFSSpec(
theVRefNum,
theDirID,
UAM_PREFERENCE_FILE_NAME,
&theSpec
);
if ((theError == noErr) || (theError == fnfErr))
{
theRefNum = UAM_OpenOrCreateFile(
&theSpec,
'MACS',
'pref'
);
}
else
{
//
//There was a failure creating the spec that we cannot handle. Nothing
//to do unless we're in debug.
//
DbgPrint_((DBGBUFF, "Failed to create pref file spec (%d)", ResError()));
}
}
return(theRefNum);
}
// ---------------------------------------------------------------------------
// � UAM_RetrieveUAMPreferenceData()
// ---------------------------------------------------------------------------
// Retrieves the preferences data saved in the file.
//
OSErr UAM_RetrieveUAMPreferenceData(
PUAM_PREFERENCES ioUAMPrefs
)
{
OSErr theError = noErr;
SInt16 theRefNum = -1;
SInt32 theSize;
UAM_PREFERENCES thePrefs;
//
//Open the resource fork of the preferences file.
//
theRefNum = UAM_OpenPreferenceFile();
if (theRefNum >= 0)
{
theError = SetFPos(theRefNum, fsFromStart, 0);
if (theError != noErr)
{
DbgPrint_((DBGBUFF, "FSSetForkPosition() failed! (%d)", theError));
}
//
//Read in the preference file data.
//
theSize = sizeof(UAM_PREFERENCES);
theError = FSRead(theRefNum, &theSize, &thePrefs);
//
//If there is an error or there is a discrepancy in the number
//of bytes we read in, bail.
//
if (theError != noErr)
{
DbgPrint_((DBGBUFF, "Reading from prefs file failed! (%d)", theError));
if (theError != eofErr)
{
FSClose(theRefNum);
return(theError);
}
}
}
if ((theError == eofErr) || (theSize == 0) || (theRefNum == -1))
{
DbgPrint_((DBGBUFF, "Setting default preferences..."));
//
//The preference file did not exist or we can't open the file,
//return the defaults.
//
ioUAMPrefs->flags |= UAM_PREFS_REQUIRE_STRONG_ENCRYPTION;
//
//Not really necessary, but we do it anyway.
//
ioUAMPrefs->reserved1 = 0;
ioUAMPrefs->reserved2 = 0;
theError = noEr
|