int make_dir_p(char *buff) { int ret = 0; int i = 0; char *p = buff; while (1) { if(*p == '\0') { ret = 0; if(!is_dir(buff)) { if(make_dir(buff)) { ret = 1; } } break; } if(*p == '/' && (i > 0 && *(p-1) != ':')) { *p = '\0'; if(!is_dir(buff)) { if(make_dir(buff)) { ret = 1; *p = '/'; break; } } *p = '/'; } p++; i++; } return ret; }
gchar *libravatar_cache_init(const char *dirs[], gint start, gint end) { gchar *subdir, *rootdir; int i; rootdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S, NULL); if (!is_dir_exist(rootdir)) { if (make_dir(rootdir) < 0) { g_warning("cannot create root directory '%s'", rootdir); g_free(rootdir); return NULL; } } for (i = start; i <= end; ++i) { subdir = g_strconcat(rootdir, dirs[i], NULL); if (!is_dir_exist(subdir)) { if (make_dir(subdir) < 0) { g_warning("cannot create directory '%s'", subdir); g_free(subdir); g_free(rootdir); return NULL; } } g_free(subdir); } return rootdir; }
static void prepare (void) { char *dirbuf; char dir_name[] = "/tst-fts.XXXXXX"; if (asprintf (&dirbuf, "%s%s", test_dir, dir_name) < 0) { puts ("out of memory"); exit (1); } if (mkdtemp (dirbuf) == NULL) { puts ("cannot create temporary directory"); exit (1); } add_temp_file (dirbuf); fts_test_dir = dirbuf; make_file ("12"); make_file ("345"); make_file ("6789"); make_dir ("aaa"); make_file ("aaa/1234"); make_file ("aaa/5678"); make_dir ("bbb"); make_file ("bbb/1234"); make_file ("bbb/5678"); make_file ("bbb/90ab"); }
/** * ensure_dir will build the entire directory structure up to and * including path, all directories built will be owned by * user:group and permissions will be set to mode. */ int ensure_dir(char *path, mode_t mode, uid_t uid, gid_t gid) { char *idx; if (!path || strlen(path) == 0) return 0; idx = strchr(path + 1, '/'); do { if (idx) *idx = '\0'; if (make_dir(path, mode, uid, gid)) return 1; if (idx) { *idx = '/'; idx++; } } while ((idx = strchr(idx, '/')) != NULL); if (make_dir(path, mode, uid, gid)) return 1; return 0; }
/* ensures existence of configuration directory */ static void create_config_dir(void) { LOG_FUNC_ENTER; /* ensure existence of configuration directory */ if(!is_dir(cfg.config_dir)) { #ifndef _WIN32 FILE *f; char help_file[PATH_MAX]; char rc_file[PATH_MAX]; if(make_dir(cfg.config_dir, 0777) != 0) return; snprintf(help_file, sizeof(help_file), "%s/" VIFM_HELP, cfg.config_dir); if((f = fopen(help_file, "r")) == NULL) create_help_file(); else fclose(f); snprintf(rc_file, sizeof(rc_file), "%s/" VIFMRC, cfg.config_dir); if((f = fopen(rc_file, "r")) == NULL) create_rc_file(); else fclose(f); #else if(make_dir(cfg.config_dir, 0777) != 0) return; #endif add_default_bookmarks(); } }
int mkdir_recursive(const char *pathname, mode_t mode) { char buf[128]; const char *slash; const char *p = pathname; int width; int ret; struct stat info; while ((slash = strchr(p, '/')) != NULL) { width = slash - pathname; p = slash + 1; if (width < 0) break; if (width == 0) continue; if ((unsigned int)width > sizeof(buf) - 1) { LOG(ERROR) << "path too long for mkdir_recursive"; return -1; } memcpy(buf, pathname, width); buf[width] = 0; if (stat(buf, &info) != 0) { ret = make_dir(buf, mode); if (ret && errno != EEXIST) return ret; } } ret = make_dir(pathname, mode); if (ret && errno != EEXIST) return ret; return 0; }
static void init_main_paths () { #ifdef __MINGW32__ if (is_none (get_env_path ("TEXMACS_HOME_PATH", get_env ("APPDATA") * "/TeXmacs"))) { #else if (is_none (get_env_path ("TEXMACS_HOME_PATH", "~/.TeXmacs"))) { #endif boot_error << "\n"; boot_error << "Installation problem: please send a bug report.\n"; boot_error << "'TEXMACS_HOME_PATH' could not be set to '~/.TeXmacs'.\n"; boot_error << "You may try to set this environment variable manually\n"; boot_error << "\n"; FAILED ("installation problem"); exit (1); } } /****************************************************************************** * Directory for temporary files ******************************************************************************/ static string main_tmp_dir= "$TEXMACS_HOME_PATH/system/tmp"; static void make_dir (url which) { if (!is_directory (which)) { make_dir (head (which)); mkdir (which); } } static url url_temp_dir_sub () { #ifdef __MINGW32__ static url tmp_dir= url_system (main_tmp_dir) * url_system (as_string (time (NULL))); #else static url tmp_dir= url_system (main_tmp_dir) * url_system (as_string ((int) getpid ())); #endif return (tmp_dir); } url url_temp_dir () { static url u; if (u == url_none()) { u= url_temp_dir_sub (); make_dir (u); } return u; } bool process_running (int pid) { string cmd= "ps -p " * as_string (pid); string ret= eval_system (cmd); return occurs ("texmacs", ret) && occurs (as_string (pid), ret); }
static int dev_create(const char *root, dev_res *dev) { char buf1[STR_SIZE]; char buf2[STR_SIZE]; struct stat st, st2; const char* udev_paths[] = { "/lib/udev/devices", "/etc/udev/devices", NULL}; int i; if (!dev->name) return 0; if (check_var(root, "VE_ROOT is not set")) return VZ_VE_ROOT_NOTSET; /* Get device information from CT0 and create it in CT */ snprintf(buf1, sizeof(buf1), "%s/dev/%s", root, dev->name); snprintf(buf2, sizeof(buf2), "/dev/%s", dev->name); if (stat(buf2, &st)) { if (errno == ENOENT) logger(-1, 0, "Incorrect name or no such device %s", buf2); else logger(-1, errno, "Unable to stat device %s", buf2); return VZ_SET_DEVICES; } if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) { logger(-1, 0, "The %s is not block or character device", buf2); return VZ_SET_DEVICES; } if (make_dir(buf1, 0)) return VZ_SET_DEVICES; unlink(buf1); if (mknod(buf1, st.st_mode, st.st_rdev)) { logger(-1, errno, "Unable to create device %s", buf1); return VZ_SET_DEVICES; } /* Try to create static device node for udev */ for (i = 0; udev_paths[i] != NULL; i++) { if (stat(udev_paths[i], &st2) == 0) { if (S_ISDIR(st2.st_mode)) { snprintf(buf1, sizeof(buf1), "%s/%s/%s", root, udev_paths[i], dev->name); make_dir(buf1, 0); unlink(buf1); mknod(buf1, st.st_mode, st.st_rdev); break; } } } return 0; }
inline void make_dirs(const std::string& path) { VERIFY(!path.empty()); size_t slash_pos = 0; while ((slash_pos = path.find_first_of('/', slash_pos + 1)) != std::string::npos) { make_dir(path.substr(0, slash_pos)); } if (path[path.size() - 1] != '/') { make_dir(path); } }
static void make_dirs(char *dir, int was_missing) { char *p; for (p = strchr(dir + (dir[0] == '/'), '/'); p; p = strchr(p + 1, '/')) { *p = '\0'; make_dir(dir, was_missing); *p = '/'; } make_dir(dir, was_missing); }
static int save_bridge_info(struct bridge_info *b) { int rc; char dirpath[PATH_MAX], path[PATH_MAX], path2[PATH_MAX]; char dir1[5], dir2[5]; unsigned char hexpwdhash[41]; FILE *f; fprintf(stderr, "save_bridge_info 1\n"); memset(dir1, 0, sizeof(dir1)); memset(dir2, 0, sizeof(dir2)); snis_format_sha1_hash(b->pwdhash, hexpwdhash, 41); memcpy(&dir1[0], &hexpwdhash[0], 4); memcpy(&dir2[0], &hexpwdhash[4], 4); sprintf(path, "%s/%s", database_root, dir1); if (make_dir(path)) { fprintf(stderr, "snis_multiverse: mkdir failed: %s: %s\n", path, strerror(errno)); return -1; } sprintf(dirpath, "%s/%s/%s", database_root, dir1, dir2); if (make_dir(dirpath)) { fprintf(stderr, "snis_multiverse: mkdir failed: %s: %s\n", dirpath, strerror(errno)); return -1; } sprintf(path, "%s/%s.update", dirpath, hexpwdhash); sprintf(path2, "%s/%s.data", dirpath, hexpwdhash); f = fopen(path, "w+"); if (!f) { fprintf(stderr, "snis_multiverse: fopen %s failed: %s\n", path, strerror(errno)); return -1; } rc = write_bridge_info(f, b); if (rc) { fprintf(stderr, "snis_multiverse: failed to write to %s\n", path); fclose(f); return -1; } sync_file(f); fclose(f); rc = rename(path, path2); sync_dir(dirpath); if (rc) { fprintf(stderr, "snis_multiverse: failed to rename %s to %s: %s\n", path, path2, strerror(errno)); return -1; } fprintf(stderr, "snis_multiverse: wrote bridge info to %s\n", path2); return 0; }
/********************************************************* ****************** Main Function ****************** *********************************************************/ int main(int agrc, char *agrv[]) { int rt = 0; /* return value of function main */ char buf[128] = {0}; /** * Get paramters from the command line */ if (parser_args(agrc, agrv) < 0) { print_usage(); rt = -1; goto error; }; if (perm_flag) { rt = detect_permission(path, perm); switch (rt) { case 0: printf("\033[1;36m%s has permission %s? : \033[1;35mno\n\033[0m", path, perm); break; case 1: printf("\033[1;36m%s has permission %s? : \033[1;35myes\n\033[0m", path, perm); break; default: printf("\033[1;36m%s has permission %s? : \033[1;31mfailed\n\033[0m", path, perm); break; } } if (size_flag) { rt = get_file_size(path); if (rt > 0) printf("\033[1;34msize of %s is: \033[1;35m%d bytes\n\033[0m", path, rt); else printf("\033[1;31m%s get size failed\n\033[0m", path); } if (basename_flag) { if (basename(path, buf, sizeof(buf)) != NULL) printf("\033[1;34mbasename of %s is: \033[1;35m%s\n\033[0m", path, buf); else printf("\033[1;31m%s get basename failed\n\033[0m", path); } if (dirname_flag) { if (dirname(path, buf, sizeof(buf)) != NULL) printf("\033[1;34mdirname of %s is: \033[1;35m%s\n\033[0m", path, buf); else printf("\033[1;31m%s get dirname failed\n\033[0m", path); } if (create_flag) { if (!make_dir(path, 0755)) printf("\033[1;35mcreate dir %s success\n\033[0m", path); else printf("\033[1;31mcreate dir %s failed\n\033[0m", path); } error: /** * error handling */ return rt; }
/* * Callback to make a directory from the ext4 code */ static int do_installkeys_ensure_dir_exists(const char* dir) { if (make_dir(dir, 0700) && errno != EEXIST) { return -1; } return 0; }
int heap_init ( const char *heappath) { int rc = HEAP_OK; if (file_exists (heappath)) { if (!file_is_directory (heappath)) rc = HEAP_INVALID_PATH; } else { if (make_dir (heappath) != 0) rc = HEAP_CANNOT_CREATE_PATH; } if (! rc) { path = mem_strdup (heappath); if (!path) rc = HEAP_MEMORY_ERROR; } return rc; }
int do_mkdir(int nargs, char **args) { mode_t mode = 0755; int ret; /* mkdir <path> [mode] [owner] [group] */ if (nargs >= 3) { mode = strtoul(args[2], 0, 8); } ret = make_dir(args[1], mode); /* chmod in case the directory already exists */ if (ret == -1 && errno == EEXIST) { ret = _chmod(args[1], mode); } if (ret == -1) { return -errno; } if (nargs >= 4) { uid_t uid = decode_uid(args[3]); gid_t gid = -1; if (nargs == 5) { gid = decode_uid(args[4]); } if (_chown(args[1], uid, gid) < 0) { return -errno; } } return 0; }
void envar_handler(GNode *node) { gchar profile_file[PATH_MAX] = { 0 }; buffer_envar = g_string_new(""); LOG(MOD "Groups Handler running...\n"); g_node_children_foreach(node, G_TRAVERSE_ALL, envar_item, NULL); g_strlcpy(profile_file, PROFILE_PATH, PATH_MAX); if (make_dir(profile_file, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) { LOG(MOD "Cannot create directory '%s'\n", (char*)profile_file); goto fail; } g_strlcat(profile_file, "/cloud-init.sh", PATH_MAX); if (!write_file(buffer_envar->str, buffer_envar->len, profile_file, O_CREAT|O_APPEND|O_WRONLY, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) { LOG(MOD "Cannot write environment variables\n"); goto fail; } fail: g_string_free(buffer_envar, true); buffer_envar = NULL; }
int main(int argc, char **argv) { int i, parent = 0, er = 0; if (argc < 2) goto usage; if ((argv[1][0] == '-') && (argv[1][1] == 'p')) parent = 1; newmode = 0777 & ~umask(0); for (i = parent + 1; i < argc; i++) { if (argv[i][0] != '-') { if (argv[i][strlen(argv[i])-1] == '/') argv[i][strlen(argv[i])-1] = '\0'; if (make_dir(argv[i],parent)) { fprintf(stderr, "mkdir: cannot create directory: %s\n", argv[i]); er = 1; } } else goto usage; } exit(er); usage: fprintf(stderr, "usage: %s new_dir1 [new_dir2] ...\n", argv[0]); exit(1); }
KeyValueEngine* LMDBEngineFactory::CreateDB(const std::string& name) { if (!m_env_opened) { make_dir(m_cfg.path); int env_opt = MDB_NOSYNC | MDB_NOMETASYNC | MDB_NOMETASYNC; int rc = mdb_env_open(m_env, m_cfg.path.c_str(), env_opt, 0664); if (rc != 0) { ERROR_LOG("Failed to open mdb:%s\n", mdb_strerror(rc)); return NULL; } m_env_opened = true; } LMDBEngine* engine = new LMDBEngine(); LMDBConfig cfg = m_cfg; if (engine->Init(cfg, m_env, name) != 0) { DELETE(engine); return NULL; } DEBUG_LOG( "Create DB:%s at path:%s success", name.c_str(), cfg.path.c_str()); return engine; }
/* * Extract a directory. */ static void extract_dir(struct archive *a, struct archive_entry *e, const char *path) { int mode; mode = archive_entry_mode(e) & 0777; if (mode == 0) mode = 0755; /* * Some zipfiles contain directories with weird permissions such * as 0644 or 0444. This can cause strange issues such as being * unable to extract files into the directory we just created, or * the user being unable to remove the directory later without * first manually changing its permissions. Therefore, we whack * the permissions into shape, assuming that the user wants full * access and that anyone who gets read access also gets execute * access. */ mode |= 0700; if (mode & 0040) mode |= 0010; if (mode & 0004) mode |= 0001; info("d %s\n", path); make_dir(path, mode); ac(archive_read_data_skip(a)); }
int main(int argc, char *argv[]) { if (argc != 2) { char usage[] = "%s block_directory\n\t" "Starts the bitcoin client with block chain storage " "in the specified directory\n"; printf(usage, argv[0]); exit(2); } srand(time(NULL)); /* initialize block chain and selector */ char *dir = make_dir(argv[1]); block_chain = BRNewBlockChain(dir); selector = BRNewSelector(); /* allow readline to work with select */ rl_callback_handler_install("$ ", handle_line); BRAddSelectable(selector, STDIN_FILENO, readline_callback, NULL, 0, FOR_READING); BRLoop(selector); free(dir); return 0; }
bool make_dir(const std::string& para_path) { if (is_dir_exist(para_path)) { return true; } if (is_file_exist(para_path)) { ERROR_LOG("Exist file '%s' is not a dir.", para_path.c_str()); return false; } std::string path = para_path; size_t found = path.rfind("/"); if (found == path.size() - 1) { path = path.substr(0, path.size() - 1); found = path.rfind("/"); } if (found != std::string::npos) { std::string base_dir = path.substr(0, found); if (make_dir(base_dir)) { //mode is 0755 return mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0; } } else { return mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0; } return false; }
/* * Ensure that all directories leading up to (but not including) the * specified path exist. * * XXX inefficient + modifies the file in-place */ static void make_parent(char *path) { struct stat sb; char *sep; sep = strrchr(path, '/'); if (sep == NULL || sep == path) return; *sep = '\0'; if (lstat(path, &sb) == 0) { if (S_ISDIR(sb.st_mode)) { *sep = '/'; return; } unlink(path); } make_parent(path); mkdir(path, 0755); *sep = '/'; #if 0 for (sep = path; (sep = strchr(sep, '/')) != NULL; sep++) { /* root in case of absolute d_arg */ if (sep == path) continue; *sep = '\0'; make_dir(path, 0755); *sep = '/'; } #endif }
/**************************************************************** if the client is capable of 'wanting hack', then the server will send the client a filename in the packet_join_game_reply packet. this function creates the file with a suitably random string in it and then sends the string to the server. If the server can open and read the string, then the client is given hack access. *****************************************************************/ void send_client_wants_hack(const char *filename) { if (filename[0] != '\0') { struct packet_single_want_hack_req req; struct section_file *file; if (!is_safe_filename(filename)) { return; } /* get the full filename path */ interpret_tilde(challenge_fullname, sizeof(challenge_fullname), "~/.freeciv/"); make_dir(challenge_fullname); sz_strlcat(challenge_fullname, filename); /* generate an authentication token */ randomize_string(req.token, sizeof(req.token)); file = secfile_new(FALSE); secfile_insert_str(file, req.token, "challenge.token"); if (!secfile_save(file, challenge_fullname, 0, FZ_PLAIN)) { log_error("Couldn't write token to temporary file: %s", challenge_fullname); } secfile_destroy(file); /* tell the server what we put into the file */ send_packet_single_want_hack_req(&client.conn, &req); } }
// < 0 - some errors // 0 - directory was created // 1 - directory exists int checkVEDir(const char * vedir, int unique) { int rc = 0; int cnt, i; vzctl_ids_t *ctids; char path[PATH_MAX + 1]; if (access(vedir, F_OK) == 0) { if (unique) return putErr(MIG_ERR_EXISTS, MIG_MSG_AREA_EXISTS, vedir); } else { return make_dir(vedir, DEF_DIR_MODE); } /* check that another VE, doesn't use this dir */ if ((ctids = vzctl2_alloc_env_ids()) == NULL) return putErr(MIG_ERR_VZCTL, "vzctl2_alloc_env_ids(): %s", vzctl2_get_last_error()); if ((cnt = vzctl2_get_env_ids_by_state(ctids, ENV_STATUS_EXISTS)) < 0) return putErr(MIG_ERR_VZCTL, "vzctl2_get_env_ids_by_state(): %s", vzctl2_get_last_error()); for (i = 0; i < cnt; i++) { struct vzctl_env_handle *h; int err; const char *data; if (EMPTY_CTID(ctids->ids[i])) continue; vzctl2_get_env_conf_path(ctids->ids[i], path, sizeof(path)); h = vzctl2_env_open(ctids->ids[i], VZCTL_CONF_SKIP_GLOBAL | VZCTL_CONF_BASE_SET | VZCTL_CONF_SKIP_PARAM_ERRORS, &err); if (err) { logger(LOG_ERR, "vzctl2_env_open(%s) error: %s", path, vzctl2_get_last_error()); continue; } // check that existed directories is not parent or equal // directories for VE if (vzctl2_env_get_ve_root_path(vzctl2_get_env_param(h), &data) == 0 && isConcurrentDirs(vedir, data)) rc = putErr(MIG_ERR_EXISTS, MIG_MSG_AREA_USED, vedir, ctids->ids[i]); if (vzctl2_env_get_ve_private_path(vzctl2_get_env_param(h), &data) == 0 && isConcurrentDirs(vedir, data)) rc = putErr(MIG_ERR_EXISTS, MIG_MSG_AREA_USED, vedir, ctids->ids[i]); vzctl2_env_close(h); if (rc) break; } vzctl2_free_env_ids(ctids); if (rc) return rc; return 1; }
void create_dir(char *dir) { char *d,ch; d=dir; while (*d) { if (*d=='\\' || *d=='/') { ch=*d; *d=0; make_dir(dir); *d=ch; } d++; } make_dir(dir); }
int syscall_mkdir(const char *pathname, mode_t mode) { int i, j, drive, res; int curdir_handle; char name_comp[13], conv_name[11], dir_path[501]; if (strlen(pathname) > 500) return ELONGPATH; parse_path(pathname, &drive, dir_path, name_comp); if (name_comp[0] == 0 && strlen(dir_path) > 0) { // remove the last component if (dir_path[strlen(dir_path)-1] == '/' || dir_path[strlen(dir_path)-1] == '\\') dir_path[strlen(dir_path)-1] = 0; j = 13; for (i=strlen(dir_path); (j>=0 && i>=0 && dir_path[i] != '/' && dir_path[i] != '\\'); i--) name_comp[--j] = dir_path[i]; if (j<0) j = 0; // Long name. Incorrect results if (i == 0) // special case dir_path[1] = 0; // only root dir else dir_path[i] = 0; // replace last / with null char for (i=0; i<=12; i++) name_comp[i] = name_comp[j++]; } if (dir_path[0] != 0) { curdir_handle = open_path(drive, dir_path); if (curdir_handle < 0) return curdir_handle; // Error } else { curdir_handle = get_curdir_handle(drive); increment_ref_count(curdir_handle); } // Last new dir name component. if (name_comp[0] == 0) { close_dir(curdir_handle); return EDUPLICATE_ENTRY; } if (convert_name(name_comp, conv_name) < 0) { close_dir(curdir_handle); return EINVALIDNAME; // Error } res = make_dir(curdir_handle, conv_name); close_dir(curdir_handle); if (res == 1) return 0; // Success else return res; // failure }
int LevelDBEngine::Init(const LevelDBConfig& cfg) { m_cfg = cfg; m_options.create_if_missing = true; m_options.comparator = &m_comparator; if (cfg.block_cache_size > 0) { leveldb::Cache* cache = leveldb::NewLRUCache(cfg.block_cache_size); m_options.block_cache = cache; } if (cfg.block_size > 0) { m_options.block_size = cfg.block_size; } if (cfg.block_restart_interval > 0) { m_options.block_restart_interval = cfg.block_restart_interval; } if (cfg.write_buffer_size > 0) { m_options.write_buffer_size = cfg.write_buffer_size; } m_options.max_open_files = cfg.max_open_files; if (cfg.bloom_bits > 0) { m_options.filter_policy = leveldb::NewBloomFilterPolicy(cfg.bloom_bits); } make_dir(cfg.path); m_db_path = cfg.path; leveldb::Status status = leveldb::DB::Open(m_options, cfg.path.c_str(), &m_db); do { if (status.ok()) { break; } else if (status.IsCorruption()) { ERROR_LOG("Failed to init engine:%s", status.ToString().c_str()); status = leveldb::RepairDB(cfg.path.c_str(), m_options); if (!status.ok()) { ERROR_LOG("Failed to repair:%s for %s", cfg.path.c_str(), status.ToString().c_str()); return -1; } status = leveldb::DB::Open(m_options, cfg.path.c_str(), &m_db); } else { ERROR_LOG("Failed to init engine:%s", status.ToString().c_str()); break; } } while (1); return status.ok() ? 0 : -1; }
void recv_chunk(int sockfd){ ssize_t n; char buf[MAXLINE]; char *res = "data recv done"; FILE *fs = NULL; // for store int chunk_count = 0; char fp[FP_LEN+1]; char filename[MAXLINE]; fp[FP_LEN] = '\0'; struct sockaddr_in cliaddr; socklen_t cliaddrlen = sizeof(cliaddr); // recv the data chunk and store while ( (n = Recvfrom(sockfd, buf, MAXLINE, 0, (SA *) &cliaddr, &cliaddrlen)) > 0){ printf("**recv chunk** %s\n", buf); if(strcmp(buf, "exit") == 0){ //3. EOF printf("File EOF\n"); count += 1; break; }else{ //1. we know the first packet is fp+file name if(chunk_count == 0){ // get file fp and file name memcpy(fp, buf, FP_LEN); memcpy(filename, &buf[FP_LEN], n - FP_LEN); filename[n - FP_LEN] = '\0'; // printf("File FP: %s, File name: %s\n", fp, filename); res = "fp_filename"; //Write(sockfd, res, strlen(res)); // Sendto(sockfd, res, strlen(res), 0, (SA *) &cliaddr, cliaddrlen); // index has been updated in recv_fp, so here just create file for store char path[MAXLINE]; path[0] = '\0'; strcat(path, "store/"); strcat(path, fp); printf("%s\n", path); make_dir("store"); fs = fopen(path, "wb"); if(!fs){ printf("fopen error\n"); exit(-1); } }else{ // 2. store this file and index printf("**%d bytes recved and stored**\n", n); fwrite(buf, sizeof(char), n, fs); // if need reply to the client ? } } chunk_count ++; memset(buf, 0, sizeof(buf)); } fclose(fs); }
int Backup::Save() { if (m_is_saving) { return 1; } if (m_server->m_cfg.backup_dir.empty()) { ERROR_LOG("Empty bakup dir for backup."); return -1; } //m_server->m_db->CloseAll(); m_is_saving = true; int ret = 0; char cmd[m_server->m_cfg.data_base_path.size() + 256]; make_dir(m_server->m_cfg.backup_dir); char dest[m_server->m_cfg.backup_dir.size() + 256]; char shasumfile[m_server->m_cfg.backup_dir.size() + 256]; uint32 now = time(NULL); sprintf(dest, "%s/dbsave-%u.tar", m_server->m_cfg.backup_dir.c_str(), now); sprintf(shasumfile, "%s/dbsave-%u.sha", m_server->m_cfg.backup_dir.c_str(), now); std::string source = m_server->m_cfg.data_base_path; if (0 == chdir(m_server->m_cfg.data_base_path.c_str())) { source = "*"; } sprintf(cmd, "tar cf %s %s;", dest, source.c_str()); ret = system(cmd); if (0 != chdir(m_server->m_cfg.home.c_str())) { WARN_LOG("Failed to change dir to home:%s", m_server->m_cfg.home.c_str()); } if (-1 == ret) { ERROR_LOG( "Failed to create backup data archive:%s", dest); } else { std::string sha1sum_str; ret = sha1sum_file(dest, sha1sum_str); if (-1 == ret) { ERROR_LOG( "Failed to compute sha1sum for data archive:%s", dest); } else { INFO_LOG("Save file SHA1sum is %s", sha1sum_str.c_str()); file_write_content(shasumfile, sha1sum_str); m_last_save = now; } } m_is_saving = false; return ret; }
bool write_sudo_directives(const GString* data, const gchar* filename, int oflags) { gchar sudoers_file[PATH_MAX] = { 0 }; g_strlcpy(sudoers_file, SUDOERS_PATH, PATH_MAX); if (make_dir(sudoers_file, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) { return false; } g_strlcat(sudoers_file, filename, PATH_MAX); return write_file(data->str, data->len, sudoers_file, oflags, S_IRUSR|S_IRGRP); }