Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Deleting a Record

The sample code in Listing 3-5 demonstrates how to delete a record. The sample code opens an Open Directory session and gets an Open Directory reference. Then it calls its MyOpenDirNode routine and passes to it the address of the node reference (nodeRef) that it has allocated. The MyOpenDirNode routine is described in the section “Opening and Closing a Node.”

The sample code then calls its DeleteRecord routine and passes to it the node reference (nodeRef) obtained by calling its MyOpenDirNode routine.

The DeleteRecord routine calls dsDataNodeAllocateString to allocate a data node (recName) containing the string “testuser” as the name of the record that is to be deleted and another data node (recType) specifying kDSStdRecordTypeUsers as the record type of the record that is to be deleted. It then calls dsOpenRecord to open the record that is to be deleted and calls dsDeleteRecord to delete the record.

To reclaim memory associated with recType and recName, the DeleteRecord routine calls dsDataNodeDeAllocate.

The dsDeleteRecord function implicitly closes any record that it deletes. If dsDeleteRecord returns an error, indicating that the record was not deleted, the DeleteRecord routine in Listing 3-5 calls dsCloseRecord to close the record.

Listing 3-5  Deleting a record

void main ( )
{
    long dirStatus = eDSNoErr;
    tDirNodeReference nodeRef = NULL;
    dirStatus = dsOpenDirService( &gDirRef );
    if ( dirStatus == eDSNoErr )
    {
        dirStatus = MyOpenDirNode( &nodeRef );
        if ( dirStatus == eDSNoErr )
        {
            DeleteRecord( nodeRef );
            dsCloseDirNode( nodeRef );
        }
    }
    if ( gDirRef != NULL )
    {
        dirStatus = dsCloseDirService( gDirRef );
    }
}
void DeleteRecord ( const tDirNodeReference nodeRef )
{
    long dirStatus = eDSNoErr;
    tRecordReference recRef = NULL;
    tDataNodePtr recName = NULL;
    tDataNodePtr recType = NULL;
    recName = dsDataNodeAllocateString( gDirRef, "testuser"  );
    if ( recName != NULL )
    {
        recType = dsDataNodeAllocateString( gDirRef, kDSStdRecordTypeUsers  );
        if ( recType != NULL )
        {
            dirStatus = dsOpenRecord( nodeRef, recType, recName,  &recRef );
            if ( dirStatus == eDSNoErr )
            {
                dirStatus = dsDeleteRecord( recRef );
                if (dirStatus != eDSNoErr)
                {
                    // The record was not deleted, so close it.
                    dirStatus = dsCloseRecord( recRef );
                }
                recRef = NULL;
            }
            dsDataNodeDeAllocate( gDirRef, recType );
            recType = NULL;
        }
        dsDataNodeDeAllocate( gDirRef, recName );
        recName = NULL;
    }
} // DeleteRecord

Note that for this example to work, it would have to be run by a root process on the local NetInfo domain, or by a user process that has called dsDoDirNodeAuth with the inDirNodeAuthOnlyFlag parameter set to FALSE to get permission to make this change.



< Previous PageNext Page > Hide TOC


Last updated: 2007-01-08




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice