i_o.c

/*
    File:       i_o.c
    
    Description:
 
    Author:     
 
    Copyright:  Copyright: © 1990-1999 by Apple Computer, Inc.
                all rights reserved.
    
    Disclaimer: You may incorporate this sample code into your applications without
                restriction, though the sample code has been provided "AS IS" and the
                responsibility for its operation is 100% yours.  However, what you are
                not permitted to do is to redistribute the source as "DSC Sample Code"
                after having made changes. If you're going to re-distribute the source,
                we require that you make it clear in the source that the code was
                descended from Apple Sample Code, but that you've made changes.
    
    Change History (most recent first):
                6/24/99 Updated for Metrowerks Codewarror Pro 2.1(KG)
 
*/
#include <stdio.h>
#include <Types.h>
#include <ToolUtils.h>
#include <Quickdraw.h>
#include <Files.h>
#include <Memory.h>
#include <Devices.h>
#include <MacMemory.h>
 
#define ISO
 
#include "HighSierra.h"
#include "BuildISO.h"
 
#include "i_o.h"
#include "Support.h"
#include "ErrorMsg.h"
 
 
/************************************************************************
 *
 *  Function:       isoOpen
 *
 *  Purpose:        Open the driver for i/o
 *
 *  Returns:        OSErr
 *
 *  Side Effects:   fills *RefNum with valid reference number for driver.
 *
 *  Description:    Issue Mac OS PBOpen call to open the driver.  We
 *                  can use the returned reference number to access the
 *                  driver for raw i/o.
 *
 *
 ************************************************************************/
OSErr
isoOpen(StringPtr fn, short *RefNum)
{
    ParamBlockRec   pb;
    OSErr   result;
    
    pb.ioParam.ioNamePtr = fn;
    pb.ioParam.ioCompletion = 0;
    pb.ioParam.ioPermssn = fsRdPerm;
    result = PBOpen(&pb, false);
    *RefNum = pb.ioParam.ioRefNum;
    
    return result;
}
 
/************************************************************************
 *
 *  Function:   isoRead
 *
 *  Purpose:    read from the driver
 *
 *  Returns:    OSErr
 *
 *  Side Effects:   fills dest with contents for 'count' bytes.
 *
 *  Description:    do a call to the Mac CD driver to read a specified
 *                  number of bytes starting at 'offset'.  The number of
 *                  bytes must be a multiple of 512, or this call won't
 *                  work (the driver returns an error)
 *
 ************************************************************************/
OSErr
isoRead(short referenceNumber, Ptr dest, long count, long offset)
{
    OSErr result;
    ParamBlockRec   io;
    
    ClearOut((char *)&io, sizeof(io));
    io.ioParam.ioCompletion = NULL;
    io.ioParam.ioVRefNum = 0;
    io.ioParam.ioRefNum = referenceNumber;
    io.ioParam.ioBuffer = dest;
    io.ioParam.ioReqCount = count;
    io.ioParam.ioPosMode = fsFromStart;
    io.ioParam.ioPosOffset = offset;
    result = PBRead(&io, false);
    
    if (result != noErr)
        ErrorMsg("PBRead failed. ioActCount = %d", io.ioParam.ioActCount);
    
    return result;
}
 
 
/************************************************************************
 *
 *  Function:   isoWrite
 *
 *  Purpose:    Write to the driver
 *
 *  Returns:    OSErr
 *
 *  Side Effects:   none
 *
 *  Description:    do a call to the Mac CD driver to write a specified
 *                  number of bytes starting at 'offset'.  The number of
 *                  bytes must be a multiple of 512, or this call won't
 *                  work (the driver returns an error.)
 *
 ************************************************************************/
OSErr
isoWrite(short referenceNumber, Ptr buffer, long count, long offset)
{
    OSErr result;
    ParamBlockRec   io;
    
    ClearOut((char *)&io, sizeof(io));
    io.ioParam.ioCompletion = NULL;
    io.ioParam.ioVRefNum = 1;
    io.ioParam.ioRefNum = referenceNumber;
    io.ioParam.ioBuffer = buffer;
    io.ioParam.ioReqCount = count;
    io.ioParam.ioPosMode = fsFromStart;
    io.ioParam.ioPosOffset = offset;
    result = PBWrite(&io, false);
    
    if (result != noErr)
        ErrorMsg("PBWrite failed. result = %d", result);
    
    return result;
}
 
 
/************************************************************************
 *
 *  Function:       ZeroDisk
 *
 *  Purpose:        Clean out this disk so it's not HFS anymore
 *
 *  Returns:        OSErr
 *
 *  Side Effects:   zero out a lot of disk
 *
 *  Description:    loop, doing a write of zeros.
 *
 ************************************************************************/
OSErr
ZeroDisk(short referenceNumber)
{
    Ptr     noBuffer;
    short   i;
    short   j;
    OSErr   result;
    CursHandle  cursor;
    
    noBuffer = NewPtr(CDBLKSIZE);
    if (noBuffer == NULL)
        return (MemError());
    for (i = 0; i < CDBLKSIZE; i++)
        noBuffer[i] = 0;
 
    result = noErr;
 
    cursor = GetCursor(watchCursor);
    if (!cursor)
        SetCursor(*cursor);
    for (i = 0, j = 0; i < 10 && result == noErr; i++, j++)
        result = isoWrite(referenceNumber, noBuffer, (long)CDBLKSIZE, (long) j*CDBLKSIZE);
    
    SetCursor(&qd.arrow);
    DisposePtr(noBuffer);
    return result;
}
 
/************************************************************************
 *
 *  Function:       GetDriveNumber
 *
 *  Purpose:        Get the driver number
 *
 *  Returns:        short.  The driver number
 *
 *  Side Effects:   none.
 *
 *  Description:    PBHGetVInfo() will retrieve the driver
 *                  number, given the vRefNum associated with a file.
 *
 ************************************************************************/
short
GetDriveNumber(short vRefNum)
{
    HParamBlockRec  io;
    
    io.volumeParam.ioCompletion = NULL;
    io.volumeParam.ioNamePtr = NULL;
    io.volumeParam.ioVRefNum = vRefNum;
    io.volumeParam.ioVolIndex = 0;
    PBHGetVInfo(&io, false);
    return io.volumeParam.ioVDrvInfo;
}