void writePropertyListToFile (CFDataRef data) { CFStringRef errorString; CFPropertyListRef propertyList = CFPropertyListCreateFromXMLData (NULL, data, kCFPropertyListMutableContainersAndLeaves, &errorString); if (errorString == NULL) { CFStringRef urlString = CFStringCreateWithCString (NULL, kFilename, CFStringGetSystemEncoding ()); CFURLRef fileURL = CFURLCreateWithFileSystemPath (NULL, urlString, kCFURLPOSIXPathStyle, FALSE); CFWriteStreamRef stream = CFWriteStreamCreateWithFile (NULL, fileURL); Boolean isOpen = CFWriteStreamOpen (stream); CFShow (CFSTR ("Property list (as written to file):")); CFShow (propertyList); CFIndex bytesWritten = CFPropertyListWriteToStream (propertyList, stream, kCFPropertyListXMLFormat_v1_0, NULL); CFWriteStreamClose (stream); } else { CFShow (errorString); CFRelease (errorString); } CFRelease (propertyList); }
/* @overload write_plist(hash, path) * * Writes the serialized contents of a property list to the specified path. * * @note This does not yet support all possible types that can exist in a valid property list. * * @note This currently only assumes to be given an Xcode project document. * This means that it only accepts dictionaries, arrays, and strings in * the document. * * @param [Hash] hash The property list to serialize. * @param [String] path The path to the property list file. * @return [true, false] Wether or not saving was successful. */ static VALUE write_plist(VALUE self, VALUE hash, VALUE path) { VALUE h = rb_check_convert_type(hash, T_HASH, "Hash", "to_hash"); if (NIL_P(h)) { rb_raise(rb_eTypeError, "%s can't be coerced to Hash", rb_obj_classname(hash)); } CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); rb_hash_foreach(h, dictionary_set, (st_data_t)dict); CFURLRef fileURL = str_to_url(path); CFWriteStreamRef stream = CFWriteStreamCreateWithFile(NULL, fileURL); CFRelease(fileURL); CFIndex success = 0; if (CFWriteStreamOpen(stream)) { CFStringRef errorString; success = CFPropertyListWriteToStream(dict, stream, kCFPropertyListXMLFormat_v1_0, &errorString); if (!success) { CFShow(errorString); } CFWriteStreamClose(stream); } else { printf("Unable to open stream!\n"); } CFRelease(stream); CFRelease(dict); return success ? Qtrue : Qfalse; }
void TCPStream_CFNetwork::close() { if (state == Stream::State::Closed) { return; } state = Stream::State::Closed; if (secureLayerContext != nullptr) { SSLClose(secureLayerContext); } if (inputStream != nullptr) { CFReadStreamUnscheduleFromRunLoop(inputStream, CFRunLoopGetMain(), kCFRunLoopDefaultMode); CFReadStreamClose(inputStream); CFRelease(inputStream); inputStream = nullptr; } if (outputStream != nullptr) { CFWriteStreamClose(outputStream); CFRelease(outputStream); outputStream= nullptr; } if (secureLayerContext != nullptr) { CFRelease(secureLayerContext); secureLayerContext = nullptr; } handleClosedEvent(); }
void Caching_Stream::streamEndEncountered() { if (m_fileOutput) { delete m_fileOutput, m_fileOutput = 0; } if (m_cacheable) { if (m_writable) { CS_TRACE("Successfully cached the stream\n"); CS_TRACE_CFURL(m_fileUrl); // We only write the meta data if the stream was successfully streamed. // In that way we can use the meta data as an indicator that there is a file to stream. if (!m_cacheMetaDataWritten) { CFWriteStreamRef writeStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault, m_metaDataUrl); if (writeStream) { if (CFWriteStreamOpen(writeStream)) { CFStringRef contentType = m_target->contentType(); UInt8 buf[1024]; CFIndex usedBytes = 0; if (contentType) { // It is possible that some streams don't provide a content type CFStringGetBytes(contentType, CFRangeMake(0, CFStringGetLength(contentType)), kCFStringEncodingUTF8, '?', false, buf, 1024, &usedBytes); } if (usedBytes > 0) { CS_TRACE("Writing the meta data\n"); CS_TRACE_CFSTRING(contentType); CFWriteStreamWrite(writeStream, buf, usedBytes); } CFWriteStreamClose(writeStream); } CFRelease(writeStream); } m_cacheable = false; m_writable = false; m_useCache = true; m_cacheMetaDataWritten = true; } } } if (m_delegate) { m_delegate->streamEndEncountered(); } }
void dump_plist(CFMutableDictionaryRef properties) { CFWriteStreamRef stdoutStream = NULL; CFURLRef devStdout = CFURLCreateWithFileSystemPath( NULL, CFSTR("/dev/stdout"), kCFURLPOSIXPathStyle, false ); stdoutStream = CFWriteStreamCreateWithFile(NULL, devStdout); CFRelease(devStdout); if (stdoutStream == NULL) errx(1,"cannot create CFWriteStream for /dev/stdout"); if (!CFWriteStreamOpen(stdoutStream)) errx(1,"cannot open CFWriteStream for /dev/stdout"); CFPropertyListWrite( properties, stdoutStream, kCFPropertyListXMLFormat_v1_0, 0, NULL ); CFWriteStreamClose(stdoutStream); CFRelease(stdoutStream); }
RetainPtr<CFDataRef> LegacyWebArchive::rawDataRepresentation() { RetainPtr<CFDictionaryRef> propertyList = createPropertyListRepresentation(this); ASSERT(propertyList); if (!propertyList) { LOG(Archives, "LegacyWebArchive - Failed to create property list for archive, returning no data"); return 0; } RetainPtr<CFWriteStreamRef> stream(AdoptCF, CFWriteStreamCreateWithAllocatedBuffers(0, 0)); CFWriteStreamOpen(stream.get()); CFPropertyListWriteToStream(propertyList.get(), stream.get(), kCFPropertyListBinaryFormat_v1_0, 0); RetainPtr<CFDataRef> plistData(AdoptCF, static_cast<CFDataRef>(CFWriteStreamCopyProperty(stream.get(), kCFStreamPropertyDataWritten))); ASSERT(plistData); CFWriteStreamClose(stream.get()); if (!plistData) { LOG(Archives, "LegacyWebArchive - Failed to convert property list into raw data, returning no data"); return 0; } return plistData; }
void SocketStreamHandle::platformClose() { LOG(Network, "SocketStreamHandle %p platformClose", this); if (m_pacRunLoopSource) removePACRunLoopSource(); ASSERT(!m_readStream == !m_writeStream); if (!m_readStream) { if (m_connectingSubstate == New || m_connectingSubstate == ExecutingPACFile) m_client->didCloseSocketStream(this); return; } #if PLATFORM(WIN) CFReadStreamUnscheduleFromRunLoop(m_readStream.get(), loaderRunLoop(), kCFRunLoopDefaultMode); CFWriteStreamUnscheduleFromRunLoop(m_writeStream.get(), loaderRunLoop(), kCFRunLoopDefaultMode); #else CFReadStreamUnscheduleFromRunLoop(m_readStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFWriteStreamUnscheduleFromRunLoop(m_writeStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); #endif CFReadStreamClose(m_readStream.get()); CFWriteStreamClose(m_writeStream.get()); m_readStream = 0; m_writeStream = 0; m_client->didCloseSocketStream(this); }
/* MyStreamInfoDestroy destroys a MyStreamInfo 'object', cleaning up any resources that it owns. */ static void MyStreamInfoDestroy(MyStreamInfo * info) { assert(info != NULL); if (info->readStream) { CFReadStreamUnscheduleFromRunLoop(info->readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); (void) CFReadStreamSetClient(info->readStream, kCFStreamEventNone, NULL, NULL); /* CFReadStreamClose terminates the stream. */ CFReadStreamClose(info->readStream); CFRelease(info->readStream); } if (info->writeStream) { CFWriteStreamUnscheduleFromRunLoop(info->writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); (void) CFWriteStreamSetClient(info->writeStream, kCFStreamEventNone, NULL, NULL); /* CFWriteStreamClose terminates the stream. */ CFWriteStreamClose(info->writeStream); CFRelease(info->writeStream); } if (info->proxyDict) { CFRelease(info->proxyDict); // see discussion of <rdar://problem/3745574> below } free(info); }
static void cfstream_data_close(struct mailstream_cfstream_data * cfstream_data) { if (cfstream_data->writeStream != NULL) { CFWriteStreamClose(cfstream_data->writeStream); CFRelease(cfstream_data->writeStream); cfstream_data->writeStream = NULL; } if (cfstream_data->readStream != NULL) { CFReadStreamClose(cfstream_data->readStream); CFRelease(cfstream_data->readStream); cfstream_data->readStream = NULL; } }
static void CloseStreams(MyStreamInfoPtr myInfoPtr) { assert(myInfoPtr != NULL); if (myInfoPtr->rStreamRef) { CFReadStreamClose(myInfoPtr->rStreamRef); myInfoPtr->rStreamRef = NULL; } if (myInfoPtr->wStreamRef) { CFWriteStreamClose(myInfoPtr->wStreamRef); myInfoPtr->wStreamRef = NULL; } }
static void cfstream_data_close(struct mailstream_cfstream_data * cfstream_data) { if (cfstream_data->writeStream != NULL) { CFWriteStreamSetClient(cfstream_data->writeStream, kCFStreamEventNone, NULL, NULL); CFWriteStreamClose(cfstream_data->writeStream); CFRelease(cfstream_data->writeStream); cfstream_data->writeStream = NULL; } if (cfstream_data->readStream != NULL) { CFReadStreamSetClient(cfstream_data->readStream, kCFStreamEventNone, NULL, NULL); CFReadStreamClose(cfstream_data->readStream); CFRelease(cfstream_data->readStream); cfstream_data->readStream = NULL; } }
sdmmd_return_t SDMMD_ServiceSendStream(SocketConnection handle, CFPropertyListRef data, CFPropertyListFormat format) { CFStringRef errStr; CFWriteStreamRef write = CFWriteStreamCreateWithAllocatedBuffers(kCFAllocatorDefault, kCFAllocatorDefault); CFWriteStreamOpen(write); uint32_t length = CFPropertyListWriteToStream(data, write, format, &errStr); CFDataRef xmlData = CFWriteStreamCopyProperty(write, kCFStreamPropertyDataWritten); sdmmd_return_t result = kAMDInvalidArgumentError; if (length == CFDataGetLength(xmlData)) { result = ((data) ? SDMMD_ServiceSend(handle, xmlData) : kAMDInvalidArgumentError); } if (xmlData) CFRelease(xmlData); CFWriteStreamClose(write); if (write) CFRelease(write); return result; }
/* extern */ void HttpContextClose(HttpContextRef context) { CFRunLoopRef runLoop = CFRunLoopGetCurrent(); // Check if the read stream exists. if (context->_inStream) { // Unschedule, close, and release it. CFReadStreamSetClient(context->_inStream, 0, NULL, NULL); CFReadStreamUnscheduleFromRunLoop(context->_inStream, runLoop, kCFRunLoopCommonModes); CFReadStreamClose(context->_inStream); CFRelease(context->_inStream); // Remove the reference. context->_inStream = NULL; } // Check if the write stream exists. if (context->_outStream) { // Unschedule, close, and release it. CFWriteStreamSetClient(context->_outStream, 0, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop(context->_outStream, runLoop, kCFRunLoopCommonModes); CFWriteStreamClose(context->_outStream); CFRelease(context->_outStream); // Remove the reference. context->_outStream = NULL; } // Get rid of the timer, if it still exists if (context->_timer != NULL) { CFRunLoopTimerInvalidate(context->_timer); CFRelease(context->_timer); context->_timer = NULL; } }
void SSLImpl::shutdown(void *storage) { ssl_data_t *data = (ssl_data_t*)storage; if (!data) return; CFReadStreamScheduleWithRunLoop( data->readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFWriteStreamScheduleWithRunLoop( data->writeStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); if (data->readStream) { CFReadStreamRef readStream = (CFReadStreamRef)data->readStream; CFReadStreamClose(readStream); data->readStream = NULL; } if (data->writeStream) { CFWriteStreamRef writeStream = (CFWriteStreamRef)data->writeStream; CFWriteStreamClose(writeStream); data->writeStream = NULL; } }
void SocketStreamHandle::platformClose() { LOG(Network, "SocketStreamHandle %p platformClose", this); #ifndef BUILDING_ON_TIGER if (m_pacRunLoopSource) removePACRunLoopSource(); #endif ASSERT(!m_readStream == !m_writeStream); if (!m_readStream) return; CFReadStreamUnscheduleFromRunLoop(m_readStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFWriteStreamUnscheduleFromRunLoop(m_writeStream.get(), CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFReadStreamClose(m_readStream.get()); CFWriteStreamClose(m_writeStream.get()); m_readStream = 0; m_writeStream = 0; m_client->didClose(this); }
CFNetDiagnosticStatus CFNetDiagnosticDiagnoseProblemInteractively(CFNetDiagnosticRef details) { SInt32 retval = 0; mach_port_t port = MACH_PORT_NULL; CFDataRef msgData = NULL; kern_return_t err; //build message CFWriteStreamRef stream = CFWriteStreamCreateWithAllocatedBuffers(kCFAllocatorDefault, kCFAllocatorDefault); CFWriteStreamOpen(stream); CFIndex len = CFPropertyListWriteToStream(details, stream, kCFPropertyListBinaryFormat_v1_0, NULL); CFWriteStreamClose(stream); if(len > 0) { msgData = CFWriteStreamCopyProperty(stream, kCFStreamPropertyDataWritten); } CFRelease(stream); if(msgData) { err = bootstrap_look_up(bootstrap_port, *((name_t*)(&_CFNetDiagnosticMachPortName)), &port); if (err == KERN_SUCCESS) { err = _CFNetDiagnosticClient_passDescriptor( port, _CFNetDiagnosticMachProtocolVersion, (vm_address_t)CFDataGetBytePtr(msgData), CFDataGetLength(msgData)); if (err == KERN_SUCCESS) { //FIXME Yay!!! } } CFRelease(msgData); } return (CFNetDiagnosticStatus)retval; }
int main() { if (geteuid()) { syslog(LOG_ERR,"Error: Daemon must run as root."); exit(geteuid()); } encrypt_buffer = CFDataCreateMutable(kCFAllocatorDefault,8); /*********Set up File**********/ if (!(pathName = (CFStringRef)CFPreferencesCopyAppValue(PATHNAME_PREF_KEY,PREF_DOMAIN))) { pathName = CFSTR(DEFAULT_PATHNAME); CFPreferencesSetAppValue(PATHNAME_PREF_KEY,pathName,PREF_DOMAIN); } CFURLRef logPathURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,pathName,kCFURLPOSIXPathStyle,false); logStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault,logPathURL); CFRelease(logPathURL); if (!logStream) { syslog(LOG_ERR,"Error: Couldn't open file stream at start."); return 1; } /*********Check encryption & keymap**********/ updateEncryption(); updateKeymap(); /*********Check space**********/ if (outOfSpace(pathName)) { stamp_file(CFSTR("Not enough disk space remaining!")); CFRunLoopStop(CFRunLoopGetCurrent()); } /*********Connect to kernel extension**********/ if (!connectToKext()) { if (load_kext()) { stamp_file(CFSTR("Could not load KEXT")); return 1; } if (!connectToKext()) { stamp_file(CFSTR("Could not connect with KEXT")); return 1; } } sleep(1); // just a little time to let the kernel notification handlers finish stamp_file(CFSTR("LogKext Daemon starting up")); // stamp login file with initial user LoginLogoutCallBackFunction(NULL, NULL, NULL); CFPreferencesAppSynchronize(PREF_DOMAIN); /*********Create Daemon Timer source**********/ CFRunLoopTimerContext timerContext = { 0 }; CFRunLoopSourceRef loginLogoutSource; if (InstallLoginLogoutNotifiers(&loginLogoutSource)) syslog(LOG_ERR,"Error: could not install login notifier"); else CFRunLoopAddSource(CFRunLoopGetCurrent(),loginLogoutSource, kCFRunLoopDefaultMode); CFRunLoopTimerRef daemonTimer = CFRunLoopTimerCreate(NULL, 0, TIME_TO_SLEEP, 0, 0, DaemonTimerCallback, &timerContext); CFRunLoopAddTimer(CFRunLoopGetCurrent(), daemonTimer, kCFRunLoopCommonModes); CFRunLoopRun(); stamp_file(CFSTR("Server error: closing Daemon")); CFWriteStreamClose(logStream); }
File_Output::~File_Output() { CFWriteStreamClose(m_writeStream); CFRelease(m_writeStream); }
static krb5_error_code od_dump_entry(krb5_context kcontext, HDB *db, hdb_entry_ex *entry, void *data) { CFErrorRef error = NULL; CFDictionaryRef dict; CFStringRef fn, uuidstr; CFUUIDRef uuid; CFURLRef url; dict = HeimODDumpHdbEntry(&entry->entry, &error); if (dict == NULL) { if (error) CFRelease(error); return 0; } uuid = CFUUIDCreate(NULL); if (uuid == NULL) { krb5_warnx(kcontext, "out of memory"); CFRelease(dict); return 0; } uuidstr = CFUUIDCreateString(NULL, uuid); CFRelease(uuid); if (uuidstr == NULL) { krb5_warnx(kcontext, "out of memory"); CFRelease(dict); return 0; } fn = CFStringCreateWithFormat(NULL, NULL, CFSTR("%s/%@.plist"), (char *)data, uuidstr); CFRelease(uuidstr); if (fn == NULL) { krb5_warnx(kcontext, "out of memory"); CFRelease(dict); return 0; } url = CFURLCreateWithFileSystemPath(NULL, fn, kCFURLPOSIXPathStyle, false); CFRelease(fn); if (url == NULL) { krb5_warnx(kcontext, "out of memory"); CFRelease(dict); return 0; } CFDataRef xmldata = CFPropertyListCreateData(NULL, dict, kCFPropertyListXMLFormat_v1_0, 0, NULL); CFRelease(dict); if (xmldata == NULL) { CFRelease(url); krb5_warnx(kcontext, "out of memory"); return 0; } CFWriteStreamRef stream = CFWriteStreamCreateWithFile(NULL, url); if (stream) { if (CFWriteStreamOpen(stream)) CFWriteStreamWrite(stream, CFDataGetBytePtr(xmldata), CFDataGetLength(xmldata)); CFWriteStreamClose(stream); CFRelease(stream); } CFRelease(url); CFRelease(xmldata); return 0; }
// domain should already be locked. static Boolean _writeXMLFile(CFURLRef url, CFMutableDictionaryRef dict, Boolean isWorldReadable, Boolean *tryAgain) { Boolean success = false; CFAllocatorRef alloc = __CFPreferencesAllocator(); *tryAgain = false; if (CFDictionaryGetCount(dict) == 0) { // Destroy the file CFBooleanRef val = (CFBooleanRef) CFURLCreatePropertyFromResource(alloc, url, kCFURLFileExists, NULL); if (val && CFBooleanGetValue(val)) { success = CFURLDestroyResource(url, NULL); } else { success = true; } if (val) CFRelease(val); } else { CFPropertyListFormat desiredFormat = __CFPreferencesShouldWriteXML() ? kCFPropertyListXMLFormat_v1_0 : kCFPropertyListBinaryFormat_v1_0; CFWriteStreamRef binStream = CFWriteStreamCreateWithAllocatedBuffers(alloc, alloc); CFWriteStreamOpen(binStream); CFPropertyListWriteToStream(dict, binStream, desiredFormat, NULL); CFWriteStreamClose(binStream); CFDataRef data = (CFDataRef) CFWriteStreamCopyProperty(binStream, kCFStreamPropertyDataWritten); CFRelease(binStream); if (data) { SInt32 mode; #if DEPLOYMENT_TARGET_MACOSX mode = isWorldReadable ? S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH : S_IRUSR|S_IWUSR; #else mode = 0666; #endif #if DEPLOYMENT_TARGET_MACOSX { // Try quick atomic way first, then fallback to slower ways and error cases CFStringRef scheme = CFURLCopyScheme(url); if (!scheme) { *tryAgain = false; CFRelease(data); return false; } else if (CFStringCompare(scheme, CFSTR("file"), 0) == kCFCompareEqualTo) { SInt32 length = CFDataGetLength(data); const void *bytes = (0 == length) ? (const void *)"" : CFDataGetBytePtr(data); Boolean atomicWriteSuccess = __CFWriteBytesToFileWithAtomicity(url, bytes, length, mode, true); if (atomicWriteSuccess) { CFRelease(scheme); *tryAgain = false; CFRelease(data); return true; } if (!atomicWriteSuccess && thread_errno() == ENOSPC) { CFRelease(scheme); *tryAgain = false; CFRelease(data); return false; } } CFRelease(scheme); } #endif success = CFURLWriteDataAndPropertiesToResource(url, data, URLPropertyDictForPOSIXMode(mode), NULL); URLPropertyDictRelease(); if (success) { CFDataRef readData; if (!CFURLCreateDataAndPropertiesFromResource(alloc, url, &readData, NULL, NULL, NULL) || !CFEqual(readData, data)) { success = false; *tryAgain = true; } if (readData) CFRelease(readData); } else { CFBooleanRef val = (CFBooleanRef) CFURLCreatePropertyFromResource(alloc, url, kCFURLFileExists, NULL); if (!val || !CFBooleanGetValue(val)) { CFURLRef tmpURL = CFURLCreateWithFileSystemPathRelativeToBase(alloc, CFSTR("."), kCFURLPOSIXPathStyle, true, url); // Just "." because url is not a directory URL CFURLRef parentURL = tmpURL ? CFURLCopyAbsoluteURL(tmpURL) : NULL; if (tmpURL) CFRelease(tmpURL); if (val) CFRelease(val); val = (CFBooleanRef) CFURLCreatePropertyFromResource(alloc, parentURL, kCFURLFileExists, NULL); if ((!val || !CFBooleanGetValue(val)) && _createDirectory(parentURL, isWorldReadable)) { // parent directory didn't exist; now it does; try again to write success = CFURLWriteDataAndPropertiesToResource(url, data, URLPropertyDictForPOSIXMode(mode), NULL); URLPropertyDictRelease(); if (success) { CFDataRef rdData; if (!CFURLCreateDataAndPropertiesFromResource(alloc, url, &rdData, NULL, NULL, NULL) || !CFEqual(rdData, data)) { success = false; *tryAgain = true; } if (rdData) CFRelease(rdData); } } if (parentURL) CFRelease(parentURL); } if (val) CFRelease(val); } CFRelease(data); } else { // ??? This should never happen CFLog(__kCFLogAssertion, CFSTR("Could not generate XML data for property list")); success = false; } } return success; }