int btFiles::BuildFromFS(const char *pathname) { struct stat sb; BTFILE *pbf = (BTFILE*) 0; if( stat(pathname, &sb) < 0 ){ fprintf(stderr,"error, stat file %s failed, %s\n",pathname,strerror(errno)); return -1; } if( S_IFREG & sb.st_mode ){ pbf = _new_bfnode(); #ifndef WINDOWS if( !pbf ) return -1; #endif pbf->bf_length = m_total_files_length = sb.st_size; pbf->bf_filename = new char[strlen(pathname) + 1]; #ifndef WINDOWS if( !pbf->bf_filename ) return -1; #endif strcpy(pbf->bf_filename, pathname); m_btfhead = pbf; }else if( S_IFDIR & sb.st_mode ){ char wd[MAXPATHLEN]; #if HAVE_GETCWD if( !getcwd(wd,MAXPATHLEN) ) return -1; #else if( !getwd(wd) ) return -1; #endif m_directory = new char[strlen(pathname) + 1]; #ifndef WINDOWS if( !m_directory ) return -1; #endif strcpy(m_directory, pathname); if(chdir(m_directory) < 0){ fprintf(stderr,"error, change work directory to %s failed, %s",m_directory, strerror(errno)); return -1; } if(_btf_recurses_directory((const char*)0, (BTFILE*) 0) < 0) return -1; if( chdir(wd) < 0) return -1; }else{ fprintf(stderr,"error, %s is not a directory or regular file.\n",pathname); return -1; } return 0; }
int btFiles::BuildFromFS(const char *pathname) { struct stat sb; BTFILE *pbf = (BTFILE*) 0; BTFILE *lastnode = (BTFILE*) 0; if( stat(pathname, &sb) < 0 ){ CONSOLE.Warning(1, "error, stat file \"%s\" failed: %s", pathname, strerror(errno)); return -1; } if( S_IFREG & sb.st_mode ){ pbf = _new_bfnode(); #ifndef WINDOWS if( !pbf ) return -1; #endif pbf->bf_length = m_total_files_length = sb.st_size; pbf->bf_filename = new char[strlen(pathname) + 1]; #ifndef WINDOWS if( !pbf->bf_filename ) return -1; #endif strcpy(pbf->bf_filename, pathname); m_btfhead = pbf; }else if( S_IFDIR & sb.st_mode ){ char wd[MAXPATHLEN]; if( !getcwd(wd,MAXPATHLEN) ) return -1; m_directory = new char[strlen(pathname) + 1]; #ifndef WINDOWS if( !m_directory ) return -1; #endif strcpy(m_directory, pathname); if(chdir(m_directory) < 0){ CONSOLE.Warning(1, "error, change work directory to \"%s\" failed: %s", m_directory, strerror(errno)); return -1; } if(_btf_recurses_directory((const char*)0, &lastnode) < 0) return -1; if( chdir(wd) < 0) return -1; }else{ CONSOLE.Warning(1, "error, \"%s\" is not a directory or regular file.", pathname); return -1; } return 0; }
int btFiles::_btf_recurses_directory(const char *cur_path, BTFILE* *plastnode) { char full_cur[MAXPATHLEN]; char fn[MAXPATHLEN]; struct stat sb; struct dirent *dirp; DIR *dp; BTFILE *pbf; if( !getcwd(full_cur,MAXPATHLEN) ) return -1; if( cur_path ){ strcpy(fn, full_cur); if( MAXPATHLEN <= snprintf(full_cur, MAXPATHLEN, "%s%c%s", fn, PATH_SP, cur_path) ){ errno = ENAMETOOLONG; return -1; } } if( (DIR*) 0 == (dp = opendir(full_cur)) ){ CONSOLE.Warning(1, "error, open directory \"%s\" failed: %s", cur_path, strerror(errno)); return -1; } while( (struct dirent*) 0 != (dirp = readdir(dp)) ){ if( 0 == strcmp(dirp->d_name, ".") || 0 == strcmp(dirp->d_name, "..") ) continue; if( cur_path ){ if(MAXPATHLEN < snprintf(fn, MAXPATHLEN, "%s%c%s", cur_path, PATH_SP, dirp->d_name)){ CONSOLE.Warning(1, "error, pathname too long"); errno = ENAMETOOLONG; return -1; } }else{ strcpy(fn, dirp->d_name); } if( stat(fn, &sb) < 0 ){ CONSOLE.Warning(1, "error, stat \"%s\" failed: %s",fn,strerror(errno)); return -1; } if( S_IFREG & sb.st_mode ){ pbf = _new_bfnode(); #ifndef WINDOWS if( !pbf ){ errno = ENOMEM; return -1; } #endif pbf->bf_filename = new char[strlen(fn) + 1]; #ifndef WINDOWS if( !pbf->bf_filename ){ closedir(dp); errno = ENOMEM; return -1; } #endif strcpy(pbf->bf_filename, fn); pbf->bf_length = sb.st_size; m_total_files_length += sb.st_size; if( *plastnode ) (*plastnode)->bf_next = pbf; else m_btfhead = pbf; *plastnode = pbf; }else if( S_IFDIR & sb.st_mode ){ if(_btf_recurses_directory(fn, plastnode) < 0){closedir(dp); return -1;} }else{ CONSOLE.Warning(1, "error, \"%s\" is not a directory or regular file.", fn); closedir(dp); return -1; } } // end while closedir(dp); return 0; }
int btFiles::_btf_recurses_directory(const char *cur_path, BTFILE* lastnode) { char full_cur[MAXPATHLEN]; char fn[MAXPATHLEN]; struct stat sb; struct dirent *dirp; DIR *dp; BTFILE *pbf; #if HAVE_GETCWD if( !getcwd(full_cur,MAXPATHLEN) ) return -1; #else if( !getwd(full_cur) ) return -1; #endif if( cur_path ){ strcpy(fn, full_cur); if( MAXPATHLEN <= snprintf(full_cur, MAXPATHLEN, "%s%c%s", fn, PATH_SP, cur_path)) return -1; } if( (DIR*) 0 == (dp = opendir(full_cur))){ fprintf(stderr,"error, open directory %s failed, %s\n",cur_path,strerror(errno)); return -1; } while( (struct dirent*) 0 != (dirp = readdir(dp)) ){ if( 0 == strcmp(dirp->d_name, ".") || 0 == strcmp(dirp->d_name, "..") ) continue; if( cur_path ){ if(MAXPATHLEN < snprintf(fn, MAXPATHLEN, "%s%c%s", cur_path, PATH_SP, dirp->d_name)){ fprintf(stderr,"error, pathname too long\n"); return -1; } }else{ strcpy(fn, dirp->d_name); } if( stat(fn, &sb) < 0 ){ fprintf(stderr,"error, stat %s failed, %s\n",fn,strerror(errno)); return -1; } if( S_IFREG & sb.st_mode ){ pbf = _new_bfnode(); #ifndef WINDOWS if( !pbf ) return -1; #endif pbf->bf_filename = new char[strlen(fn) + 1]; #ifndef WINDOWS if( !pbf->bf_filename ){ closedir(dp); return -1;} #endif strcpy(pbf->bf_filename, fn); pbf->bf_length = sb.st_size; m_total_files_length += sb.st_size; if( lastnode ) lastnode->bf_next = pbf; else m_btfhead = pbf; lastnode = pbf; }else if( S_IFDIR & sb.st_mode ){ if(_btf_recurses_directory(fn, lastnode) < 0){closedir(dp); return -1;} }else{ fprintf(stderr,"error, %s is not a directory or regular file.\n",fn); closedir(dp); return -1; } } // end while closedir(dp); return 0; }