static void __DAStageProbeCallback( int status, CFBooleanRef clean, CFStringRef name, CFUUIDRef uuid, void * context ) { DADiskRef disk = context; CFMutableArrayRef keys = NULL; CFStringRef kind = NULL; DALogDebugHeader( "%s -> %s", gDAProcessNameID, gDAProcessNameID ); if ( status ) { CFMutableArrayRef candidates; candidates = ( void * ) DADiskGetContext( disk ); if ( DADiskGetFileSystem( disk ) ) { kind = DAFileSystemGetKind( DADiskGetFileSystem( disk ) ); DALogDebug( " probed disk, id = %@, with %@, failure.", disk, kind ); if ( status != FSUR_UNRECOGNIZED ) { DALogError( "unable to probe %@ (status code 0x%08X).", disk, status ); } } /* * Find a probe candidate for this media object. */ while ( CFArrayGetCount( candidates ) ) { CFDictionaryRef candidate; candidate = CFArrayGetValueAtIndex( candidates, 0 ); if ( candidate ) { DAFileSystemRef filesystem; filesystem = ( void * ) CFDictionaryGetValue( candidate, kDAFileSystemKey ); if ( filesystem ) { CFDictionaryRef properties; properties = CFDictionaryGetValue( candidate, CFSTR( kFSMediaPropertiesKey ) ); if ( properties ) { boolean_t match = FALSE; IOServiceMatchPropertyTable( DADiskGetIOMedia( disk ), properties, &match ); if ( match ) { /* * We have found a probe candidate for this media object. */ kind = DAFileSystemGetKind( filesystem ); DADiskSetFileSystem( disk, filesystem ); if ( CFDictionaryGetValue( candidate, CFSTR( "autodiskmount" ) ) == kCFBooleanFalse ) { DADiskSetOption( disk, kDADiskOptionMountAutomatic, FALSE ); DADiskSetOption( disk, kDADiskOptionMountAutomaticNoDefer, FALSE ); } CFArrayRemoveValueAtIndex( candidates, 0 ); DALogDebug( " probed disk, id = %@, with %@, ongoing.", disk, kind ); DAFileSystemProbe( filesystem, DADiskGetDevice( disk ), __DAStageProbeCallback, context ); return; } } } } CFArrayRemoveValueAtIndex( candidates, 0 ); } } DADiskSetState( disk, kDADiskStateRequireRepair, FALSE ); DADiskSetState( disk, kDADiskStateRequireRepairQuotas, FALSE ); if ( status ) { /* * We have found no probe match for this media object. */ kind = NULL; if ( DADiskGetFileSystem( disk ) ) { DADiskSetFileSystem( disk, NULL ); DALogDebug( " probed disk, id = %@, no match.", disk ); } } else { /* * We have found a probe match for this media object. */ kind = DAFileSystemGetKind( DADiskGetFileSystem( disk ) ); ///w:start if ( DADiskGetDescription( disk, kDADiskDescriptionMediaWritableKey ) == kCFBooleanFalse ) { clean = kCFBooleanTrue; } ///w:stop if ( clean == kCFBooleanFalse ) { DADiskSetState( disk, kDADiskStateRequireRepair, TRUE ); DADiskSetState( disk, kDADiskStateRequireRepairQuotas, TRUE ); } DALogDebug( " probed disk, id = %@, with %@, success.", disk, kind ); } keys = CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks ); if ( keys ) { CFTypeRef object; object = kind ? kCFBooleanTrue : kCFBooleanFalse; if ( DADiskCompareDescription( disk, kDADiskDescriptionVolumeMountableKey, object ) ) { DADiskSetDescription( disk, kDADiskDescriptionVolumeMountableKey, object ); CFArrayAppendValue( keys, kDADiskDescriptionVolumeMountableKey ); } if ( DADiskCompareDescription( disk, kDADiskDescriptionVolumeKindKey, kind ) ) { DADiskSetDescription( disk, kDADiskDescriptionVolumeKindKey, kind ); CFArrayAppendValue( keys, kDADiskDescriptionVolumeKindKey ); } if ( DADiskCompareDescription( disk, kDADiskDescriptionVolumeNameKey, name ) ) { DADiskSetDescription( disk, kDADiskDescriptionVolumeNameKey, name ); CFArrayAppendValue( keys, kDADiskDescriptionVolumeNameKey ); } if ( DADiskCompareDescription( disk, kDADiskDescriptionVolumeUUIDKey, uuid ) ) { DADiskSetDescription( disk, kDADiskDescriptionVolumeUUIDKey, uuid ); CFArrayAppendValue( keys, kDADiskDescriptionVolumeUUIDKey ); } if ( CFArrayGetCount( keys ) ) { if ( DADiskGetState( disk, kDADiskStateStagedAppear ) ) { DADiskDescriptionChangedCallback( disk, keys ); } } CFRelease( keys ); } if ( DADiskGetState( disk, kDADiskStateStagedMount ) == FALSE ) { struct statfs * mountList; int mountListCount; int mountListIndex; /* * Determine whether the disk is mounted. */ mountListCount = getmntinfo( &mountList, MNT_NOWAIT ); for ( mountListIndex = 0; mountListIndex < mountListCount; mountListIndex++ ) { if ( mountList[mountListIndex].f_fsid.val[0] == DADiskGetBSDNode( disk ) ) { /* * We have determined that the disk is mounted. */ CFURLRef path; path = CFURLCreateFromFileSystemRepresentation( kCFAllocatorDefault, ( void * ) mountList[mountListIndex].f_mntonname, strlen( mountList[mountListIndex].f_mntonname ), TRUE ); if ( path ) { _DAMountCreateTrashFolder( disk, path ); DADiskSetBypath( disk, path ); DADiskSetDescription( disk, kDADiskDescriptionVolumePathKey, path ); CFRelease( path ); } if ( strcmp( mountList[mountListIndex].f_mntonname, "/" ) == 0 ) { path = DAMountCreateMountPointWithAction( disk, kDAMountPointActionLink ); if ( path ) { DADiskSetBypath( disk, path ); CFRelease( path ); } DADiskSetOption( disk, kDADiskOptionMountAutomatic, TRUE ); DADiskSetOption( disk, kDADiskOptionMountAutomaticNoDefer, TRUE ); } DADiskSetState( disk, kDADiskStateRequireRepair, FALSE ); DADiskSetState( disk, kDADiskStateRequireRepairQuotas, FALSE ); break; } } } DAUnitSetState( disk, kDAUnitStateCommandActive, FALSE ); DADiskSetState( disk, kDADiskStateCommandActive, FALSE ); DADiskSetContext( disk, NULL ); DAStageSignal( ); CFRelease( disk ); }
void DAMountWithArguments( DADiskRef disk, CFURLRef mountpoint, DAMountCallback callback, void * callbackContext, ... ) { /* * Mount the specified volume. A status of 0 indicates success. All arguments in * the argument list shall be of type CFStringRef. The argument list must be NULL * terminated. */ CFStringRef argument = NULL; va_list arguments; CFBooleanRef automatic = kCFBooleanTrue; CFBooleanRef check = NULL; __DAMountCallbackContext * context = NULL; CFIndex count = 0; DAFileSystemRef filesystem = DADiskGetFileSystem( disk ); Boolean force = FALSE; CFIndex index = 0; CFDictionaryRef map = NULL; CFMutableStringRef options = NULL; int status = 0; DALogDebugHeader( "%s -> %s", gDAProcessNameID, gDAProcessNameID ); /* * Initialize our minimal state. */ if ( mountpoint ) { CFRetain( mountpoint ); } /* * Prepare the mount context. */ context = malloc( sizeof( __DAMountCallbackContext ) ); if ( context == NULL ) { status = ENOMEM; goto DAMountWithArgumentsErr; } /* * Prepare the mount options. */ options = CFStringCreateMutable( kCFAllocatorDefault, 0 ); if ( options == NULL ) { status = ENOMEM; goto DAMountWithArgumentsErr; } va_start( arguments, callbackContext ); while ( ( argument = va_arg( arguments, CFStringRef ) ) ) { if ( CFEqual( argument, kDAFileSystemMountArgumentForce ) ) { force = TRUE; } else if ( CFEqual( argument, CFSTR( "automatic" ) ) ) { automatic = NULL; check = kCFBooleanTrue; } else { CFStringAppend( options, argument ); CFStringAppend( options, CFSTR( "," ) ); } } va_end( arguments ); CFStringTrim( options, CFSTR( "," ) ); ///w:start context->automatic = ( automatic == NULL ) ? TRUE : FALSE; ///w:stop /* * Determine whether the volume is to be updated. */ if ( DAMountContainsArgument( options, kDAFileSystemMountArgumentUpdate ) ) { if ( mountpoint ) { status = EINVAL; goto DAMountWithArgumentsErr; } mountpoint = DADiskGetDescription( disk, kDADiskDescriptionVolumePathKey ); if ( mountpoint == NULL ) { status = EINVAL; goto DAMountWithArgumentsErr; } CFRetain( mountpoint ); } /* * Scan the mount map list. */ count = CFArrayGetCount( gDAMountMapList1 ); for ( index = 0; index < count; index++ ) { map = CFArrayGetValueAtIndex( gDAMountMapList1, index ); if ( map ) { CFTypeRef id; CFStringRef kind; id = CFDictionaryGetValue( map, kDAMountMapProbeIDKey ); kind = CFDictionaryGetValue( map, kDAMountMapProbeKindKey ); if ( kind ) { /* * Determine whether the volume kind matches. */ if ( CFEqual( kind, DAFileSystemGetKind( filesystem ) ) == FALSE ) { continue; } } if ( CFGetTypeID( id ) == CFUUIDGetTypeID( ) ) { /* * Determine whether the volume UUID matches. */ if ( DADiskCompareDescription( disk, kDADiskDescriptionVolumeUUIDKey, id ) == kCFCompareEqualTo ) { break; } } else if ( CFGetTypeID( id ) == CFStringGetTypeID( ) ) { /* * Determine whether the volume name matches. */ if ( DADiskCompareDescription( disk, kDADiskDescriptionVolumeNameKey, id ) == kCFCompareEqualTo ) { break; } } else if ( CFGetTypeID( id ) == CFDictionaryGetTypeID( ) ) { boolean_t match = FALSE; /* * Determine whether the device description matches. */ IOServiceMatchPropertyTable( DADiskGetIOMedia( disk ), id, &match ); if ( match ) { break; } } } } /* * Process the map. */ if ( index < count ) { CFStringRef string; /* * Determine whether the volume is to be mounted. */ if ( automatic == NULL ) { automatic = CFDictionaryGetValue( map, kDAMountMapMountAutomaticKey ); if ( automatic == kCFBooleanTrue ) { DADiskSetState( disk, _kDADiskStateMountAutomatic, TRUE ); DADiskSetState( disk, _kDADiskStateMountAutomaticNoDefer, TRUE ); } } /* * Prepare the mount options. */ string = CFDictionaryGetValue( map, kDAMountMapMountOptionsKey ); if ( string ) { CFStringInsert( options, 0, CFSTR( "," ) ); CFStringInsert( options, 0, string ); } /* * Prepare the mount point. */ if ( mountpoint == NULL ) { mountpoint = CFDictionaryGetValue( map, kDAMountMapMountPathKey ); if ( mountpoint ) { CFRetain( mountpoint ); } } } /* * Scan the mount map list. */ count = CFArrayGetCount( gDAMountMapList2 ); for ( index = 0; index < count; index++ ) { map = CFArrayGetValueAtIndex( gDAMountMapList2, index ); if ( map ) { CFTypeRef id; id = CFDictionaryGetValue( map, kDAMountMapProbeIDKey ); /* * Determine whether the volume UUID matches. */ if ( DADiskCompareDescription( disk, kDADiskDescriptionVolumeUUIDKey, id ) == kCFCompareEqualTo ) { break; } } } /* * Process the map. */ if ( index < count ) { CFStringRef string; /* * Prepare the mount options. */ string = CFDictionaryGetValue( map, kDAMountMapMountOptionsKey ); if ( string ) { CFStringInsert( options, 0, CFSTR( "," ) ); CFStringInsert( options, 0, string ); } } /* * Determine whether the volume is to be mounted. */ if ( automatic == NULL ) { if ( DADiskGetState( disk, _kDADiskStateMountAutomatic ) ) { if ( DADiskGetState( disk, _kDADiskStateMountAutomaticNoDefer ) ) { automatic = kCFBooleanTrue; } } else { automatic = kCFBooleanFalse; } if ( automatic == NULL ) { if ( gDAConsoleUserList == NULL ) { if ( DAMountGetPreference( disk, kDAMountPreferenceDefer ) ) { automatic = kCFBooleanFalse; } } } } if ( automatic == kCFBooleanFalse ) { status = ECANCELED; goto DAMountWithArgumentsErr; } /* * Prepare the mount options. */ if ( DADiskGetDescription( disk, kDADiskDescriptionMediaWritableKey ) == kCFBooleanFalse ) { CFStringInsert( options, 0, CFSTR( "," ) ); CFStringInsert( options, 0, kDAFileSystemMountArgumentNoWrite ); } if ( DAMountGetPreference( disk, kDAMountPreferenceTrust ) == FALSE ) { CFStringInsert( options, 0, CFSTR( "," ) ); CFStringInsert( options, 0, kDAFileSystemMountArgumentNoSetUserID ); CFStringInsert( options, 0, CFSTR( "," ) ); CFStringInsert( options, 0, kDAFileSystemMountArgumentNoOwnership ); CFStringInsert( options, 0, CFSTR( "," ) ); CFStringInsert( options, 0, kDAFileSystemMountArgumentNoDevice ); } ///w:start if ( CFEqual( DAFileSystemGetKind( filesystem ), CFSTR( "hfs" ) ) ) { ___CFStringInsertFormat( options, 0, CFSTR( "-m=%o," ), 0755 ); if ( DADiskGetUserGID( disk ) ) { ___CFStringInsertFormat( options, 0, CFSTR( "-g=%d," ), DADiskGetUserGID( disk ) ); } else { ___CFStringInsertFormat( options, 0, CFSTR( "-g=%d," ), ___GID_UNKNOWN ); } if ( DADiskGetUserUID( disk ) ) { ___CFStringInsertFormat( options, 0, CFSTR( "-u=%d," ), DADiskGetUserUID( disk ) ); } else { ___CFStringInsertFormat( options, 0, CFSTR( "-u=%d," ), ___UID_UNKNOWN ); } } ///w:stop CFStringTrim( options, CFSTR( "," ) ); /* * Determine whether the volume is to be repaired. */ if ( check == NULL ) { if ( DAMountContainsArgument( options, kDAFileSystemMountArgumentNoWrite ) ) { check = kCFBooleanFalse; } else { check = kCFBooleanTrue; } } if ( check == kCFBooleanFalse ) { if ( DADiskGetState( disk, kDADiskStateRequireRepair ) ) { if ( force == FALSE ) { status = ___EDIRTY; goto DAMountWithArgumentsErr; } } } if ( check == kCFBooleanTrue ) { if ( DADiskGetState( disk, kDADiskStateRequireRepair ) == FALSE ) { check = kCFBooleanFalse; } } /* * Repair the volume. */ CFRetain( disk ); context->assertionID = kIOPMNullAssertionID; context->callback = callback; context->callbackContext = callbackContext; context->disk = disk; context->force = force; context->mountpoint = mountpoint; context->options = options; if ( check == kCFBooleanTrue ) { DALogDebug( " repaired disk, id = %@, ongoing.", disk ); IOPMAssertionCreateWithDescription( kIOPMAssertionTypePreventUserIdleSystemSleep, CFSTR( _kDADaemonName ), NULL, NULL, NULL, 0, NULL, &context->assertionID ); DAFileSystemRepair( DADiskGetFileSystem( disk ), DADiskGetDevice( disk ), __DAMountWithArgumentsCallbackStage1, context ); } else { __DAMountWithArgumentsCallbackStage1( ECANCELED, context ); } DAMountWithArgumentsErr: if ( status ) { if ( context ) { free( context ); } if ( mountpoint ) { CFRelease( mountpoint ); } if ( options ) { CFRelease( options ); } if ( callback ) { ( callback )( status, NULL, callbackContext ); } } }