static bool check_dir(acl::scan_dir& scan, const char* to, int* ncopied) { const char* rpath = scan.curr_path(); if (rpath == false) { logger_error("get from's path error: %s, to: %s", acl::last_serror(), to); return false; } SKIP(rpath); acl::string to_path; to_path << to << SEP << rpath; // printf(">>to_path: %s, to: %s\r\n", to_path.c_str(), to); if (access(to_path.c_str(), 0) == 0) return true; else { int ret = acl_make_dirs(to_path.c_str(), 0755); if (ret == 0) { (*ncopied)++; return true; } else { logger_error("make dirs(%s) error: %s", to_path.c_str(), acl::last_serror()); return false; } } }
bool queue_file::move_file(const char* queueName, const char* extName) { acl::string buf(256); bool once_again = false; while (true) { buf.clear(); buf << m_home << PATH_SEP << queueName << PATH_SEP << m_queueSub << PATH_SEP << m_partName << "." << extName; #ifdef WIN32 // 在win32下必须先关闭文件句柄 this->close(); #endif if (rename(m_filePath.c_str(), buf.c_str()) == 0) break; // 如果返回错误原因是目标路径不存在,则尝试创建目录结构 if (once_again || acl_last_error() != ENOENT) { logger_error("move from %s to %s error(%s), errno: %d, %d", m_filePath.c_str(), buf.c_str(), acl_last_serror(), acl_last_error(), ENOENT); return (false); } // 设置重试标志位 once_again = true; buf.clear(); buf << m_home << PATH_SEP << queueName << PATH_SEP << m_queueSub; // 创建队列目录 if (acl_make_dirs(buf.c_str(), 0700) == -1) { logger_error("mkdir: %s error(%s)", buf.c_str(), acl_last_serror()); return false; } } #ifdef WIN32 // win32 下需要重新再打开 return (open(m_home, queueName, m_queueSub, m_partName, extName)); #else if (m_queueName != queueName) ACL_SAFE_STRNCPY(m_queueName, queueName, sizeof(m_queueName)); if (m_extName != extName) ACL_SAFE_STRNCPY(m_extName, extName, sizeof(m_extName)); m_filePath.clear(); m_filePath << m_home << PATH_SEP << m_queueName << PATH_SEP << m_queueSub << PATH_SEP << m_partName << "." << m_extName; #endif return (true); }
queue_manager::queue_manager(const char* home, const char* queueName, unsigned sub_width) : m_scanDir(NULL) , m_home(home) , m_queueName(queueName) { if (sub_width == 0) sub_width_ = 2; else sub_width_ = sub_width; string buf = home; buf << PATH_SEP << queueName; // 先创建根目录 if (acl_make_dirs(buf.c_str(), 0700) == -1) logger_error("create dir: %s error %s", buf.c_str(), last_serror()); else logger("create dir: %s ok", buf.c_str()); char node[32]; for (unsigned i = 0; i < sub_width_; i++) { buf.clear(); safe_snprintf(node, sizeof(node), "%d", i); buf << home << PATH_SEP << queueName << PATH_SEP << node; // 创建队列下子目录 if (acl_make_dirs(buf.c_str(), 0700) == -1) logger_error("create dir: %s error %s", buf.c_str(), last_serror()); else logger("create dir: %s ok", buf.c_str()); } }
int main(int argc, char* argv[]) { #ifdef WIN32 acl::acl_cpp_init(); #endif logger_open("fs_benchmark.log", "fs_benchmark"); int ch; int nthreads = 2; #ifdef WIN32 snprintf(__path, sizeof(__path), "var/%d", (int) _getpid()); #else snprintf(__path, sizeof(__path), "var/%d", (int) getpid()); #endif while ((ch = getopt(argc, argv, "hn:c:l:kp:P:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 'n': nthreads = atoi(optarg); break; case 'c': __nfiles = atoi(optarg); break; case 'l': __length = atoi(optarg); break; case 'k': __kernel_event = true; break; case 'p': #ifdef WIN32 snprintf(__path, sizeof(__path), "%s/%d", optarg, (int) _getpid()); #else snprintf(__path, sizeof(__path), "%s/%d", optarg, (int) getpid()); #endif break; case 'P': __parallel = atoi(optarg); if (__parallel <= 0) __parallel = 1; break; default: break; } } for (size_t i = 0; i < sizeof(__data); i++) { __data[i] = 'X'; } acl_make_dirs(__path, 0700); // 创建线程池句柄 acl_pthread_pool_t* tp = acl_thread_pool_create(nthreads, 0); // 添加线程处理任务 for (int i = 0; i < nthreads; i++) acl_pthread_pool_add(tp, thread_main, NULL); // 销毁线程池 acl_pthread_pool_destroy(tp); logger_close(); #ifdef WIN32 printf("enter any key to exit\r\n"); getchar(); #endif return 0; }
static bool copy_file(acl::ifstream& in, const acl::string& to_path, const acl::string& to_filepath, int* ncopied) { if (in.fseek(0, SEEK_SET) < 0) { logger_error("fseek from file: %s error: %s", in.file_path(), acl::last_serror()); return false; } if (access(to_path.c_str(), 0) != 0) { if (acl_make_dirs(to_path.c_str(), 0755) == -1) { logger_error("create dirs: %s error: %s", to_path.c_str(), acl::last_serror()); return false; } } acl_int64 length = in.fsize(); if (length < 0) { logger_error("get file(%s)'s size error: %s", in.file_path(), acl::last_serror()); return false; } acl::ofstream out; if (out.open_trunc(to_filepath.c_str()) == false) { logger_error("ope_trunc file: %s error: %s", to_filepath.c_str(), acl::last_serror()); return false; } logger("copying from file: %s, to file: %s", in.file_path(), to_filepath.c_str()); char buf[4096]; int ret; acl_int64 nread = 0; while (true) { ret = in.read(buf, sizeof(buf), false); if (ret == -1) { if (nread == length) break; logger_error("read from file: %s error: %s, " "to file: %s, nread: %lld, length: %lld", in.file_path(), acl::last_serror(), to_filepath.c_str(), nread, length); return false; } nread += ret; if (out.write(buf, ret) == -1) { logger_error("write to file: %s error: %s", to_filepath.c_str(), acl::last_serror()); return false; } } (*ncopied)++; return true; }
void file_tmpl::create_dirs() { acl_make_dirs(project_name_.c_str(), 0755); }
void master_creator() { char buf[256]; int n; while (true) { printf("please input your program name: "); fflush(stdout); n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); if (n == ACL_VSTREAM_EOF) break; if (n == 0) snprintf(master_name, sizeof(master_name), "master_service"); else snprintf(master_name, sizeof(master_name), "%s", buf); // ´´½¨Ä¿Â¼ snprintf(dst_path_, sizeof(dst_path_), "%s", master_name); acl_make_dirs(dst_path_, 0755); printf("choose master_service type:\r\n"); printf("t: for master_threads; p: for master_proc; " "a: for master_aio; g: for master_trigger; " "r: for master_rpc; u: for master_udp; " "s: skip choose\r\n"); printf(">"); fflush(stdout); n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf)); if (n == ACL_VSTREAM_EOF) break; else if (strcasecmp(buf, "t") == 0) { create_master_threads(); break; } else if (strcasecmp(buf, "p") == 0) { create_master_proc(); break; } else if (strcasecmp(buf, "a") == 0) { create_master_aio(); break; } else if (strcasecmp(buf, "r") == 0) { create_master_rpc(); break; } else if (strcasecmp(buf, "g") == 0) { create_master_trigger(); break; } else if (strcasecmp(buf, "u") == 0) { create_master_udp(); break; } else if (strcasecmp(buf, "s") == 0) break; else printf("unknown ch: %s\r\n", buf); } for (int i = 0; i < 78; i++) putchar('-'); printf("\r\n"); }
bool queue_file::create(const char* home, const char* queueName, const char* extName, unsigned width) { acl_assert(width > 0); struct timeval tv; acl::string buf; acl::fstream* fp = NULL; int i = 0; bool dir_exist; ACL_SAFE_STRNCPY(m_home, home, sizeof(m_home)); ACL_SAFE_STRNCPY(m_queueName, queueName, sizeof(m_queueName)); ACL_SAFE_STRNCPY(m_extName, extName, sizeof(m_extName)); unsigned int n; while (1) { // 产生部分文件名 memset(&tv, 0, sizeof(tv)); gettimeofday(&tv, NULL); snprintf(m_partName, sizeof(m_partName), "%08x%08x%08x%08x%08x", (unsigned int) getpid(), (unsigned int) acl::thread::thread_self(), (unsigned int) tv.tv_sec, (unsigned int) tv.tv_usec, (unsigned int) __counter); if (__counter++ >= 1024000) __counter = 0; // 计算队列子目录 n = queue_manager::hash_queueSub(m_partName, width); snprintf(m_queueSub, sizeof(m_queueSub), "%u", n); buf.clear(); buf << m_home << PATH_SEP << m_queueName << PATH_SEP << m_queueSub << PATH_SEP << m_partName << "." << extName; fp = NEW fstream; dir_exist = false; while (true) { // 排它性创建唯一文件 if (fp->open(buf.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600) == true) goto END; logger_warn("open file %s error(%s)", buf.c_str(), acl_last_serror()); if (acl_last_error() != ENOENT || dir_exist) break; // 尝试性创建目录 buf.clear(); buf << m_home << PATH_SEP << m_queueName << PATH_SEP << m_queueSub; if (acl_make_dirs(buf.c_str(), 0700) == -1) { logger_error("mkdir: %s error(%s)", buf.c_str(), acl_last_serror()); delete fp; return false; } else logger("create path: %s ok", buf.c_str()); dir_exist = true; buf.clear(); buf << m_home << PATH_SEP << m_queueName << PATH_SEP << m_queueSub << PATH_SEP << m_partName << "." << extName; } delete fp; sleep(1); if (i++ >= 10) { logger_error("can't create file, loop 10 times for (%s)", buf.c_str()); return false; } } END: m_fp = fp; m_filePath = buf.c_str(); // 打开队列文件对象锁 if (m_locker.open(m_fp->file_handle()) == false) { logger_error("open lock for %s error(%s)", m_filePath.c_str(), acl_last_serror()); m_bLockerOpened = false; } else m_bLockerOpened = true; return (true); }