void persistent_write (url dir, string key, string val, url file, unsigned int code) { string v= local_prefix (dir) * key; if (is_directory (file)) { persistent_update_key (dir, key, file, code); persistent_write (dir, key, val, persistent_file [v], persistent_hash [v]); } else { hashmap<string,string> map (""); if (is_regular (file)) map= persistent_read_map (file); map (key)= val; if (code == 0 || (N (map) <= MAX_INNER && total_size (map) <= MAX_SIZE)) persistent_write_map (file, map); else { remove (file); mkdir (file); iterator<string> it= iterate (map); while (it->busy ()) { string skey= it->next (); string sval= map [skey]; string sv = local_prefix (dir) * skey; unsigned int scode= persistent_hash [sv]; persistent_write (dir, skey, sval, file, scode); } } } }
void FileGrabber::findNextFile() { bool first = true; while (mCurrentFile != cEndOfDirectory && (!isExtensionAccepted() || !is_regular(*mCurrentFile) || first)) { ++mCurrentFile; first = false; } }
void FileGrabber::rescanFolder(){ //mCurrentFile = boost::filesystem::directory_iterator(mDirectoryPath); mDirectory = boost::filesystem::path(mDirectoryPath); mCurrentFile = boost::filesystem::directory_iterator(mDirectory); //cEndOfDirectory = boost::filesystem::directory_iterator(); mFirstImage = true; if (isExtensionAccepted() && is_regular(*mCurrentFile)) loadImage(); }
static bool list_in_directory (string dir, string name, array<string> suffix, bool dir_flag) { if (name == "") return false; if (name == "..") return dir_flag; if (name[0]=='.') return false; if (dir_flag) return is_directory (url_system (dir, name)); else return is_regular (url_system (dir, name)) && has_suffix (name, suffix); }
void persistent_remove (url dir, string key, url file, unsigned int code) { string v= local_prefix (dir) * key; if (is_directory (file)) { persistent_update_key (dir, key, file, code); persistent_remove (dir, key, persistent_file [v], persistent_hash [v]); } else if (is_regular (file)) { hashmap<string,string> map= persistent_read_map (file); if (map->contains (key)) { map->reset (key); persistent_write_map (file, map); } } }
FileGrabber::FileGrabber( const char *pDirectoryPath): mDirectoryPath(pDirectoryPath), mDirectory(pDirectoryPath), mCurrentFile(mDirectory), mFirstImage(true), mImage(cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3)) { if (mCurrentFile == cEndOfDirectory) std::cout << "there was no image in your directory" << std::endl; while (mCurrentFile != cEndOfDirectory && (!isExtensionAccepted() || !is_regular(*mCurrentFile))) { ++mCurrentFile; } loadImage(); }
void tmfs_import (url prj_dir, url u, string prj) { cout << "Process " << (prj_dir * u) << "\n"; if (is_or (u)) { tmfs_import (prj_dir, u[1], prj); tmfs_import (prj_dir, u[2], prj); } else if (is_directory (prj_dir * u)) { bool flag; array<string> a= read_directory (prj_dir * u, flag); if (flag) return; for (int i=0; i<N(a); i++) if (!is_cruft (a[i]) && a[i] != "." && a[i] != "..") tmfs_import (prj_dir, u * a[i], prj); } else if (is_regular (prj_dir * u)) { string loc= as_string (u); properties ps; ps << seq ("mirror", "?file", prj, loc) << seq ("in", "?file", prj); collection files= tmfs_query (ps, "?file"); if (N (files) == 0) { properties xps; xps << seq ("mirror", "self", prj, loc); string val = load_string (prj_dir * u); string name= create_name (as_string (tail (u)), val); string file= tmfs_create_file (name, val, prj, xps); cout << "Import " << u << " -> " << file << "\n"; } else { string file= first (files); string val1= tmfs_load_file (file); string val2= load_string (prj_dir * u); if (val1 == val2) return; tmfs_save_file (file, val2); cout << "Update " << u << " -> " << file << "\n"; } } properties ps= tmfs_list_heads_inside (u, prj); for (int i=0; i<N(ps); i++) { url v = ps[i][0]; string file= ps[i][1]; if (!exists (prj_dir * v)) { tmfs_reset_head (file); cout << "Remove " << v << " -> " << file << "\n"; } } }
void persistent_retrieve (url dir, string key, url file, unsigned int code) { string v= local_prefix (dir) * key; if (is_directory (file)) { persistent_update_key (dir, key, file, code); persistent_retrieve (dir, key, persistent_file [v], persistent_hash [v]); } else if (is_regular (file)) { hashmap<string,string> map= persistent_read_map (file); if (map->contains (key)) { persistent_cache (v)= map [key]; persistent_has (v)= true; } else persistent_has (v)= false; } else persistent_has (v)= false; }
void hash_entry(const directory_entry &i, crypto_generichash_state &state) { auto &p = i.path(); size_t size = 0; if (is_regular_file(i.status())) size = (size_t)file_size(i.path()); if (is_regular(i.status())) { char chunk_buffer[16 * 1024]; size_t chunk_buffer_size = sizeof(chunk_buffer); size_t chunk_cnt = size / chunk_buffer_size; size_t last_chunk_size = size % chunk_buffer_size; std::ifstream file(p.native(), std::ifstream::binary); if (last_chunk_size != 0) ++chunk_cnt; else last_chunk_size = chunk_buffer_size; for (size_t chunk = 0; chunk < chunk_cnt; ++chunk) { size_t chunk_size = chunk_buffer_size; if (chunk == chunk_cnt - 1) chunk_size = last_chunk_size; file.read(&chunk_buffer[0], chunk_size); crypto_generichash_update(&state, (unsigned char *)&chunk_buffer[0], chunk_size); } return; } if (is_symlink(i.status())) { path sym_path(::read_symlink(p)); std::string s = sym_path.generic_string(); crypto_generichash_update(&state, (unsigned char *) s.c_str(), s.length()); return; } if (is_directory(i.status())) { crypto_generichash_update(&state, (const unsigned char *)"d", 1); return; } }
url persistent_file_name (url dir, string suffix) { (void) local_prefix (dir); dir= dir * url ("_"); url nr= dir * url ("_"); if (number_persistent_file_names == -1) { if (is_regular (nr)) { string s; load_string (nr, s, false); number_persistent_file_names= as_int (s); } else number_persistent_file_names= 0; } url r= persistent_make_file_name (dir, number_persistent_file_names, suffix); number_persistent_file_names++; save_string (nr, as_string (number_persistent_file_names)); return r; }
ResultCode FileReadBuffer::restore(const boost::filesystem::path& pathFile) { if (!exists(pathFile) || !is_regular(pathFile)) { return rcFailure; } std::ifstream fileIn(pathFile.string().c_str(), std::ios::binary); if (!fileIn) { return rcFailure; } uiPos_ = 0; uiSize_ = 4096; char pTemp[4096]; while (true) { fileIn.read(pTemp, uiSize_); if (fileIn.bad()) { return rcFailure; } size_t uiCount = fileIn.gcount(); if (uiCount == 0) break; Buffer* pNewBuffer = new Buffer[uiPos_ + uiCount]; memcpy(pNewBuffer, pBuffer_, uiPos_); memcpy(pNewBuffer+uiPos_, pTemp, uiCount); delete[] pBuffer_; pBuffer_ = pNewBuffer; uiPos_ += uiCount; if (uiCount < uiSize_) break; } uiSize_ = uiPos_; uiPos_ = 0; fileIn.close(); return rcSuccess; }
ResultCode FileReadWriteBuffer::restore() { if (!exists(pathFile_) || !is_regular(pathFile_)) { return rcFailure; } std::ifstream fileIn(pathFile_.string().c_str(), std::ios::binary); if (!fileIn) { return rcFailure; } setPosition(0); const size_t size = 4096; resize(size); char pTemp[size]; while (true) { fileIn.read(pTemp, size); if (fileIn.bad()) { return rcFailure; } size_t uiCount = fileIn.gcount(); if (uiCount == 0) break; writeData((const Buffer*)pTemp, uiCount); if (uiCount < size) break; } setPosition(0); fileIn.close(); return rcSuccess; }
void directory_get_files_with_ext(const std::string & dir, const std::string & ext, std::list<std::string> & list) { boost::filesystem::path p(dir); if(!exists(p)) { return; } boost::filesystem::directory_iterator end_itr; for ( boost::filesystem::directory_iterator itr( p ); itr != end_itr; ++itr ) { // is_regular() is deprecated but is_regular_file isn't in 1.34. if ( is_regular(*itr) && (ext.empty() || (std_string_to_lower(extension(*itr)) == ext)) ) { #if BOOST_VERSION >= 104601 list.push_back(itr->path().string()); #else list.push_back(itr->string()); #endif } } }
static JSBool do_import(JSContext *context, JSObject *obj, Importer *priv, std::string &name) { std::string filename; std::string full_path; std::string dirname; jsval search_path_val; JSObject *search_path; JSObject *module_obj = NULL; uint32_t search_path_len; uint32_t i; JSBool result; std::vector<std::string> directories; jsid search_path_name; bool exists; search_path_name = gjs_context_get_const_string(context, GJS_STRING_SEARCH_PATH); if (!gjs_object_require_property(context, obj, "importer", search_path_name, &search_path_val)) { return JS_FALSE; } if (!search_path_val.isObject()) { gjs_throw(context, "searchPath property on importer is not an object"); return JS_FALSE; } search_path = JSVAL_TO_OBJECT(search_path_val); if (!JS_IsArrayObject(context, search_path)) { gjs_throw(context, "searchPath property on importer is not an array"); return JS_FALSE; } if (!JS_GetArrayLength(context, search_path, &search_path_len)) { gjs_throw(context, "searchPath array has no length"); return JS_FALSE; } result = JS_FALSE; filename = std::string(name) + ".js"; /* First try importing an internal module like byteArray */ if (priv->is_root && gjs_is_registered_native_module(context, obj, name) && import_native_file(context, obj, name)) { gjs_debug(GJS_DEBUG_IMPORTER, "successfully imported module '%s'", name); result = JS_TRUE; goto out; } for (i = 0; i < search_path_len; ++i) { jsval elem; elem = JSVAL_VOID; if (!JS_GetElement(context, search_path, i, &elem)) { /* this means there was an exception, while elem == JSVAL_VOID * means no element found */ goto out; } if (JSVAL_IS_VOID(elem)) continue; if (!JSVAL_IS_STRING(elem)) { gjs_throw(context, "importer searchPath contains non-string"); goto out; } if (!gjs_string_to_utf8(context, elem, dirname)) goto out; /* Error message already set */ /* Ignore empty path elements */ if (dirname[0] == '\0') continue; /* Try importing __init__.js and loading the symbol from it */ full_path = pathCombine(dirname, MODULE_INIT_FILENAME); module_obj = load_module_init(context, obj, full_path); if (module_obj != NULL) { jsval obj_val; if (JS_GetProperty(context, module_obj, name.c_str(), &obj_val)) { if (!JSVAL_IS_VOID(obj_val) && JS_DefineProperty(context, obj, name.c_str(), obj_val, NULL, NULL, GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT)) { result = JS_TRUE; goto out; } } } /* Second try importing a directory (a sub-importer) */ full_path = pathCombine(dirname, name); if (is_directory(full_path)) { std::cout << "Adding directory '" << full_path << "' to child importer '" << name << "'\n", directories.push_back(full_path); } /* If we just added to directories, we know we don't need to * check for a file. If we added to directories on an earlier * iteration, we want to ignore any files later in the * path. So, always skip the rest of the loop block if we have * directories. */ if (directories.size() > 0) { continue; } /* Third, if it's not a directory, try importing a file */ full_path = pathCombine(dirname, filename); std::cout << "full path: " << full_path << "\n"; exists = is_regular(full_path); if (!exists) { std::cout << "JS import '" << name << "' not found in " << dirname << "\n"; continue; } if (import_file_on_module (context, obj, name, full_path)) { std::cout << "successfully imported module '" << name << "'\n"; result = JS_TRUE; } /* Don't keep searching path if we fail to load the file for * reasons other than it doesn't exist... i.e. broken files * block searching for nonbroken ones */ goto out; } if (directories.size() > 0) { /* NULL-terminate the char** */ if (import_directory(context, obj, name, directories)) { std::cout << "successfully imported directory '" << name << "'\n"; result = JS_TRUE; } } out: if (!result && !JS_IsExceptionPending(context)) { /* If no exception occurred, the problem is just that we got to the * end of the path. Be sure an exception is set. */ gjs_throw(context, "No JS module '%s' found in search path", name.c_str()); } return result; }
inline bool is_regular(const char *p_path) { file_status _status; return stat(_status, p_path) && is_regular(_status); }