void *file_list1st(const char *dir, FLINFO *fli) { FLISTH ret; Str255 fname; FSSpec fss; FSRef fsr; FSIterator fsi; mkstr255(fname, dir); if ((FSMakeFSSpec(0, 0, fname, &fss) != noErr) || (FSpMakeFSRef(&fss, &fsr) != noErr) || (FSOpenIterator(&fsr, kFSIterateFlat, &fsi) != noErr)) { goto ff1_err1; } ret = _MALLOC(sizeof(_FLHDL), dir); if (ret == NULL) { goto ff1_err2; } ((FLHDL)ret)->eoff = FALSE; ((FLHDL)ret)->fsi = fsi; if (file_listnext(ret, fli) == SUCCESS) { return(ret); } ff1_err2: FSCloseIterator(fsi); ff1_err1: return(NULL); }
void file_listclose(FLISTH hdl) { if (hdl) { FSCloseIterator(((FLHDL)hdl)->fsi); _MFREE(hdl); } }
/* Starts enumeration process. */ CHXDirectory::FSOBJ CHXDirectory::FindFirst(const char* szPattern, char* szPath, UINT16 nSize) { OSErr err; CHXDirSpecifier dirSpec(m_strPath); require(dirSpec.IsSet() && CHXFileSpecUtils::DirectoryExists(dirSpec), bail); // if there is already an iterator, dispose it if (m_FSIterator) { err = FSCloseIterator(m_FSIterator); check_noerr(err); m_FSIterator = 0; } err = FSOpenIterator((FSRef *) dirSpec, kFSIterateFlat, &m_FSIterator); require_noerr(err, bail); m_strFindPattern = szPattern; return FindNext(szPath, nSize); bail: return FSOBJ_NOTVALID; }
void wxDirData::Close() { if ( m_iterator ) { FSCloseIterator( m_iterator ) ; m_iterator = NULL ; } }
CHXDirectory::~CHXDirectory() { if (m_FSIterator) { (void) FSCloseIterator(m_FSIterator); m_FSIterator = 0; } }
void HLDirectory::FetchItems() { if(!mItemsFetched) { mItems = new ItemList; // make an FSIterator for this directory FSIterator theFSIterator; OSStatus theError = FSOpenIterator(&mFSRef, kFSIterateFlat, &theFSIterator); ThrowIfError(theError, CAException(theError), "HLDirectory::FetchItems: couldn't allocate the FSIterator"); try { // iterate through the items in the directory and get the FSRefs do { FSRef theFSRef; UInt32 theNumberItemsFetched = 0; theError = FSGetCatalogInfoBulk(theFSIterator, 1, &theNumberItemsFetched, NULL, kFSCatInfoNone, NULL, &theFSRef, NULL, NULL); ThrowIf((theError != 0) && (theError != errFSNoMoreItems), CAException(theError), "HLDirectory::FetchItems: couldn't get the catalog info"); if(theError != errFSNoMoreItems) { mItems->push_back(theFSRef); } } while(theError != errFSNoMoreItems); mItemsFetched = true; } catch(...) { FSCloseIterator(theFSIterator); throw; } // close the iterator FSCloseIterator(theFSIterator); } }
// // release the directory // void CMacFindFile::OS_CloseDirectory () { HX_VECTOR_DELETE(m_pszOutFileName); if (m_FSIterator) { OSErr err; err = FSCloseIterator(m_FSIterator); check_noerr(err); m_FSIterator = 0; } return; }
static OSStatus AddCCLsInFolderToArray(const FSRef *folderRef, CFMutableArrayRef result) // Iterates through the contents of the folder specified by folderRef, // adding the name of each CCL file (ie any file that's visible) to // the result array. { OSStatus err; OSStatus junk; FSRef * scriptRefs; HFSUniStr255 * scriptNames; FSIterator iter; ItemCount actCount; ItemCount thisItemIndex; Boolean done; iter = NULL; scriptRefs = NULL; scriptNames = NULL; // Allocate buffers for the FSRefs and long Unicode names. Given that // we're processing 50 files at a time, these buffers are way too big // to store on the stack (25 KB for scriptNames alone). scriptRefs = (FSRef *) malloc( kFolderItemsPerBulkCall * sizeof(FSRef) ); scriptNames = (HFSUniStr255 *) malloc( kFolderItemsPerBulkCall * sizeof(HFSUniStr255) ); err = noErr; if (scriptRefs == NULL || scriptNames == NULL) { err = memFullErr; } // Create the iterator. if (err == noErr) { err = FSOpenIterator(folderRef, kFSIterateFlat, &iter); } // Iterate over the contents of the directory. For each item found // we check whether it's a visible plain file and, if it is, add its // name to the array. if (err == noErr) { done = false; do { err = FSGetCatalogInfoBulk(iter, kFolderItemsPerBulkCall, &actCount, NULL, kFSCatInfoNone, NULL, scriptRefs, NULL, scriptNames); if (err == errFSNoMoreItems) { // We ran off the end of the directory. Record that we're // done, but set err to noErr so that we process any partial // results. done = true; err = noErr; } if (err == noErr) { for (thisItemIndex = 0; thisItemIndex < actCount; thisItemIndex++) { Boolean visible; err = MyIsVisibleFile(&scriptRefs[thisItemIndex], &visible); if ( (err == noErr) && visible ) { CFStringRef thisItemName; thisItemName = CFStringCreateWithCharacters(NULL, scriptNames[thisItemIndex].unicode, scriptNames[thisItemIndex].length); if (thisItemName == NULL) { err = coreFoundationUnknownErr; } if (err == noErr) { CFArrayAppendValue(result, thisItemName); } CFQRelease(thisItemName); } if (err != noErr) { break; } } } } while (err == noErr && !done); } // Clean up. if (iter != NULL) { junk = FSCloseIterator(iter); assert(junk == noErr); } if (scriptRefs != NULL) { free(scriptRefs); } if (scriptNames != NULL) { free(scriptNames); } return err; }
// Search through the directory specified by 'parent' for an item that appears to be a Second Life viewer. static OSErr findAppBundleOnDiskImage(FSRef *parent, FSRef *app) { FSIterator iterator; bool found = false; OSErr err = FSOpenIterator( parent, kFSIterateFlat, &iterator ); if(!err) { do { ItemCount actualObjects = 0; Boolean containerChanged = false; FSCatalogInfo info; FSRef ref; HFSUniStr255 unicodeName; err = FSGetCatalogInfoBulk( iterator, 1, &actualObjects, &containerChanged, kFSCatInfoNodeFlags, &info, &ref, NULL, &unicodeName ); if(actualObjects == 0) break; if(!err) { // Call succeeded and not done with the iteration. std::string name = HFSUniStr255_to_utf8str(&unicodeName); llinfos << "Considering \"" << name << "\"" << llendl; if(info.nodeFlags & kFSNodeIsDirectoryMask) { // This is a directory. See if it's a .app if(name.find(".app") != std::string::npos) { // Looks promising. Check to see if it has the right bundle identifier. if(isFSRefViewerBundle(&ref)) { llinfos << name << " is the one" << llendl; // This is the one. Return it. *app = ref; found = true; break; } else { llinfos << name << " is not the bundle we are looking for; move along" << llendl; } } } } } while(!err); llinfos << "closing the iterator" << llendl; FSCloseIterator(iterator); llinfos << "closed" << llendl; } if(!err && !found) err = fnfErr; return err; }
void SplitForks(FSRef * inFileReference,FSRef * inParentReference,Boolean inFirstLevel) { OSErr tErr; FSIterator tIterator; if (inFirstLevel==TRUE) { FSCatalogInfo tInfo; HFSUniStr255 tUnicodeFileName; // We need to split forks of the first level (and it allows us to check whether it's a folder or not) tErr=FSGetCatalogInfo(inFileReference,kFSCatInfoGettableInfo,&tInfo,&tUnicodeFileName,NULL,NULL); if (tErr==noErr) { // Check this is not a Hard Link if ((tInfo.nodeFlags & kFSNodeHardLinkMask)==0) { tErr=SplitFileIfNeeded(inFileReference,inParentReference,&tInfo,&tUnicodeFileName); if (tErr==noErr) { if (tInfo.nodeFlags & kFSNodeIsDirectoryMask) { // It's a folder // We need to proceed with the contents of the folder SplitForks(inFileReference,inParentReference,FALSE); } } else { exit(-1); } } } else { logerror("An error while getting Catalog Information for the File\n"); } return; } tErr=FSOpenIterator(inFileReference,kFSIterateFlat,&tIterator); if (tErr==noErr) { ItemCount tLookForItems,tFoundItems; FSCatalogInfo * tFoundCatInfo; FSRef * tFoundReferences; HFSUniStr255 * tFoundFileNames; FSRef tPreviousCacheRef; Boolean tPreviousCacheRefSet=FALSE; tLookForItems=1; tFoundCatInfo=(FSCatalogInfo *) malloc(tLookForItems*sizeof(FSCatalogInfo)); tFoundReferences=(FSRef *) malloc(tLookForItems*sizeof(FSRef)); tFoundFileNames=(HFSUniStr255 *) malloc(tLookForItems*sizeof(HFSUniStr255)); do { tErr=FSGetCatalogInfoBulk(tIterator, tLookForItems, &tFoundItems, NULL, kFSCatInfoGettableInfo, tFoundCatInfo, tFoundReferences, NULL, tFoundFileNames); if (tErr==noErr) { if (tPreviousCacheRefSet==TRUE) { if (FSCompareFSRefs(&tPreviousCacheRef,&tFoundReferences[0])==noErr) { // We need to skip the file since this is the one we processed previously continue; } } else { tPreviousCacheRefSet=TRUE; } tPreviousCacheRef=tFoundReferences[0]; // Check this is not a Hard Link if ((tFoundCatInfo[0].nodeFlags & kFSNodeHardLinkMask)==0) { tErr=SplitFileIfNeeded(&tFoundReferences[0],inFileReference,&tFoundCatInfo[0],&tFoundFileNames[0]); if (tErr==noErr) { if (tFoundCatInfo[0].nodeFlags & kFSNodeIsDirectoryMask) { // 2. We need to proceed with the contents of the folder SplitForks(&tFoundReferences[0],inFileReference,FALSE); } } else { exit(-1); } } } else { // A COMPLETER } } while (tErr==noErr); free(tFoundCatInfo); free(tFoundReferences); free(tFoundFileNames); FSCloseIterator (tIterator); } if (tErr!=noErr) { switch(tErr) { case errFSNoMoreItems: // No more items in the folder, this is perfectly ok break; case afpAccessDenied: break; default: // A COMPLETER break; } } }
void TTFoundationLoadExternalClassesFromFolder(const TTString& fullpath) { #ifdef TT_PLATFORM_MAC FSRef ref; Boolean isDirectory; OSStatus status = noErr; ItemCount count = 0; FSIterator iterator; HFSUniStr255* names = NULL; CFStringRef name; char cname[4096]; TTString path; TTCString cpath = (char*)fullpath.c_str(); void* handle; TTExtensionInitializationMethod initializer; TTErr err; status = FSPathMakeRef((UInt8*)cpath, &ref, &isDirectory); if (status != noErr) { #ifdef TT_DEBUG TTLogMessage("TTFoundation - no extensions location found @ %s\n", cpath); #endif return; } status = FSOpenIterator(&ref, kFSIterateFlat, &iterator); if (!status) { names = (HFSUniStr255 *)malloc(sizeof(HFSUniStr255) * 4096); if (names) { // Request information about files in the given directory, // until we get a status code back from the File Manager do{ status = FSGetCatalogInfoBulk(iterator, 4096, &count, NULL, kFSCatInfoNone, NULL, NULL, NULL, names); // Process all items received if (status == OSStatus(noErr) || status == OSStatus(errFSNoMoreItems)) { for (UInt32 i=0; i < count; i += 1) { name = CFStringCreateWithCharacters(kCFAllocatorDefault, names[i].unicode, names[i].length); // TODO: filter on name. We only want to try and load .ttdylib files CFStringGetCString(name, cname, 4096, kCFStringEncodingUTF8); path = fullpath; path += "/"; path += cname; handle = dlopen(path.c_str(), RTLD_LAZY); // TODO: assert -- or at least do a log post -- if handle is NULL initializer = (TTExtensionInitializationMethod)dlsym(handle, "loadTTExtension"); if (initializer) err = initializer(); CFRelease(name); } } } while (status == OSStatus(noErr)); // errFSNoMoreItems tells us we have successfully processed all // items in the directory -- not really an error if (status == OSStatus(errFSNoMoreItems)) status = noErr; // Free the array memory free( (void *) names ); } FSCloseIterator(iterator); } #elif TT_PLATFORM_WIN HANDLE fdHandle; WIN32_FIND_DATA findFileData; TTString path; HANDLE hLib = NULL; TTExtensionInitializationMethod initializer; TTErr err; path = fullpath; path += "*.ttdll"; fdHandle = FindFirstFile(path.c_str(), &findFileData); if (fdHandle && (fdHandle != INVALID_HANDLE_VALUE)) { while (fdHandle) { path = fullpath; path += findFileData.cFileName; hLib = LoadLibrary(path.c_str()); if (hLib) { initializer = (TTExtensionInitializationMethod)GetProcAddress((HMODULE)hLib, "loadTTExtension"); if (initializer) err = initializer(); } if (!FindNextFile(fdHandle, &findFileData)) break; } } #else ; #endif }
int ListTrackFiles (FSVolumeRefNum theVolume, FSRef *trackFiles, int numTracks) { OSStatus result = -1; FSIterator iterator; ItemCount actualObjects; FSRef rootDirectory; FSRef ref; HFSUniStr255 nameStr; result = FSGetVolumeInfo (theVolume, 0, NULL, kFSVolInfoFSInfo, NULL, NULL, &rootDirectory); if (result != noErr) { SDL_SetError ("ListTrackFiles: FSGetVolumeInfo returned %d", result); return result; } result = FSOpenIterator (&rootDirectory, kFSIterateFlat, &iterator); if (result == noErr) { do { result = FSGetCatalogInfoBulk (iterator, 1, &actualObjects, NULL, kFSCatInfoNone, NULL, &ref, NULL, &nameStr); if (result == noErr) { CFStringRef name; name = CFStringCreateWithCharacters (NULL, nameStr.unicode, nameStr.length); if (CFStringHasSuffix (name, CFSTR(".aiff")) || CFStringHasSuffix (name, CFSTR(".cdda"))) { int trackID = 0, i = 0; while (i < nameStr.length && !isdigit(nameStr.unicode[i])) { ++i; } while (i < nameStr.length && isdigit(nameStr.unicode[i])) { trackID = 10 * trackID +(nameStr.unicode[i] - '0'); ++i; } #if DEBUG_CDROM printf("Found AIFF for track %d: '%s'\n", trackID, CFStringGetCStringPtr (name, CFStringGetSystemEncoding())); #endif trackID--; assert(0 <= trackID && trackID <= SDL_MAX_TRACKS); if (trackID < numTracks) memcpy (&trackFiles[trackID], &ref, sizeof(FSRef)); } CFRelease (name); } } while(noErr == result); FSCloseIterator (iterator); } return 0; }