bool FileRepository::findFile(std::string &path, struct stat &s) { if (path.empty()) return false; if (path[0] != '/') { if (!g_context->getIncludePathArray().isNull()) { for (ArrayIter iter(g_context->getIncludePathArray()); iter; ++iter) { string ip = iter.second().toString().data(); string p; if (!ip.empty() && ip[ip.length() - 1] == '/') { p = ip + path; } else { p = ip + "/" + path; } if (fileStat(p, s) && !S_ISDIR(s.st_mode)) { path = p; return true; } } } string cwd = g_context->getCwd().data(); if (!cwd.empty() && cwd[cwd.length() - 1] == '/') { path = cwd + path; } else { path = cwd + "/" + path; } } return fileStat(path, s) && !S_ISDIR(s.st_mode); }
//static bool Misc::dir_Exists(const char* path){ if ( !path || !*path ) return false; struct cl_stat_t buf; int32_t ret = fileStat(path,&buf); return ( ret == 0); }
int64_t Misc::file_Size(const char* path){ struct cl_stat_t buf; if ( fileStat(path,&buf) == 0 ) return buf.st_size; else return -1; }
bool FileRepository::findFile(std::string &path, struct stat &s, const char *currentDir) { // Check working directory first since that's what php does if (fileStat(path, s)) { return true; } for (vector<string>::const_iterator it = RuntimeOption::IncludeSearchPaths.begin(); it != RuntimeOption::IncludeSearchPaths.end(); ++it) { string p; if ((*it)[0] == '.' && currentDir) { p = string(currentDir) + it->substr(1); } else { p = *it; } p += path; if (fileStat(p, s)) { path = p; return true; } } return false; }
arma::mat readHDF5(const std::string& file_name) { arma::mat HDF5; if (fileStat(file_name)) { HDF5.load(file_name, arma::hdf5_binary); } else { throw std::runtime_error("Problem with loading MDS input file " + file_name); } return HDF5; }
bool FileRepository::findFile(const StringData *path, struct stat *s) { if (isAuthoritativeRepo()) { { UnitMd5Map::const_accessor acc; if (s_unitMd5Map.find(acc, path)) { return acc->second.m_present; } } MD5 md5; const StringData* spath = StringData::GetStaticString(path); UnitMd5Map::accessor acc; if (s_unitMd5Map.insert(acc, spath)) { bool present = VM::Repo::get().findFile( path->data(), SourceRootInfo::GetCurrentSourceRoot(), md5); acc->second.m_present = present; acc->second.m_unitMd5 = md5; } return acc->second.m_present; } return fileStat(path->data(), s) && !S_ISDIR(s->st_mode); }
// =-=-=-=-=-=-=- // local function to handle call to stat via resource plugin int _rsFileStat( rsComm_t* _comm, fileStatInp_t* _stat_inp, rodsStat_t** _stat_out ) { struct stat myFileStat; memset( &myFileStat, 0, sizeof( myFileStat ) ); // =-=-=-=-=-=-=- // make call to stat via resource plugin irods::file_object_ptr file_obj( new irods::file_object( _comm, _stat_inp->objPath, _stat_inp->fileName, _stat_inp->rescId, 0, 0, 0 ) ); irods::error stat_err = fileStat( _comm, file_obj, &myFileStat ); // =-=-=-=-=-=-=- // log error if necessary if ( !stat_err.ok() ) { // irods::log(LOG_ERROR, stat_err.result()); return stat_err.code(); } // =-=-=-=-=-=-=- // convert unix stat struct to an irods stat struct *_stat_out = ( rodsStat_t* )malloc( sizeof( rodsStat_t ) ); int status = statToRodsStat( *_stat_out, &myFileStat ); // =-=-=-=-=-=-=- // manage error if necessary if ( status < 0 ) { free( *_stat_out ); *_stat_out = NULL; } return status; } // _rsFileStat
bool Misc::listFiles(const char* directory, std::vector<std::string>& files, bool fullPath){ //clear old files DIR* dir = opendir(directory); if ( dir == NULL ) return false; struct dirent* fl = readdir(dir); struct cl_stat_t buf; string path; while ( fl != NULL ){ path = string(directory) + "/" + fl->d_name; int32_t ret = fileStat(path.c_str(),&buf); if ( ret==0 && !(buf.st_mode & S_IFDIR) ) { if ( (strcmp(fl->d_name, ".")) && (strcmp(fl->d_name, "..")) ) { if ( fullPath ){ files.push_back(path); }else{ files.push_back(fl->d_name); } } } fl = readdir(dir); } closedir(dir); return true; }
// =-=-=-=-=-=-=- // int chkEmptyDir( rsComm_t* rsComm, const std::string& cacheDir, const std::string& hier ) { int status = 0; char childPath[MAX_NAME_LEN]; struct stat myFileStat; struct rodsDirent* myFileDirent = 0; // =-=-=-=-=-=-=- // call opendir via resource plugin irods::collection_object_ptr cacheDir_obj( new irods::collection_object( cacheDir, hier, 0, 0 ) ); irods::error opendir_err = fileOpendir( rsComm, cacheDir_obj ); // =-=-=-=-=-=-=- // don't log an error as this is part // of the functionality of this code if ( !opendir_err.ok() ) { return 0; } // =-=-=-=-=-=-=- // make call to readdir via resource plugin irods::error readdir_err = fileReaddir( rsComm, cacheDir_obj, &myFileDirent ); while ( readdir_err.ok() && 0 == readdir_err.code() ) { // =-=-=-=-=-=-=- // handle relative paths if ( strcmp( myFileDirent->d_name, "." ) == 0 || strcmp( myFileDirent->d_name, ".." ) == 0 ) { readdir_err = fileReaddir( rsComm, cacheDir_obj, &myFileDirent ); continue; } // =-=-=-=-=-=-=- // get status of path snprintf( childPath, MAX_NAME_LEN, "%s/%s", cacheDir.c_str(), myFileDirent->d_name ); irods::collection_object_ptr tmp_coll_obj( new irods::collection_object( childPath, hier, 0, 0 ) ); irods::error stat_err = fileStat( rsComm, tmp_coll_obj, &myFileStat ); // =-=-=-=-=-=-=- // handle hard error if ( stat_err.code() < 0 ) { rodsLog( LOG_ERROR, "chkEmptyDir: fileStat error for %s, status = %d", childPath, stat_err.code() ); break; } // =-=-=-=-=-=-=- // path exists if ( myFileStat.st_mode & S_IFREG ) { status = SYS_DIR_IN_VAULT_NOT_EMPTY; rodsLog( LOG_ERROR, "chkEmptyDir: file %s exists", childPath, status ); break; } // =-=-=-=-=-=-=- // recurse for another directory if ( myFileStat.st_mode & S_IFDIR ) { status = chkEmptyDir( rsComm, childPath, hier ); if ( status == SYS_DIR_IN_VAULT_NOT_EMPTY ) { rodsLog( LOG_ERROR, "chkEmptyDir: dir %s is not empty", childPath ); break; } } // =-=-=-=-=-=-=- // continue with child path readdir_err = fileReaddir( rsComm, cacheDir_obj, &myFileDirent ); } // while // =-=-=-=-=-=-=- // make call to closedir via resource plugin, log error if necessary irods::error closedir_err = fileClosedir( rsComm, cacheDir_obj ); if ( !closedir_err.ok() ) { std::stringstream msg; msg << "fileClosedir failed for ["; msg << cacheDir; msg << "]"; irods::error log_err = PASSMSG( msg.str(), closedir_err ); irods::log( log_err ); } if ( status != SYS_DIR_IN_VAULT_NOT_EMPTY ) { irods::collection_object_ptr coll_obj( new irods::collection_object( cacheDir, hier, 0, 0 ) ); irods::error rmdir_err = fileRmdir( rsComm, coll_obj ); if ( !rmdir_err.ok() ) { std::stringstream msg; msg << "fileRmdir failed for ["; msg << cacheDir; msg << "]"; irods::error err = PASSMSG( msg.str(), rmdir_err ); irods::log( err ); } status = 0; } return status; } // chkEmptyDir
// =-=-=-=-=-=-=- // mk the directory recursively int mkFileDirR( rsComm_t * rsComm, size_t startDirLen, const std::string& destDir, const std::string& hier, int mode ) { std::string physical_directory_prefix; if ( destDir.empty() ) { rodsLog( LOG_ERROR, "mkFileDirR called with empty dest directory" ); return SYS_INVALID_INPUT_PARAM ; } if ( destDir.size() < startDirLen ) { rodsLog( LOG_ERROR, "mkFileDirR called with a destDir: [%s]" "shorter than its startDirLen: [%ju]", destDir.c_str(), ( uintmax_t )startDirLen ); return SYS_INVALID_INPUT_PARAM; } if ( !rsComm ) { rodsLog( LOG_ERROR, "mkFileDirR called with null rsComm" ); return SYS_INVALID_INPUT_PARAM; } if ( isValidFilePath( destDir ) ) { std::string vault_path; irods::error err = irods::get_vault_path_for_hier_string( hier, vault_path ); if ( !err.ok() ) { rodsLog( LOG_ERROR, err.result().c_str() ); return err.code(); } if ( destDir.compare( 0, vault_path.size(), vault_path ) == 0 && ( destDir[ vault_path.size() ] == '/' || destDir.size() == vault_path.size() ) ) { physical_directory_prefix = vault_path; } } std::vector< std::string > directories_to_create; std::string physical_directory = destDir; if ( physical_directory[ physical_directory.size() - 1 ] == '/' ) { physical_directory.erase( physical_directory.size() - 1 ); } while ( physical_directory.size() > startDirLen ) { irods::collection_object_ptr tmp_coll_obj( new irods::collection_object( physical_directory, hier, 0, 0 ) ); struct stat statbuf; irods::error stat_err = fileStat( rsComm, tmp_coll_obj, &statbuf ); if ( stat_err.code() >= 0 ) { if ( statbuf.st_mode & S_IFDIR ) { break; } else { rodsLog( LOG_NOTICE, "mkFileDirR: A local non-directory %s already exists \n", physical_directory.c_str() ); return stat_err.code(); } } else { directories_to_create.push_back( physical_directory.substr( physical_directory_prefix.size() ) ); } /* Go backward */ size_t index_of_last_slash = physical_directory.rfind( '/', physical_directory.size() - 1 ); if ( std::string::npos != index_of_last_slash ) { physical_directory = physical_directory.substr( 0, index_of_last_slash ); } else { break; } } // while std::string irods_directory_prefix = "/"; irods_directory_prefix += getLocalZoneName(); /* Now we go forward and make the required dir */ while ( !directories_to_create.empty() ) { physical_directory = physical_directory_prefix; physical_directory += directories_to_create.back(); std::string irods_directory = irods_directory_prefix; irods_directory += directories_to_create.back(); directories_to_create.pop_back(); irods::collection_object_ptr tmp_coll_obj( new irods::collection_object( physical_directory, hier, mode, 0 ) ); irods::error mkdir_err = fileMkdir( rsComm, tmp_coll_obj ); if ( !mkdir_err.ok() && ( getErrno( mkdir_err.code() ) != EEXIST ) ) { // JMC - backport 4834 std::stringstream msg; msg << "fileMkdir for ["; msg << physical_directory; msg << "]"; irods::error ret_err = PASSMSG( msg.str(), mkdir_err ); irods::log( ret_err ); return mkdir_err.code(); } } return 0; }
struct dirent * readdir (DIR * dirp) { errno = 0; /* Check for valid DIR struct. */ if (!dirp) { errno = EFAULT; return NULL; } if (dirp->dd_dir.d_name != dirp->dd_dta.name) { /* The structure does not seem to be set up correctly. */ errno = EINVAL; return NULL; } bool bCallFindNext = true; if (dirp->dd_stat < 0) { /* We have already returned all files in the directory * (or the structure has an invalid dd_stat). */ return NULL; } else if (dirp->dd_stat == 0) { /* We haven't started the search yet. */ /* Start the search */ dirp->dd_handle = _findfirst (dirp->dd_name, &(dirp->dd_dta)); if (dirp->dd_handle == -1) { /* Whoops! Seems there are no files in that * directory. */ dirp->dd_stat = -1; } else { dirp->dd_stat = 1; } /* Dont call _findnext first time. */ bCallFindNext = false; } while (dirp->dd_stat > 0) { if (bCallFindNext) { /* Get the next search entry. */ if (_findnext (dirp->dd_handle, &(dirp->dd_dta))) { /* We are off the end or otherwise error. */ _findclose (dirp->dd_handle); dirp->dd_handle = -1; dirp->dd_stat = -1; return NULL; } else { /* Update the status to indicate the correct * number. */ dirp->dd_stat++; } } /* Successfully got an entry. Everything about the file is * already appropriately filled in except the length of the * file name. */ dirp->dd_dir.d_namlen = strlen (dirp->dd_dir.d_name); bool bThisFolderOrUpFolder = dirp->dd_dir.d_name[0] == '.' && (dirp->dd_dir.d_name[1] == 0 || (dirp->dd_dir.d_name[1] == '.' && dirp->dd_dir.d_name[2] == 0)); if (!bThisFolderOrUpFolder) { struct cl_stat_t buf; char buffer[CL_MAX_DIR]; size_t bl = strlen(dirp->dd_name)-strlen(DIRENT_SEARCH_SUFFIX); strncpy(buffer,dirp->dd_name,bl); buffer[bl]=0; strcat(buffer, dirp->dd_dir.d_name); if ( fileStat(buffer,&buf) == 0 ) { /* Finally we have a valid entry. */ return &dirp->dd_dir; } } /* Allow to find next file. */ bCallFindNext = true; } return NULL; }
DIR * opendir (const char *szPath) { DIR *nd; char szFullPath[CL_MAX_PATH]; errno = 0; if (!szPath) { errno = EFAULT; return NULL; } if (szPath[0] == '\0') { errno = ENOTDIR; return NULL; } /* Attempt to determine if the given path really is a directory. */ struct cl_stat_t rcs; if ( fileStat(szPath,&rcs) == -1) { /* call GetLastError for more error info */ errno = ENOENT; return NULL; } if (!(rcs.st_mode & _S_IFDIR)) { /* Error, entry exists but not a directory. */ errno = ENOTDIR; return NULL; } /* Make an absolute pathname. */ _realpath(szPath,szFullPath); /* Allocate enough space to store DIR structure and the complete * directory path given. */ //nd = (DIR *) malloc (sizeof (DIR) + _tcslen (szFullPath) + _tcslen (DIRENT_SLASH) + // _tcslen (DIRENT_SEARCH_SUFFIX)+1); nd = new DIR; if (!nd) { /* Error, out of memory. */ errno = ENOMEM; return NULL; } /* Create the search expression. */ strcpy (nd->dd_name, szFullPath); /* Add on a slash if the path does not end with one. */ if (nd->dd_name[0] != '\0' && nd->dd_name[strlen (nd->dd_name) - 1] != '/' && nd->dd_name[strlen (nd->dd_name) - 1] != '\\') { strcat (nd->dd_name, DIRENT_SLASH); } /* Add on the search pattern */ strcat (nd->dd_name, DIRENT_SEARCH_SUFFIX); /* Initialize handle to -1 so that a premature closedir doesn't try * to call _findclose on it. */ nd->dd_handle = -1; /* Initialize the status. */ nd->dd_stat = 0; /* Initialize the dirent structure. ino and reclen are invalid under * Win32, and name simply points at the appropriate part of the * findfirst_t structure. */ //nd->dd_dir.d_ino = 0; //nd->dd_dir.d_reclen = 0; nd->dd_dir.d_namlen = 0; nd->dd_dir.d_name = nd->dd_dta.name; return nd; }
// Use boost::program_options to parse command line input // This does pretty much all the parameter checking needed int parseCommandLine (int argc, char *argv[], po::variables_map& vm) { int failed = 0; //Required options po::options_description required("Required options"); required.add_options() ("kmers,k", po::value<std::string>(), "dsm kmer output file (not needed if using --mds_concat)") ("pheno,p", po::value<std::string>(), ".pheno metadata"); po::options_description mds("MDS options"); mds.add_options() ("output,o", po::value<std::string>(), "output prefix for new dsm file") ("no_mds", "do not perform MDS; output subsampled matrix instead") ("write_distances", "write csv of distance matrix") ("mds_concat", po::value<std::string>(), "list of subsampled matrices to use in MDS. Performs only MDS; implies --no_filtering") ("pc", po::value<int>()->default_value(pc_default), "number of principal coordinates to output") ("size", po::value<long int>()->default_value(size_default), "number of kmers to use in MDS") ("threads", po::value<int>()->default_value(1), ("number of threads. Suggested: " + std::to_string(std::thread::hardware_concurrency())).c_str()); //Optional filtering parameters //NB pval cutoffs are strings for display, and are converted to floats later po::options_description filtering("Filtering options"); filtering.add_options() ("no_filtering", "turn off all filtering and do not output new kmer file") ("max_length", po::value<long int>()->default_value(max_length_default), "maximum kmer length") ("maf", po::value<double>()->default_value(maf_default), "minimum kmer frequency") ("min_words", po::value<int>(), "minimum kmer occurences. Overrides --maf"); po::options_description other("Other options"); other.add_options() ("version", "prints version and exits") ("help,h", "full help message"); po::options_description all; all.add(required).add(mds).add(filtering).add(other); try { po::store(po::command_line_parser(argc, argv).options(all).run(), vm); if (vm.count("help")) { printHelp(all); failed = 1; } else if (vm.count("version")) { std::cout << VERSION << std::endl; failed = 1; } else { po::notify(vm); failed = 0; // Check input files exist, and can stat if ((vm.count("kmers") && !fileStat(vm["kmers"].as<std::string>())) || (vm.count("pheno") && !fileStat(vm["pheno"].as<std::string>()))) { failed = 1; } } } catch (po::error& e) { // Report errors from boost library std::cerr << "Error in command line input: " << e.what() << "\n"; std::cerr << "Run 'kmds --help' for full option listing\n\n"; std::cerr << required << "\n" << other << "\n"; failed = 1; } return failed; }
// =-=-=-=-=-=-=- // local function which handles removing directories via the resource plugin int _rsFileRmdir( rsComm_t* _comm, fileRmdirInp_t* _rmdir_inp ) { irods::collection_object_ptr coll_obj( new irods::collection_object( _rmdir_inp->dirName, _rmdir_inp->rescHier, 0, 0 ) ); if ( ( _rmdir_inp->flags & RMDIR_RECUR ) != 0 ) { // FIXME :: revisit this after moving to First class Objects // recursive. This is a very dangerous operation. curently // it is only used to remove cache directory of structured // files struct rodsDirent* myFileDirent = 0; // if it is not a cache dir, return an error as we only do this // for cache dirs at the moment. if ( strstr( _rmdir_inp->dirName, CACHE_DIR_STR ) == NULL ) { rodsLog( LOG_ERROR, "_rsFileRmdir: recursive rm of non cachedir path %s", _rmdir_inp->dirName ); return SYS_INVALID_FILE_PATH; } // call opendir via resource plugin irods::error opendir_err = fileOpendir( _comm, coll_obj ); // log an error, if any if ( !opendir_err.ok() ) { std::stringstream msg; msg << "fileOpendir failed for ["; msg << _rmdir_inp->dirName; msg << "]"; irods::error err = PASSMSG( msg.str(), opendir_err ); irods::log( err ); return opendir_err.code(); } // read the directory via resource plugin and either handle files or recurse into another directory irods::error readdir_err = fileReaddir( _comm, coll_obj, &myFileDirent ); while ( readdir_err.ok() && 0 == readdir_err.code() ) { struct stat statbuf; char myPath[MAX_NAME_LEN]; // handle relative paths if ( strcmp( myFileDirent->d_name, "." ) == 0 || strcmp( myFileDirent->d_name, ".." ) == 0 ) { readdir_err = fileReaddir( _comm, coll_obj, &myFileDirent ); continue; } // cache path name snprintf( myPath, MAX_NAME_LEN, "%s/%s", &_rmdir_inp->dirName[0], &myFileDirent->d_name[0] ); // =-=-=-=-=-=-=- // call stat via resource plugin, handle errors irods::collection_object_ptr myPath_obj( new irods::collection_object( myPath, _rmdir_inp->rescHier, 0, 0 ) ); irods::error stat_err = fileStat( _comm, myPath_obj, &statbuf ); if ( !stat_err.ok() ) { std::stringstream msg; msg << "fileStat failed for ["; msg << myPath; msg << "]"; irods::error log_err = PASSMSG( msg.str(), stat_err ); irods::log( log_err ); // call closedir via resource plugin, handle errors irods::error closedir_err = fileClosedir( _comm, myPath_obj ); if ( !closedir_err.ok() ) { std::stringstream msg; msg << "fileClosedir for ["; msg << myPath; msg << "]"; irods::error log_err = PASSMSG( msg.str(), closedir_err ); irods::log( log_err ); } return stat_err.code(); } // if !stat_err.ok() // filter based on stat results: file, dir, or error int status = 0; if ( ( statbuf.st_mode & S_IFREG ) != 0 ) { // handle case where file is found irods::error unlink_err = fileUnlink( _comm, myPath_obj ); status = unlink_err.code(); if ( !unlink_err.ok() ) { irods::error log_err = PASSMSG( "error during unlink.", unlink_err ); irods::log( log_err ); } } else if ( ( statbuf.st_mode & S_IFDIR ) != 0 ) { // handle case where directory is found fileRmdirInp_t myRmdirInp; memset( &myRmdirInp, 0, sizeof( myRmdirInp ) ); myRmdirInp.flags = _rmdir_inp->flags; rstrcpy( myRmdirInp.dirName, myPath, MAX_NAME_LEN ); rstrcpy( myRmdirInp.rescHier, _rmdir_inp->rescHier, MAX_NAME_LEN ); // RECURSE - call _rsFileRmdir on this new found directory status = _rsFileRmdir( _comm, &myRmdirInp ); } else { status = SYS_INVALID_FILE_PATH; rodsLog( LOG_NOTICE, "_rsFileRmdir: for %s, status = %d", myPath, status ); } // handle error condition on the above three cases if ( status < 0 ) { rodsLog( LOG_NOTICE, "_rsFileRmdir: rm of %s failed, status = %d", myPath, status ); // call closedir via resource plugin, handle errors irods::error closedir_err = fileClosedir( _comm, myPath_obj ); if ( !closedir_err.ok() ) { std::stringstream msg; msg << "fileClosedir failed for ["; msg << myPath; msg << "]"; irods::error log_err = PASSMSG( msg.str(), closedir_err ); irods::log( log_err ); } return status; } // if status < 0 // =-=-=-=-=-=-=- // continue readdir via resource plugin readdir_err = fileReaddir( _comm, coll_obj, &myFileDirent ); } // while // =-=-=-=-=-=-=- // call closedir via resource plugin, handle errors irods::error closedir_err = fileClosedir( _comm, coll_obj ); if ( !closedir_err.ok() ) { std::stringstream msg; msg << "fileClosedir failed for ["; msg << _rmdir_inp->dirName; msg << "]"; irods::error log_err = PASSMSG( msg.str(), closedir_err ); irods::log( log_err ); } } // if RMDIR_RECUR ( recursive delete ) // =-=-=-=-=-=-=- // make the call to rmdir via the resource plugin irods::error rmdir_err = fileRmdir( _comm, coll_obj ); if ( !rmdir_err.ok() ) { std::stringstream msg; msg << "fileRmdir failed for ["; msg << _rmdir_inp->dirName; msg << "]"; irods::error err = PASSMSG( msg.str(), rmdir_err ); irods::log( err ); } return rmdir_err.code(); } // _rsFileRmdir
int chkEmptyDir (int fileType, rsComm_t *rsComm, char *cacheDir) { void *dirPtr = NULL; #if defined(solaris_platform) char fileDirent[sizeof (struct dirent) + MAX_NAME_LEN]; struct dirent *myFileDirent = (struct dirent *) fileDirent; #else struct dirent fileDirent; struct dirent *myFileDirent = &fileDirent; #endif int status; char childPath[MAX_NAME_LEN]; struct stat myFileStat; status = fileOpendir ((fileDriverType_t)fileType, rsComm, cacheDir, &dirPtr); if (status < 0) { return (0); } while ((status = fileReaddir ((fileDriverType_t)fileType, rsComm, dirPtr, myFileDirent)) >= 0) { if (strcmp (myFileDirent->d_name, ".") == 0 || strcmp (myFileDirent->d_name, "..") == 0) { continue; } snprintf (childPath, MAX_NAME_LEN, "%s/%s", cacheDir, myFileDirent->d_name); status = fileStat ((fileDriverType_t)fileType, rsComm, childPath, &myFileStat); if (status < 0) { rodsLog (LOG_ERROR, "chkEmptyDir: fileStat error for %s, status = %d", childPath, status); break; } if (myFileStat.st_mode & S_IFREG) { rodsLog (LOG_ERROR, "chkEmptyDir: file %s exists", childPath, status); status = SYS_DIR_IN_VAULT_NOT_EMPTY; break; } if (myFileStat.st_mode & S_IFDIR) { status = chkEmptyDir ((fileDriverType_t)fileType, rsComm, childPath); if (status == SYS_DIR_IN_VAULT_NOT_EMPTY) { rodsLog (LOG_ERROR, "chkEmptyDir: dir %s is not empty", childPath); break; } } } fileClosedir ((fileDriverType_t)fileType, rsComm, dirPtr); if (status != SYS_DIR_IN_VAULT_NOT_EMPTY) { fileRmdir ((fileDriverType_t)fileType, rsComm, cacheDir); status = 0; } return status; }
int mkFileDirR (int fileType, rsComm_t *rsComm, char *startDir, char *destDir, int mode) { int status; int startLen; int pathLen, tmpLen; char tmpPath[MAX_NAME_LEN]; struct stat statbuf; #ifdef DIRECT_ACCESS_VAULT rodsHostAddr_t addr; rodsServerHost_t *rodsServerHost; char *zoneName; char *outVaultPath; int vp_len; char collName[MAX_NAME_LEN]; keyValPair_t condInput; #endif startLen = strlen (startDir); pathLen = strlen (destDir); rstrcpy (tmpPath, destDir, MAX_NAME_LEN); tmpLen = pathLen; while (tmpLen > startLen) { status = fileStat ( (fileDriverType_t)fileType, rsComm, tmpPath, &statbuf); if (status >= 0) { if (statbuf.st_mode & S_IFDIR) { break; } else { rodsLog (LOG_NOTICE, "mkFileDirR: A local non-directory %s already exists \n", tmpPath); return (status); } } /* Go backward */ while (tmpLen && tmpPath[tmpLen] != '/') tmpLen --; tmpPath[tmpLen] = '\0'; } #ifdef DIRECT_ACCESS_VAULT if (fileType == DIRECT_ACCESS_FILE_TYPE) { zoneName = getLocalZoneName(); addr.hostAddr[0] = '\0'; resolveHost(&addr, &rodsServerHost); vp_len = matchVaultPath(rsComm, destDir, rodsServerHost, &outVaultPath); if (vp_len == 0) { outVaultPath = NULL; } } #endif /* Now we go forward and make the required dir */ while (tmpLen < pathLen) { /* Put back the '/' */ tmpPath[tmpLen] = '/'; #ifdef DIRECT_ACCESS_VAULT memset(&condInput, 0, sizeof(condInput)); if (fileType == DIRECT_ACCESS_FILE_TYPE) { snprintf(collName, MAX_NAME_LEN, "/%s%s", zoneName, tmpPath + vp_len); status = rsQueryDirectoryMeta(rsComm, collName, &condInput); } status = fileMkdir ((fileDriverType_t)fileType, rsComm, tmpPath, mode, &condInput); #else status = fileMkdir ((fileDriverType_t)fileType, rsComm, tmpPath, mode, NULL); #endif if (status < 0 && (getErrno (status) != EEXIST)) { rodsLog (LOG_NOTICE, "mkFileDirR: mkdir failed for %s, status =%d", tmpPath, status); return status; } #if 0 /* a fix from AndyS */ while (tmpLen && tmpPath[tmpLen] != '\0') #endif while (tmpPath[tmpLen] != '\0') tmpLen ++; } return 0; }
int _rsChkNVPathPerm( rsComm_t *rsComm, fileOpenInp_t *chkNVPathPermInp ) { struct stat myFileStat; int sysUid; char tmpPath[MAX_NAME_LEN]; int len; char *tmpPtr; if ( chkNVPathPermInp->objPath[0] == '\0' ) { std::stringstream msg; msg << __FUNCTION__; msg << " - Empty logical path."; irods::log( LOG_ERROR, msg.str() ); return -1; } /* Need to match path's owner uid with sysUid */ sysUid = rsComm->clientUser.sysUid; if ( sysUid < 0 ) { /* have tried before */ return SYS_NO_PATH_PERMISSION; } else if ( sysUid == 0 ) { sysUid = rsComm->clientUser.sysUid = getUnixUid( rsComm->clientUser.userName ); if ( sysUid < 0 ) { rsComm->clientUser.sysUid = sysUid; return SYS_NO_PATH_PERMISSION; } } rstrcpy( tmpPath, chkNVPathPermInp->fileName, MAX_NAME_LEN ); len = strlen( tmpPath ); irods::error stat_err; while ( 1 ) { irods::file_object_ptr file_obj( new irods::file_object( rsComm, chkNVPathPermInp->objPath, tmpPath, chkNVPathPermInp->resc_hier_, 0, 0, 0 ) ); stat_err = fileStat( rsComm, file_obj, &myFileStat ); if ( stat_err.code() >= 0 ) { break; } else if ( errno == EEXIST || getErrno( stat_err.code() ) == EEXIST ) { /* go back */ tmpPtr = tmpPath + len; while ( len > 0 ) { len --; if ( *tmpPtr == '/' ) { *tmpPtr = '\0'; break; } tmpPtr--; } if ( len > 0 ) { /* give it more tries */ continue; } else { break; } } else { break; } } if ( stat_err.code() < 0 ) { return SYS_NO_PATH_PERMISSION; } if ( sysUid != ( int ) myFileStat.st_uid && ( myFileStat.st_mode & S_IWOTH ) == 0 ) { return SYS_NO_PATH_PERMISSION; } else { return 0; } }
bool FileRepository::findFile(const string &path, struct stat *s) { return fileStat(path.c_str(), s) && !S_ISDIR(s->st_mode); }
bool FileRepository::findFile(std::string &path, struct stat &s, const char *currentDir) { return fileStat(path, s); }