int make_dir(const char *path, int full, int mode) { char buf[4096]; const char *ps, *p; int len; if (path == NULL) return 0; ps = path + 1; while ((p = strchr(ps, '/'))) { len = p - path + 1; snprintf(buf, len, "%s", path); ps = p + 1; if (!stat_file(buf)) { if (mkdir(buf, mode) && errno != EEXIST) { logger(-1, errno, "Cannot create the directory %s", buf); return 1; } } } if (!full) return 0; if (!stat_file(path)) { if (mkdir(path, mode) && errno != EEXIST) { logger(-1, errno, "Cannot create the directory %s", path); return 1; } } return 0; }
//------------------------------------------------------------------- bool LogListHelper::_get_log_time_size(const char* path, const char* file, time_t& date, uint32_t& size) { if(file && file[0] && strstr(file, ".px4log")) { // Convert "log000" to 00:00 (minute per flight in session) if (strncmp(file, "log", 3) == 0) { unsigned u; if(sscanf(&file[3], "%u", &u) == 1) { date += (u * 60); if (stat_file(path, 0, &size)) { return true; } } } else { if (stat_file(path, &date, &size)) { return true; } /* strptime not available for some reason // Get time from file name struct tm tt; if(strptime(file, "%H_%M_%S", &tt)) { date += mktime(&tt); return true; } */ } } return false; }
int set_attr(const char *file, struct hostfs_iattr *attrs) { struct utimbuf buf; int err, ma; if(attrs->ia_valid & HOSTFS_ATTR_MODE){ if(chmod(file, attrs->ia_mode) != 0) return(-errno); } if(attrs->ia_valid & HOSTFS_ATTR_UID){ if(chown(file, attrs->ia_uid, -1)) return(-errno); } if(attrs->ia_valid & HOSTFS_ATTR_GID){ if(chown(file, -1, attrs->ia_gid)) return(-errno); } if(attrs->ia_valid & HOSTFS_ATTR_SIZE){ if(truncate(file, attrs->ia_size)) return(-errno); } ma = HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET; if((attrs->ia_valid & ma) == ma){ buf.actime = attrs->ia_atime.tv_sec; buf.modtime = attrs->ia_mtime.tv_sec; if(utime(file, &buf) != 0) return(-errno); } else { struct timespec ts; if(attrs->ia_valid & HOSTFS_ATTR_ATIME_SET){ err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &ts, NULL, NULL, NULL); if(err != 0) return(err); buf.actime = attrs->ia_atime.tv_sec; buf.modtime = ts.tv_sec; if(utime(file, &buf) != 0) return(-errno); } if(attrs->ia_valid & HOSTFS_ATTR_MTIME_SET){ err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, &ts, NULL, NULL, NULL, NULL); if(err != 0) return(err); buf.actime = ts.tv_sec; buf.modtime = attrs->ia_mtime.tv_sec; if(utime(file, &buf) != 0) return(-errno); } } if(attrs->ia_valid & HOSTFS_ATTR_CTIME) ; if(attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)){ err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, &attrs->ia_atime, &attrs->ia_mtime, NULL, NULL, NULL); if(err != 0) return(err); } return(0); }
int vzctl2_get_env_status_info(struct vzctl_env_handle *h, vzctl_env_status_t *status, int mask) { int ret, exists = 0; char path[512]; const char *ve_private = h->env_param->fs->ve_private; const char *ve_root = h->env_param->fs->ve_root; memset(status, 0, sizeof(vzctl_env_status_t)); /* get running state */ if (mask & ENV_STATUS_RUNNING) { if (is_env_run(h) == 1) { status->mask |= ENV_STATUS_RUNNING; get_env_ops()->env_get_cpt_state(h, &status->mask); } } /* do exit if only running status requested */ if (mask == ENV_STATUS_RUNNING) return 0; if ((ret = check_var(ve_private, "VE_PRIVATE not set"))) return ret; vzctl2_get_env_conf_path(EID(h), path, sizeof(path)); if (stat_file(path) == 1 && stat_file(ve_private) == 1) { if (mask & ENV_SKIP_OWNER) exists = 1; else if (vzctl2_check_owner(ve_private) == 0) exists = 1; } if ((mask & ENV_STATUS_EXISTS) && exists) status->mask |= ENV_STATUS_EXISTS; /* get mounted state */ if (mask & (ENV_STATUS_MOUNTED | ENV_STATUS_MOUNTED_FAST) && exists) { if ((ret = check_var(ve_root, "VE_ROOT not set"))) return ret; ret = (mask & ENV_STATUS_MOUNTED) ? vzctl2_env_is_mounted(h) : fs_is_mounted_check_by_target(ve_root); if (ret == 1) status->mask |= ENV_STATUS_MOUNTED; } /* get suspended state */ if ((mask & ENV_STATUS_SUSPENDED) && exists && !(status->mask & ENV_STATUS_RUNNING)) { vzctl2_get_dump_file(h, path, sizeof(path)); if (stat_file(path) == 1) status->mask |= ENV_STATUS_SUSPENDED; } read_env_transition(EID(h), h->env_param->opts->lockdir, status); return 0; }
/** Get id by name and check VEID.conf consistensy with name * * @param name Container name in UTF8 encoding. * @param ctid return CTID. * @return -1 if no name or conflict. */ int vzctl2_get_envid_by_name(const char *name, ctid_t ctid) { char buf[PATH_MAX]; int rc; int id_by_ctid = 0; const char *id = NULL; struct vzctl_env_handle *h; /* 1. /etc/vz/conf/CTID.conf */ if (vzctl2_parse_ctid(name, ctid) == 0) { vzctl2_get_env_conf_path(ctid, buf, sizeof(buf)); rc = stat_file(buf); if (rc == -1) return -1; else if (rc == 1) id_by_ctid = 1; } /* 2. /etc/vz/name/name */ snprintf(buf, sizeof(buf), ENV_NAME_DIR "%s", name); rc = stat_file(buf); if (rc == -1) return -1; else if (rc == 0) return id_by_ctid ? 0 : -1; h = vzctl2_env_open_conf(NULL, buf, VZCTL_CONF_SKIP_GLOBAL, &rc); if (h == NULL) return -1; /* get CTID from VEID variable */ rc = -1; if (h->env_param->name->name == NULL || strcmp(h->env_param->name->name, name)) goto err; vzctl2_env_get_param(h, "VEID", &id); if (vzctl2_parse_ctid(id, ctid)) { logger(-1, 0, "Unable to get ctid by name %s: " "invalid VEID=%s", name, id); goto err; } /* Return ctid by name unconditionally * Ignore id_by_ctid result */ rc = 0; err: vzctl2_env_close(h); return rc; }
std::int64_t stat_cache::get_filesize(int const i, file_storage const& fs , std::string const& save_path, error_code& ec) { TORRENT_ASSERT(i < int(fs.num_files())); if (i >= int(m_stat_cache.size())) m_stat_cache.resize(i + 1, not_in_cache); std::int64_t sz = m_stat_cache[i].file_size; if (sz < not_in_cache) { ec = m_errors[-sz + file_error]; return file_error; } else if (sz == not_in_cache) { // query the filesystem file_status s; std::string const file_path = fs.file_path(i, save_path); stat_file(file_path, &s, ec); if (ec) { set_error(i, ec); sz = file_error; } else { set_cache(i, s.file_size); sz = s.file_size; } } return sz; }
void runStatTests(){ stat_file(); stat_largeFile(); stat_dir(); stat_enoent(); stat_enotdir(); }
int read_service_name(char *path, char *service_name, int size) { char buf[4096]; char *p; int fd, len; snprintf(buf, sizeof(buf), "%s/" VZCTL_VE_CLUSTER_SERVICE_NAME, path); if (stat_file(buf) == 0) { service_name[0] = 0; return 0; } if ((fd = open(buf, O_RDONLY)) == -1) { logger(-1, errno, "Unable to open %s", buf); return -1; } len = read(fd, service_name, size - 1); close(fd); if (len == -1) { logger(-1, errno, "Unable to read from %s", buf); return -1; } service_name[len] = 0; if ((p = strrchr(buf, '\n')) != NULL) *p = 0; return 0; }
int main(int argc, char ** argv) { int opt; while ((opt = getopt(argc, argv, "?Lq")) != -1) { switch (opt) { case 'L': dereference = 1; break; case 'q': quiet = 1; break; case '?': show_usage(argc,argv); return 1; } } if (optind >= argc) { show_usage(argc, argv); return 1; } int ret = 0; while (optind < argc) { ret |= stat_file(argv[optind]); optind++; } return ret; }
StatInfo::StatInfo( const char *path ) { char *s, *last = NULL, *trail_slash = NULL, chslash; fullpath = strnewp( path ); dirpath = strnewp( path ); // Since we've got our own copy of the full path now sitting // in dirpath, we can find the last directory delimiter, make // a copy of whatever is beyond it as the filename, and put a // NULL in the first character after the delim character so // that the dirpath always contains the directory delim. for( s = dirpath; s && *s != '\0'; s++ ) { if( *s == '\\' || *s == '/' ) { last = s; } } if( last != NULL && last[1] ) { filename = strnewp( &last[1] ); last[1] = '\0'; } else { filename = NULL; if (last != NULL) { // we only get here if the input path ended with a dir separator // we can't stat that on windows, and *nix does't care, so we remove it. trail_slash = &fullpath[last - dirpath]; } } // remove trailing slash before we stat, and then put it back after. this fixes #4747 // why do we put it back? because things crash if we don't and this is a stable series fix. if (trail_slash) { chslash = *trail_slash; *trail_slash = 0; } stat_file( fullpath ); if (trail_slash) { *trail_slash = chslash; } }
//------------------------------------------------------------------- bool LogListHelper::_get_session_date(const char* path, const char* dir, time_t& date) { if(strlen(dir) > 4) { // Convert "sess000" to 00:00 Jan 1 1970 (day per session) if (strncmp(dir, "sess", 4) == 0) { unsigned u; if(sscanf(&dir[4], "%u", &u) == 1) { date = u * 60 * 60 * 24; return true; } } else { if (stat_file(path, &date)) { return true; } /* strptime not available for some reason // Get date from directory name struct tm tt; if(strptime(dir, "%Y-%m-%d", &tt)) { date = mktime(&tt); return true; } */ } } return false; }
StatInfo::StatInfo( int fd ) { filename = NULL; fullpath = NULL; dirpath = NULL; stat_file( fd ); }
bool exists(std::string const& f) { error_code ec; file_status s; stat_file(f, &s, ec); if (ec) return false; return true; }
size_type file_size(std::string const& f) { error_code ec; file_status s; stat_file(f, &s, ec); if (ec) return 0; return s.file_size; }
StatInfo::StatInfo( const char *param_dirpath, const char *param_filename ) { this->filename = strnewp( param_filename ); this->dirpath = make_dirpath( param_dirpath ); fullpath = dircat( param_dirpath, param_filename ); stat_file( fullpath ); }
bool is_directory(std::string const& f, error_code& ec) { ec.clear(); error_code e; file_status s; stat_file(f, &s, e); if (!e && s.mode & file_status::directory) return true; ec = e; return false; }
static long int getNumberOfThreads () { std::string stat; std::ifstream stat_file ("/proc/self/stat"); // `man proc` std::getline (stat_file, stat); stat_file.close(); return get_int (stat, ' ', 19); }
static int get_env_ids_exists(vzctl_ids_t *ctids) { DIR *dir; struct dirent *ent; int cnt; char path[512]; struct vzctl_conf_simple g_conf, l_conf; if (vzctl_parse_conf_simple(0, GLOBAL_CFG, &g_conf)) return 0; if ((dir = opendir(VZ_ENV_CONF_DIR)) == NULL) return 0; cnt = 0; while ((ent = readdir(dir)) != NULL) { ctid_t id, ctid = {}; char str[6]; if (sscanf(ent->d_name, "%37[^.].%5s", id, str) != 2 || strcmp(str, "conf")) continue; if (vzctl2_parse_ctid(id, ctid)) continue; vzctl2_get_env_conf_path(ctid, path, sizeof(path)); if (vzctl_parse_conf_simple(ctid, path, &l_conf) == 0) { char ve_host[STR_SIZE] = ""; char host[STR_SIZE] = ""; if (l_conf.ve_private == NULL) { l_conf.ve_private = subst_VEID(ctid, g_conf.ve_private_orig); } if (l_conf.ve_private == NULL || stat_file(l_conf.ve_private) != 1) continue; if (vzctl_check_owner_quiet(l_conf.ve_private, host, sizeof(host), ve_host, sizeof(ve_host))) continue; if (add_eids(ctids, ctid, ++cnt)) { cnt = -1; break; } } vzctl_free_conf_simple(&l_conf); } vzctl_free_conf_simple(&g_conf); closedir(dir); return cnt; }
mode_t StatInfo::GetMode( void ) { if(!valid) { stat_file( fullpath ); } if(!valid) { EXCEPT("Avoiding a use of an undefined mode"); } return file_mode; }
static bool getFileSizeAndProcessMultipart(char *file, off64_t *size) { file_stat_t statbuf; if (stat_file(file, &statbuf) < 0) return false; *size = statbuf.file_size; char *p = strrchr(file, '.'); if (!p || strcmp(p+1, "66600") != 0) return true; off64_t prev_size; for (int i = 1; ; i++) { p[4] = '0' + (i/10); p[5] = '0' + (i%10); if (stat_file(file, &statbuf) < 0) break; *size += statbuf.file_size; if (i > 1) { if (prev_size&0x7FF) { fprintf(stderr, "666XX file must be multiple of sector, except last fragment. (file=%s)\n", file); } } prev_size = statbuf.file_size; } *p = 0; return true; }
static int __init kinode_init(void) { struct task_struct *thr; struct kstat stbuf1; struct kstat stbuf2; int rc; #ifdef CONFIG_X86_X32 pr_err(PREFIX " CONFIG_X86_X32 is set\n", run_id); #else pr_err(PREFIX " CONFIG_X86_X32 is not set\n", run_id); #endif if (strlen(fname) < 1) { pr_err(PREFIX " invalid file name '%s'\n", run_id, fname); goto out; } rc = stat_file(&stbuf1); if (rc) { pr_err(PREFIX " direct stat failed: %d\n", run_id, rc); goto out; } /* Run the same from a kthread. */ thr = kthread_run(stat_thread, &stbuf2, "kinode_%u", run_id); if (IS_ERR(thr)) { pr_err(PREFIX " Cannot create kthread\n", run_id); goto out; } /* Wait for the thread to start, then wait for it to * terminate. */ wait_for_completion(&thr_start); rc = kthread_stop(thr); if (rc) { pr_err(PREFIX " indirect stat failed: %d\n", run_id, rc); goto out; } if (stbuf1.ino != stbuf2.ino) pr_err(PREFIX " inode numbers are different: %llu %llu\n", run_id, stbuf1.ino, stbuf2.ino); else pr_err(PREFIX " inode numbers are identical: %llu\n", run_id, stbuf1.ino); out: /* Don't load. */ return -EINVAL; }
void add_files_impl(file_storage& fs, std::string const& p , std::string const& l, boost::function<bool(std::string)> pred, boost::uint32_t flags) { std::string f = combine_path(p, l); if (!pred(f)) return; error_code ec; file_status s; stat_file(f, &s, ec, (flags & create_torrent::symlinks) ? dont_follow_links : 0); if (ec) return; // recurse into directories bool recurse = (s.mode & file_status::directory) != 0; // if the file is not a link or we're following links, and it's a directory // only then should we recurse #ifndef TORRENT_WINDOWS if ((s.mode & file_status::link) && (flags & create_torrent::symlinks)) recurse = false; #endif if (recurse) { fs.add_path(parent_path(combine_path(l, "x")), s.mode & 0777); for (directory i(f, ec); !i.done(); i.next(ec)) { std::string leaf = i.file(); if (ignore_subdir(leaf)) continue; add_files_impl(fs, p, combine_path(l, leaf), pred, flags); } } else if (s.mode & (file_status::regular_file | file_status::link)) { // #error use the fields from s int file_flags = get_file_attributes(f, (flags & create_torrent::symlinks) ? dont_follow_links : 0); // mask all bits to check if the file is a symlink if ((file_flags & file_storage::attribute_symlink) && (flags & create_torrent::symlinks)) { std::string sym_path = get_symlink_path(f); fs.add_file(l, 0, file_flags, s.mtime, sym_path, s.mode & 0777); } else { fs.add_file(l, s.file_size, file_flags, s.mtime, "", s.mode & 0777); } } if (fs.num_files() == 0) fs.set_name(l); }
static int64_t calculate_directory_size(char *path) { int64_t result = 0; DIR *d; struct dirent *entry; //DPRINTF("Calculate %s\n", path); d = opendir(path); if (!d) return -1; while ((entry = readdir(d))) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0 || strlen(entry->d_name) == 0) continue; //DPRINTF("name: %s\n", entry->d_name); file_stat_t st; char newpath[strlen(path)+strlen(entry->d_name)+2]; sprintf(newpath, "%s/%s", path, entry->d_name); if (stat_file(newpath, &st) < 0) { DPRINTF("calculate_directory_size: stat failed on %s\n", newpath); result = -1; break; } if ((st.mode & S_IFDIR) == S_IFDIR) { int64_t temp = calculate_directory_size(newpath); if (temp < 0) { result = temp; break; } result += temp; } else if ((st.mode & S_IFREG) == S_IFREG) { result += st.file_size; } } closedir(d); return result; }
static int getNumberOfThreads () { std::string stat; std::ifstream stat_file ("/proc/self/stat"); std::vector <std::string> tokens; std::getline (stat_file, stat); tokenize (stat, ' ', tokens); stat_file.close(); return atoi (tokens[19].c_str() ); }
int main (int argc, char *argv[]) { // srand(time(NULL)); // rand_generator.seed (rand()); rand_generator.seed (time(NULL)); //prepare the transcriptome transcriptome custom_transtome; pool_fragment custom_pool; long num_fragment_selected, num_fragment_selected_prev; custom_transtome.input_transcripts_gaf("trans.gaf", output_folder); custom_pool.generate_orig_pool(&custom_transtome); custom_pool.output_stat(output_folder, 0, &custom_transtome); num_fragment_selected_prev = 0; // for (int seq_round = 1; seq_round <= const_max_sequencing_round; ++seq_round) // { // custom_pool.random_fragmentation(const_num_cut_per_round); // num_fragment_selected = custom_pool.size_selection(const_size_window_low, const_size_window_high); // custom_pool.output_stat(output_folder, seq_round, &custom_transtome); // // if (num_fragment_selected > const_desired_num_reads) // break; // if (num_fragment_selected < num_fragment_selected_prev) // break; // num_fragment_selected_prev = num_fragment_selected; // } int seq_round; for (seq_round = 1; seq_round <= const_max_sequencing_round; ++seq_round) { cout << seq_round << "\t"; double cur_ratio_frag_in_target = custom_pool.random_fragmentation(const_num_cut_per_round); if (cur_ratio_frag_in_target > const_thresh_fragment_in_target_ratio) { break; } } num_fragment_selected = custom_pool.size_selection(const_size_window_low, const_size_window_high); custom_pool.output_stat(output_folder, seq_round, &custom_transtome); string filename = output_folder + "stat.txt"; ofstream stat_file(filename.c_str(), fstream::app); stat_file << "simulation finished" << endl << endl << endl; stat_file.close(); return 0; }
/** * @param file_name name of the file to open * @returns a new opened GslHFile or NULL if an error occoured (errno set) * * Open a file for reading and return the associated GSL hashed file. * The motivation for using a GslHFile over normal unix file * descriptors is to reduce the amount of opened unix file descriptors and * to ensure thread safety upon reading offset relative byte blocks. * Multiple open GslHFiles with equal file names will share a * single unix file descriptor as long as the file wasn't modified meanwhile. * This function is MT-safe and may be called from any thread. */ GslHFile* gsl_hfile_open (const gchar *file_name) { GslHFile key, *hfile; gint ret_errno; errno = EFAULT; g_return_val_if_fail (file_name != NULL, NULL); key.file_name = (gchar*) file_name; if (!stat_file (file_name, &key.mtime, &key.n_bytes)) return NULL; /* errno from stat() */ sfi_mutex_lock (&fdpool_mutex); hfile = g_hash_table_lookup (hfile_ht, &key); if (hfile) { sfi_mutex_lock (&hfile->mutex); hfile->ocount++; sfi_mutex_unlock (&hfile->mutex); ret_errno = 0; } else { gint fd; fd = open (file_name, O_RDONLY | O_NOCTTY, 0); if (fd >= 0) { hfile = sfi_new_struct0 (GslHFile, 1); hfile->file_name = g_strdup (file_name); hfile->mtime = key.mtime; hfile->n_bytes = key.n_bytes; hfile->cpos = 0; hfile->fd = fd; hfile->ocount = 1; hfile->zoffset = -2; sfi_mutex_init (&hfile->mutex); g_hash_table_insert (hfile_ht, hfile, hfile); ret_errno = 0; } else ret_errno = errno; } sfi_mutex_unlock (&fdpool_mutex); errno = ret_errno; return hfile; }
int VIsoFile::open(const char *path, int flags) { file_stat_t st; char gameCode[128]; char volumeName[32]; if (flags != O_RDONLY) return -1; if (fsBuf) { close(); } if (stat_file(path, &st) != 0) { return -1; } if ((st.mode & S_IFDIR) != S_IFDIR) { return -1; } if (ps3Mode) { if (!get_title_id(path, gameCode)) return -1; strcpy(volumeName, "PS3VOLUME"); } else { const char *dn = strrchr(path, '/'); if (dn) dn++; else dn = path; snprintf(volumeName, sizeof(volumeName), "%s", dn); } if (!generate((char *)path, volumeName, gameCode)) return -1; return 0; }
/** Get vz service status * 1 - running * 0 - stopped * -1- error */ int vzctl2_vz_status(void) { int ret; struct utsname u; uname(&u); if (kver_cmp(u.release, "3.9") >= 0) return 1; /* Check /proc/vz/veinfo & /proc/vz/venetstat exists * Fixme: try to find more correct way */ if ((ret = stat_file("/proc/vz/veinfo")) != 1) return ret; return 1; }
/* * Check the cache's timestamp against the Entries file. If it is newer, drop * our copy of the list, and re-read it. */ static int check_timestamp(CVS_WORK * cache) { if (cache->num_entries) { char list[MAXPATHLEN]; Stat_t sb; if (stat_file(admin_filename(list, cache, NAME_LIST), &sb) == 0 && sb.st_mtime == cache->timestamp) { return 0; } cache->timestamp = sb.st_mtime; } else { cache->timestamp = 0; } return 1; }
static int is_dst_free(const char *dst, ctid_t ctid, int *fail_cnt) { char *ve_private; int ret; if (dst == NULL) return 1; ve_private = subst_VEID(ctid, dst); ret = stat_file(ve_private); free(ve_private); if (ret == -1) (*fail_cnt)++; return (ret == 0 ? 1 : 0); }