int main( int argc, char **argv ) { //int n; if( argc < 3 ) { printf( WIDE("Must specify two directories to compare...\n")); return 0; } tree1 = CreateBinaryTreeExx( 0, MyStrCmp, NULL ); tree2 = CreateBinaryTreeExx( 0, MyStrCmp, NULL ); nRootLen1 = strlen( argv[1] ); nRootLen2 = strlen( argv[2] ); { void *info = NULL; while( ScanFiles( argv[1], WIDE("*"), &info, AddDir, SFF_DIRECTORIES|SFF_SUBCURSE, 0 ) ); } BalanceBinaryTree( tree1 ); printf( WIDE("Files not in first tree...\n") ); { void *info = NULL; while( ScanFiles( argv[2], WIDE("*"), &info, FindDir, SFF_DIRECTORIES|SFF_SUBCURSE, 0 ) ); } BalanceBinaryTree( tree2 ); printf( WIDE("Files not in second tree...\n") ); { void *info = NULL; while( ScanFiles( argv[1], WIDE("*"), &info, FindDir2, SFF_DIRECTORIES|SFF_SUBCURSE, 0 ) ); } return 0; }
long Cx_StringTable::LoadFiles(const std::wstring& path) { long count = 0; std::vector<std::wstring> files; CScanFilesByExtension scaner(&files, L".xml"); ScanFiles(&scaner, path.c_str(), false); m_loaded = true; std::vector<std::wstring>::const_iterator it = files.begin(); for (; it != files.end(); ++it) { if (RegisterFile(*it) > 0) { count++; } } if (0 == count) { LOG_INFO2(LOGHEAD L"IDS_NO_STRFILE", path); } return count; }
void ScanHeaders( void (*proc)(char *filename, int flags) ) { void *info = NULL; while( ScanFiles( "../include" , "*.h" , &info , proc , SFF_DIRECTORIES | SFF_SUBCURSE ) ); }
/** * @brief Update library if new files found or updated */ static void UpdateLibrary( monitoring_thread_t *p_mon ) { int i_rows, i_cols, i; char **pp_results; media_library_t *p_ml = p_mon->p_ml; struct stat s_stat; bool b_recursive = var_GetBool( p_mon, "ml-recursive-scan" ); msg_Dbg( p_mon, "Scanning directories" ); Query( p_ml, &pp_results, &i_rows, &i_cols, "SELECT id AS directory_id, uri AS directory_uri, " "timestamp AS directory_ts FROM directories" ); msg_Dbg( p_mon, "%d directories to scan", i_rows ); for( i = 1; i <= i_rows; i++ ) { int id = atoi( pp_results[i*i_cols] ); char *psz_dir = pp_results[i*i_cols+1]; int timestamp = atoi( pp_results[i*i_cols+2] ); if( vlc_stat( psz_dir, &s_stat ) == -1 ) { int err = errno; if( err == ENOTDIR || err == ENOENT ) { msg_Dbg( p_mon, "Removing `%s'", psz_dir ); RemoveDirToMonitor( p_ml, psz_dir ); } else { msg_Err( p_mon, "%s: %m", psz_dir ); FreeSQLResult( p_ml, pp_results ); return; } errno = err; } if( !S_ISDIR( s_stat.st_mode ) ) { msg_Dbg( p_mon, "Removing `%s'", psz_dir ); RemoveDirToMonitor( p_ml, psz_dir ); } if( timestamp < s_stat.st_mtime ) { msg_Dbg( p_mon, "Adding `%s'", psz_dir ); ScanFiles( p_mon, id, b_recursive, NULL ); } } FreeSQLResult( p_ml, pp_results ); }
int GetMatchingFileName ( CTEXTSTR filemask, int flags, TEXTSTR pResult, int nResult ) { void *info = NULL; RESULT_BUFFER result_buf; result_buf.buffer = pResult; result_buf.len = nResult; result_buf.result_len = 0; // may need a while loop here... // but I'm just going to result the first matching anyhow. while( ScanFiles( NULL, filemask, &info, MatchFile, flags, (uintptr_t)&result_buf ) ); return result_buf.result_len; }
//--------------< scan server files >------------ std::string ScanFiles(std::string dir) { std::vector<std::string> files = FileSystem::Directory::getFiles(dir); std::vector<std::string> dirs = FileSystem::Directory::getDirectories(dir); std::string infoString; for(int i=0; i < files.size(); i++) { infoString.append("<A HREF=\""+dir+"\\"+files[i]+"\""+" target=\"_blank\">" +files[i]+"</A><br>\n"); } for(int i=0; i<dirs.size(); i++) { infoString.append(ScanFiles(dir+"\\"+dirs[i])); } return infoString; }
//----------------< Do Post Process >-------------------------------- // void PostHandler::DoProcess() { setProtocal(this->_http_Request._protocal); std::string action; //check the action if (this->_http_Request._relativeURL.find("message/") != std::string::npos) { //this is message action action = "message"; /* Put the code of how to handle the message */ std::cout<<"the message is:\n"; for(int i=0; i<_http_Request._content_Length; i++) std::cout<<this->_http_Request._requestContent[i]; std::cout<<"\n"; } else if (this->_http_Request._relativeURL.find("files/") != std::string::npos) { //this is a file action action="file"; ResourceSave(this->_http_Request._relativeURL, this->_http_Request._requestContent, this->_http_Request._content_Length); /* write the file info to the info file */ std::ofstream infofile("info.txt"); infofile<<ScanFiles("files/"); infofile.close(); } else { //handle the unknown action this->setContentType("text/html"); std::string msgBody("<p>post failure</p>"); this->setContentLength(msgBody.size()); char *msg = new char[msgBody.size()]; for(size_t i=0; i<msgBody.size(); i++) msg[i]=msgBody[i]; this->setMsgBody(msg); setStatus("404 Bad"); _hasProcessed = true; return; } this->setContentType("text/html"); std::string msgBody("<p>"+action+" posted successfully</p>"); this->setContentLength(msgBody.size()); char *msg = new char[msgBody.size()]; for(size_t i=0; i<msgBody.size(); i++) msg[i]=msgBody[i]; this->setMsgBody(msg); setStatus("200 OK"); _hasProcessed = true; return; }
int main( void ) { void *info = NULL; uint32_t free, used, chuncks, freechunks; SetSystemLog( SYSLOG_FILE, stdout ); printf( WIDE("Lets see... ini files...") ); while( ScanFiles( WIDE("."), WIDE("*.ini"), &info, ParseINI, 0, 0 ) ); GetMemStats( &free, &used, &chuncks, &freechunks ); printf( WIDE("Memory result : free:%ld used:%ld chuncks:%ld freechunks:%ld\n") , free, used, chuncks, freechunks ); DumpRegisteredNames(); SaveTree(); LoadTree(); DumpRegisteredNames(); return 0; }
void Dialog::readyUdpRead() { // when data comes in quint16 op = 0; QByteArray buffer; buffer.resize(m_socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; m_socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); QDataStream stream( &buffer, QIODevice::ReadWrite ); stream >> op >> dirString >> m_malwareFile; readDir(dirString); readSignature(m_malwareFile); ScanFiles(); }
int CTorrentBuilder::Run() { if ( m_pSection.Lock() ) { m_nTotalSize = 0; m_nTotalPos = 0; m_pFileSize = NULL; m_pFileSHA1 = NULL; m_pFileED2K = NULL; m_pFileMD5 = NULL; m_pPieceSHA1 = NULL; m_pSection.Unlock(); } if ( ScanFiles() && ! m_bAbort ) { if ( ProcessFiles() && WriteOutput() ) m_bFinished = TRUE; } if ( m_pSection.Lock() ) { delete [] m_pPieceSHA1; m_pPieceSHA1 = NULL; delete [] m_pFileMD5; m_pFileMD5 = NULL; delete [] m_pFileED2K; m_pFileED2K = NULL; delete [] m_pFileSHA1; m_pFileSHA1 = NULL; delete [] m_pFileSize; m_pFileSize = NULL; m_sThisFile.Empty(); m_bActive = FALSE; m_pSection.Unlock(); } return 0; }
static bool ScanDirectories(File::Visitor &visitor, bool recursive, Path sPath, const TCHAR* filter = _T("*")) { #ifdef HAVE_POSIX DIR *dir = opendir(sPath.c_str()); if (dir == nullptr) return false; TCHAR FileName[MAX_PATH]; _tcscpy(FileName, sPath.c_str()); size_t FileNameLength = _tcslen(FileName); FileName[FileNameLength++] = '/'; struct dirent *ent; while ((ent = readdir(dir)) != nullptr) { // omit '.', '..' and any other files/directories starting with '.' if (*ent->d_name == _T('.')) continue; _tcscpy(FileName + FileNameLength, ent->d_name); struct stat st; if (stat(FileName, &st) < 0) continue; if (S_ISDIR(st.st_mode) && recursive) ScanDirectories(visitor, true, Path(FileName), filter); else { int flags = 0; #ifdef FNM_CASEFOLD flags = FNM_CASEFOLD; #endif if (S_ISREG(st.st_mode) && fnmatch(filter, ent->d_name, flags) == 0) visitor.Visit(Path(FileName), Path(ent->d_name)); } } closedir(dir); #else /* !HAVE_POSIX */ TCHAR DirPath[MAX_PATH]; TCHAR FileName[MAX_PATH]; if (sPath != nullptr) { // e.g. "/test/data/something" _tcscpy(DirPath, sPath.c_str()); _tcscpy(FileName, sPath.c_str()); } else { DirPath[0] = 0; FileName[0] = 0; } // Scan for files in "/test/data/something" ScanFiles(visitor, Path(FileName), filter); // If we are not scanning recursive we are done now if (!recursive) return true; // "test/data/something/" _tcscat(DirPath, _T(DIR_SEPARATOR_S)); // "test/data/something/*" _tcscat(FileName, _T(DIR_SEPARATOR_S "*")); // Find the first file WIN32_FIND_DATA FindFileData; HANDLE hFind = FindFirstFile(FileName, &FindFileData); // If no file found -> return false if (hFind == INVALID_HANDLE_VALUE) return false; // Loop through remaining files while (true) { if (!IsDots(FindFileData.cFileName) && (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { // "test/data/something/" _tcscpy(FileName, DirPath); // "test/data/something/SUBFOLDER" _tcscat(FileName, FindFileData.cFileName); // Scan subfolder for matching files too ScanDirectories(visitor, true, Path(FileName), filter); } // Look for next file/folder if (!FindNextFile(hFind, &FindFileData)) { if (GetLastError() == ERROR_NO_MORE_FILES) // No more files/folders // -> Jump out of the loop break; else { // Some error occured // -> Close the handle and return false FindClose(hFind); return false; } } } // Close the file handle FindClose(hFind); #endif /* !HAVE_POSIX */ return true; }
/** * @brief Scan files in a particular directory */ static void ScanFiles( monitoring_thread_t *p_mon, int i_dir_id, bool b_recursive, stat_list_t *stparent ) { int i_rows, i_cols, i_dir_content, i, i_mon_rows, i_mon_cols; char **ppsz_monitored_files; char **pp_results, *psz_dir; char **pp_dir_content; bool *pb_processed; input_item_t *p_input; struct stat s_stat; media_library_t *p_ml = (media_library_t *)p_mon->p_ml; Query( p_ml, &pp_results, &i_rows, &i_cols, "SELECT uri AS directory_uri FROM directories WHERE id = '%d'", i_dir_id ); if( i_rows < 1 ) { msg_Dbg( p_mon, "query returned no directory for dir_id: %d (%s:%d)", i_dir_id, __FILE__, __LINE__ ); return; } psz_dir = strdup( pp_results[1] ); FreeSQLResult( p_ml, pp_results ); struct stat_list_t stself; if( vlc_stat( psz_dir, &stself.st ) == -1 ) { msg_Err( p_ml, "Cannot stat `%s': %m", psz_dir ); free( psz_dir ); return; } #ifndef WIN32 for( stat_list_t *stats = stparent; stats != NULL; stats = stats->parent ) { if( ( stself.st.st_ino == stats->st.st_ino ) && ( stself.st.st_dev == stats->st.st_dev ) ) { msg_Warn( p_ml, "Ignoring infinitely recursive directory `%s'", psz_dir ); free( psz_dir ); return; } } #else /* Windows has st_dev (driver letter - 'A'), but it zeroes st_ino, * so that the test above will always incorrectly succeed. * Besides, Windows does not have dirfd(). */ #endif stself.parent = stparent; QuerySimple( p_ml, "UPDATE directories SET timestamp=%d WHERE id = %d", stself.st.st_mtime, i_dir_id ); Query( p_ml, &ppsz_monitored_files, &i_mon_rows, &i_mon_cols, "SELECT id AS media_id, timestamp AS media_ts, uri AS media_uri " "FROM media WHERE directory_id = %d", i_dir_id ); pb_processed = malloc(sizeof(bool) * i_mon_rows); for( i = 0; i < i_mon_rows ; i++) pb_processed[i] = false; i_dir_content = vlc_scandir( psz_dir, &pp_dir_content, NULL, Sort ); if( i_dir_content == -1 ) { msg_Err( p_mon, "Cannot read `%s': %m", psz_dir ); free( pb_processed ); free( psz_dir ); return; } else if( i_dir_content == 0 ) { msg_Dbg( p_mon, "Nothing in directory `%s'", psz_dir ); free( pb_processed ); free( psz_dir ); return; } for( i = 0; i < i_dir_content; i++ ) { const char *psz_entry = pp_dir_content[i]; if( psz_entry[0] != '.' ) { /* 7 is the size of "file://" */ char psz_uri[strlen(psz_dir) + strlen(psz_entry) + 2 + 7]; sprintf( psz_uri, "%s/%s", psz_dir, psz_entry ); if( vlc_stat( psz_uri, &s_stat ) == -1 ) { msg_Err( p_mon, "%s: %m", psz_uri ); free( pb_processed ); free( psz_dir ); return; } if( S_ISREG( s_stat.st_mode ) ) { const char *psz_dot = strrchr( psz_uri, '.' ); if( psz_dot++ && *psz_dot ) { int i_is_media = 0; for( int a = 0; ppsz_MediaExtensions[a]; a++ ) { if( !strcasecmp( psz_dot, ppsz_MediaExtensions[a] ) ) { i_is_media = 1; break; } } if( !i_is_media ) { msg_Dbg( p_mon, "ignoring file %s", psz_uri ); continue; } } char * psz_tmp = encode_URI_component( psz_uri ); char * psz_encoded_uri = ( char * )calloc( strlen( psz_tmp ) + 9, 1 ); strcpy( psz_encoded_uri, "file:///" ); strcat( psz_encoded_uri, psz_tmp ); free( psz_tmp ); /* Check if given media is already in DB and it has been updated */ bool b_skip = false; bool b_update = false; int j = 1; for( j = 1; j <= i_mon_rows; j++ ) { if( strcasecmp( ppsz_monitored_files[ j * i_mon_cols + 2 ], psz_encoded_uri ) != 0 ) continue; b_update = true; pb_processed[ j - 1 ] = true; if( atoi( ppsz_monitored_files[ j * i_mon_cols + 1 ] ) < s_stat.st_mtime ) { b_skip = false; break; } else { b_skip = true; break; } } msg_Dbg( p_ml , "Checking if %s is in DB. Found: %d", psz_encoded_uri, b_skip? 1 : 0 ); if( b_skip ) continue; p_input = input_item_New( psz_encoded_uri, psz_entry ); playlist_t* p_pl = pl_Get( p_mon ); preparsed_item_t* p_itemobject; p_itemobject = malloc( sizeof( preparsed_item_t ) ); p_itemobject->i_dir_id = i_dir_id; p_itemobject->psz_uri = psz_encoded_uri; p_itemobject->i_mtime = s_stat.st_mtime; p_itemobject->p_mon = p_mon; p_itemobject->b_update = b_update; p_itemobject->i_update_id = b_update ? atoi( ppsz_monitored_files[ j * i_mon_cols + 0 ] ) : 0 ; vlc_event_manager_t *p_em = &p_input->event_manager; vlc_event_attach( p_em, vlc_InputItemPreparsedChanged, PreparseComplete, p_itemobject ); playlist_PreparseEnqueue( p_pl, p_input ); } else if( S_ISDIR( s_stat.st_mode ) && b_recursive ) { Query( p_ml, &pp_results, &i_rows, &i_cols, "SELECT id AS directory_id FROM directories " "WHERE uri=%Q", psz_uri ); FreeSQLResult( p_ml, pp_results ); if( i_rows <= 0 ) { msg_Dbg( p_mon, "New directory `%s' in dir of id %d", psz_uri, i_dir_id ); QuerySimple( p_ml, "INSERT INTO directories (uri, timestamp, " "recursive) VALUES(%Q, 0, 1)", psz_uri ); // We get the id of the directory we've just added Query( p_ml, &pp_results, &i_rows, &i_cols, "SELECT id AS directory_id FROM directories WHERE uri=%Q", psz_uri ); if( i_rows <= 0 ) { msg_Err( p_mon, "Directory `%s' was not sucessfully" " added to the database", psz_uri ); FreeSQLResult( p_ml, pp_results ); continue; } ScanFiles( p_mon, atoi( pp_results[1] ), b_recursive, &stself ); FreeSQLResult( p_ml, pp_results ); } } } } vlc_array_t* delete_ids = vlc_array_new(); for( i = 0; i < i_mon_rows; i++ ) { if( !pb_processed[i] ) { /* This file doesn't exist anymore. Let's...urm...delete it. */ ml_element_t* find = ( ml_element_t* ) calloc( 1, sizeof( ml_element_t ) ); find->criteria = ML_ID; find->value.i = atoi( ppsz_monitored_files[ (i + 1) * i_mon_cols ] ); vlc_array_append( delete_ids, find ); } } /* Delete the unfound media */ if( Delete( p_ml, delete_ids ) != VLC_SUCCESS ) msg_Dbg( p_ml, "Something went wrong in multi delete" ); for( i = 0; i < vlc_array_count( delete_ids ); i++ ) { free( vlc_array_item_at_index( delete_ids, i ) ); } vlc_array_destroy( delete_ids ); FreeSQLResult( p_ml, ppsz_monitored_files ); for( i = 0; i < i_dir_content; i++ ) free( pp_dir_content[i] ); free( pp_dir_content ); free( psz_dir ); free( pb_processed ); }
int ScanFiles( char *base , char *mask , void **pInfo , void Process( char *name, int flags ) , int flags ) { int sendflags; struct stat st; if( !*pInfo ) { char findmask[256]; sprintf( findmask, "%s/*", base ); *pInfo = malloc( sizeof( MFD ) ); findhandle(pInfo) = opendir( base ); if( findhandle(pInfo) == NULL ) { free( *pInfo ); *pInfo = NULL; return 0; } } else { getnext: if( ( finddata( pInfo ) = readdir( findhandle(pInfo) ) ) == NULL ) { closedir( findhandle(pInfo) ); free( *pInfo ); *pInfo = NULL; return 0; } } if( !strcmp( ".", finddata(pInfo)->d_name ) || !strcmp( "..", finddata(pInfo)->d_name ) ) goto getnext; if( flags & SFF_NAMEONLY ) strncpy( findbuffer( pInfo ), finddata(pInfo)->d_name, MAX_PATH_NAME ); else snprintf( findbuffer( pInfo ), MAX_PATH_NAME, "%s/%s", base, finddata(pInfo)->d_name ); findbuffer( pInfo )[MAX_PATH_NAME-1] = 0; // force nul termination... stat( findbuffer( pInfo ), &st ); if( ( flags & (SFF_DIRECTORIES|SFF_SUBCURSE) ) && S_ISDIR(st.st_mode) ) { if( flags & SFF_SUBCURSE ) { void *data = NULL; if( flags & SFF_DIRECTORIES ) if( Process ) Process( findbuffer( pInfo ), SFF_DIRECTORY ); if( flags & SFF_NAMEONLY ) // if nameonly - have to rebuild the correct name. snprintf( findbuffer( pInfo ), MAX_PATH_NAME, "%s/%s", base, finddata(pInfo)->d_name ); while( ScanFiles( findbuffer(pInfo), mask, &data, Process, flags ) ); } goto getnext; } if( ( sendflags = SFF_DIRECTORY, ( ( flags & SFF_DIRECTORIES ) && ( S_ISDIR( st.st_mode ) ) ) ) || ( sendflags = 0, CompareMask( mask, finddata(pInfo)->d_name, 0 ) ) ) { if( Process ) Process( findbuffer( pInfo ), sendflags ); return 1; } return 1; }