You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 lines
5.2 KiB
1 lines
5.2 KiB
// ===========================================================================
// 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 = noErr;
}
else
{
BlockMove(&thePrefs, ioUAMPrefs, sizeof(UAM_PREFERENCES));
DbgPrint_((DBGBUFF, "Pref flags = 0x%x", ioUAMPrefs->flags));
}
FSClose(theRefNum);
return(theError);
}
// ---------------------------------------------------------------------------
// ¥ UAM_SaveUAMPreferenceData()
// ---------------------------------------------------------------------------
// Saves the preferences data to file.
//
OSErr UAM_SaveUAMPreferenceData(
PUAM_PREFERENCES inUAMPrefs
)
{
SInt16 theRefNum = -1;
OSErr theError = noErr;
SInt32 theSize;
//
//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));
}
else
{
theSize = sizeof(UAM_PREFERENCES);
theError = FSWrite(theRefNum, &theSize, inUAMPrefs);
if (theError != noErr)
{
DbgPrint_((DBGBUFF, "Writing to prefs file failed! (%d)", theError));
}
else if (theSize == sizeof(UAM_PREFERENCES))
{
DbgPrint_((DBGBUFF, "Preferences successfully written to disk"));
}
}
FSClose(theRefNum);
}
return(theError);
}
|