int Project::load() { std::string curdir = getCurrentDir(); std::string abspath = getRealPath(curdir); char buf[PATH_MAX]; for (;;) { if (curdir.length() < MIN_PROJECT_PATH_LENGTH) { return -1; // error } std::string proj = curdir + PROJECT_FILENAME; if (existsFile(proj)) { filename = proj; content = getFileContent(proj); dir = curdir; return 0; // ok } // copy curdir to buf for, api compatibility memcpy(buf, curdir.c_str(), curdir.length()); buf[curdir.length()] = '\0'; curdir = dirname(buf); } return -1; // error }
void CResourcePathDirector::addResourcePath( std::string strKey, std::string strPath ) { std::string realPath = ""; realPath = getRealPath( strPath ); m_stl_map_ResourcePaths.insert( std::pair<std::string, std::string>( strKey, realPath ) ); }
pair<string, int64_t> ShareManager::toRealWithSize(const string& virtualFile, bool isInSharingHub) { Lock l(cs); if(virtualFile == "MyList.DcLst") { throw ShareException("NMDC-style lists no longer supported, please upgrade your client"); } if(virtualFile == Transfer::USER_LIST_NAME_BZ || virtualFile == Transfer::USER_LIST_NAME) { generateXmlList(); if (!isInSharingHub) { string emptyList = Util::getPath(Util::PATH_USER_CONFIG) + "Emptyfiles.xml.bz2"; return make_pair(emptyList, 0); } return make_pair(getBZXmlFile(), 0); } auto f = findFile(virtualFile); return make_pair(f.getRealPath(this), f.getSize()); }
int main(int argc, char **argv){ int sock_fd; create_and_conn(&sock_fd, "127.0.0.1", 9999); while(1){ message msg; printf("input the cmd(0:add 1:get 2:send [3:add 4:get 5:wc-tag])\n"); int input; scanf("%d", &input); getchar();//去除stdin中的'\n' switch(input){ case INIT: { //create a tag in current working folder //to indicate this is the WC char mode[] = "0444"; int ret; int m = strtol(mode, 0, 8); ret = creat(WORKING_FOLDER_TAG, m); if(ret == -1) { perror("creat file"); } break; } case TEST_ADD: { int ret; printf("file need to transfer:"); char path [256]; char realpath[256]; scanf("%s",path); if(!checkFileExist(path)){ printf("Error:%s doesn't exist!\n", path); goto EXIT; } /*get real path, that is, have prefix*/ ret = getRealPath(path, realpath); if(ret == -1) { goto EXIT; } printf("real path:%s\n", realpath); //mes send after all is ok msg.cmd = TEST_ADD; ret = send(sock_fd, &msg, sizeof(msg), 0); if(ret == -1){ perror("send cmd msg\n"); } printf("-------------start send----------\n"); Send(realpath, sock_fd); printf("-------------end send ----------\n"); break; } case TEST_GET: { int ret; printf("file need to Get:"); char path [256]; char realpath[256]; scanf("%s",path); /*get real path, that is, have prefix*/ ret = getRealPath(path, realpath); if(ret == -1) { goto EXIT;//return -1; } printf("real path:%s\n", realpath); getchar(); char version[32]; scanf("%s", version); getchar(); int byLatestVersion, recursive; scanf("%d %d", &byLatestVersion, &recursive); //msg send after all is ok msg.cmd = TEST_GET; ret = send(sock_fd, &msg, sizeof(msg), 0); if(ret == -1){ perror("send cmd msg\n"); } printf("-------------start Get----------\n"); clientGetFile(sock_fd, realpath, version, recursive, byLatestVersion); printf("-------------end Get ----------\n"); break; } case ADD_FILES:{ msg.cmd = ADD_FILES; int ret = send(sock_fd, &msg, sizeof(msg), 0); if(ret == -1){ perror("send cmd msg\n"); } break; } case GET_FILES:{ msg.cmd = GET_FILES; int ret = send(sock_fd, &msg, sizeof(msg), 0); if(ret == -1){ perror("send msg\n"); } //getFile(sock_fd); break; } case SEND_FILES:{ msg.cmd = SEND_FILES; int ret = send(sock_fd, &msg, sizeof(msg), 0); if(ret == -1){ perror("send msg\n"); } //getFile(sock_fd); break; } } break; //getFile(sock_fd); } EXIT: close(sock_fd); return 0; }
void Encoder::setInputFilePath(const std::string & inputPath) { m_FilePath.assign(getRealPath(inputPath)); }
int main(int argc, char *argv[]) { int ret; struct stat statbuf; if(!(argc == 2 || (argc == 4 && 0 == strcmp(argv[2], "-comp")) || (argc >= 4 && 0 == strcmp(argv[2], "-name")))) err_quit("usage: myfind <pathname> [-comp <filename> | -name<str>...]"); if(lstat(argv[1], &statbuf) < 0) err_quit("lstat error: %s\n", argv[1]); if(S_ISDIR(statbuf.st_mode) == 0) //equal 0, not a directory, then use current directory strcpy(argv[1], "."); if(argc == 2) { ret = myftw(argv[1], myfunc); ntot = nreg + ndir + nblk + nfifo + nslink + nsock + nchr; if(ntot == 0) ntot = 1;/* avoid divide by 0; print 0 for all counts*/ printf("regular files = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot); printf("less than 4096 = %7ld, %5.2f %%\n", less4096, less4096*100.0/ntot); printf("derectories = %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot); printf("block special = %7ld, %5.2f %%\n", nblk, nblk*100.0/ntot); printf("char special = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot); printf("FIFOs = %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot); printf("symbolic links = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot); printf("sockets = %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot); } /*找出所有与指定文件有 相同内容 的文件*/ if(argc == 4 && 0 == strcmp(argv[2], "-comp")) { /*输入判断,如果文件不符合,则可以直接返回错误,不用open了*/ if(lstat(argv[3], &statbuf) < 0) err_quit("lstat error: %s\n", argv[3]); if(!S_ISREG(statbuf.st_mode)) err_quit("not a regular file: %s\n", argv[3]); /*读入文件内容*/ int fd; filesize = statbuf.st_size; if((fd = open(argv[3], O_RDONLY, FILE_MODE)) == -1) err_sys("can't open the file: %s\n", argv[3]); if((filebuf = (char*)malloc(sizeof(char) * filesize)) == NULL || (comparebuf = (char*)malloc(sizeof(char) * filesize)) == NULL) err_sys("malloc error\n"); if(read(fd, filebuf, filesize) != filesize) err_sys("read error: %s\n", argv[3]); close(fd); /*将起始目录转为绝对路径*/ int len; inputpath = path_alloc(&len); getRealPath(argv[1], inputpath); char* tmp = inputpath + strlen(inputpath); *(tmp-1) = 0; //printf("inputpath_after_tmp:%s\n") //??? char *ptr, *end; const char *split; split = getFileName(argv[3]); /*分离目录和文件名*/ //保存指定文件的绝对目录 inputfilepath = path_alloc(&len); //path_alloc返回的类型是char* getRealPath(argv[1], inputfilepath); end = argv[3] + strlen(argv[3]); ptr = inputfilepath + strlen(inputfilepath); while(split < end) *ptr++ = *split++; *ptr = 0; //开始查找 printf("\nSame content as '%s':\n", argv[3]); ret = myftw(inputpath, myfuncCompareContent); if(filefindcount == 0) printf("Can't find any match file.\n"); printf("\n"); } /*找出filename相同的文件*/ if(argc >= 4 && strcmp(argv[2], "-name") == 0) { int len, i; const char *split; inputpath = path_alloc(&len); inputfilename = path_alloc(&len); getRealPath(argv[1], inputpath); //inputpath后面有'/' char* tmp = inputpath + strlen(inputpath); *(tmp-1) = 0; printf("\n"); for(i = 3; i < argc; i++) { split = getFileName(argv[i]); if(split != argv[i]) /*输入的文件名包含了路径*/ err_quit("only filename, do you include pathname?"); strncpy(inputfilename, argv[i], len); filefindcount = 0; printf("%d. Same filename as '%s':\n", i-2, argv[i]); ret = myftw(inputpath, myfuncCompareName); if(filefindcount == 0) printf("Cannot find any match file\n\n"); } } exit(ret); }
bool ExternalFileWriter::write(const osg::Object & obj, const Options * options, std::string * out_absolutePath, std::string * out_relativePath) { ObjectsSet::iterator it( _objects.find(&obj) ); if (it != _objects.end()) { // Object has already been passed to this method if (out_absolutePath) *out_absolutePath = it->second.absolutePath; if (out_relativePath) *out_relativePath = it->second.relativePath; return it->second.written; } // Object is a new entry // Get absolute source path WriteType type( getType(obj) ); std::string originalFileName( getFileName(obj, type) ); std::string absoluteSourcePath; if (_keepRelativePaths && !originalFileName.empty()) // if keepRelativePaths is false, absoluteSourcePath is not used, then we can skip this part { if (isAbsolutePath(originalFileName)) absoluteSourcePath = originalFileName; else absoluteSourcePath = concatPaths(_srcDirectory, originalFileName); absoluteSourcePath = getRealPath(convertFileNameToNativeStyle(absoluteSourcePath)); // getRealPath() here is only used to canonize the path, not to add current directory in front of relative paths, hence the "concatPaths(_srcDirectory, ...)" just above } // Compute destination paths from the source path std::string relativeDestinationPath; std::string absoluteDestinationPath; if (absoluteSourcePath.empty()) { // We have no name. Generate one. generateObjectName(relativeDestinationPath, absoluteDestinationPath, type); } else { // We have a name. if (_keepRelativePaths) { // We'll try to keep images relative path. relativeDestinationPath = getPathRelative(_srcDirectory, absoluteSourcePath); unsigned int nbDirsUp = countNbDirsUp(relativeDestinationPath); // TODO if nbDirsUp>nb dirs in _destDirectory, then issue a warning, and use simple file name if (nbDirsUp > _allowUpDirs) relativeDestinationPath = getSimpleFileName(absoluteSourcePath); } else { // We keep only the simple file name. relativeDestinationPath = getSimpleFileName(absoluteSourcePath); } absoluteDestinationPath = getRealPath(convertFileNameToNativeStyle( concatPaths(_destDirectory, relativeDestinationPath) )); // TODO Check for absolute paths collisions between multiple objects } // Write object bool written(false); if (!makeDirectoryForFile(absoluteDestinationPath)) { OSG_NOTICE << "Can't create directory for file '" << absoluteDestinationPath << "'. May fail creating the image file." << std::endl; } if (!doWrite(obj, type, absoluteDestinationPath, options)) { OSG_WARN << "Can't write file '" << absoluteDestinationPath << "'." << std::endl; } else written = true; // Add entry _objects.insert(ObjectsSet::value_type(&obj, ObjectData(absoluteDestinationPath, relativeDestinationPath, written))); _searchMap.insert(SearchMap::value_type(pathHash(absoluteDestinationPath), &obj)); // Fill output strings if (out_absolutePath) *out_absolutePath = absoluteDestinationPath; if (out_relativePath) *out_relativePath = relativeDestinationPath; return written; }
//SIGPIPE是正常的信号,对端断开后本端协议栈在向对端传送数据时,对端会返回TCP RST,导致本端抛出SIGPIPE信号 void init_daemon(char* cmd) { int pid; char *buf = "This is a Daemon, wcdj\n"; char path[FILEPATH_MAX]; getRealPath(path); strcat(path,"/"); strcat(path,"pushserver.lock"); pid = readPidFile(path); if(pid != -1) { printf("The server is start ...\n"); } if(strcmp(cmd,"stop") == 0 && pid != -1){ closeServer(pid,0); int status; int w; do { w = waitpid(pid, &status, WUNTRACED | WCONTINUED); if (w == -1) { perror("waitpid"); exit(EXIT_FAILURE); } if (WIFEXITED(status)) { printf("exited, status=%d/n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("killed by signal %d/n", WTERMSIG(status)); } else if (WIFSTOPPED(status)) { printf("stopped by signal %d/n", WSTOPSIG(status)); } else if (WIFCONTINUED(status)) { printf("continued/n"); } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); exit(0); } else if(strcmp(cmd,"restart") == 0 && pid != -1){ int status; int w; do { w = waitpid(pid, &status, WUNTRACED | WCONTINUED); if (w == -1) { perror("waitpid"); exit(EXIT_FAILURE); } if (WIFEXITED(status)) { printf("exited, status=%d/n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("killed by signal %d/n", WTERMSIG(status)); } else if (WIFSTOPPED(status)) { printf("stopped by signal %d/n", WSTOPSIG(status)); } else if (WIFCONTINUED(status)) { printf("continued/n"); } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); }else if(pid != -1){ printf("The server is start ...\n"); exit(0); } /* 屏蔽一些有关控制终端操作的信号 * 防止在守护进程没有正常运转起来时,因控制终端受到干扰退出或挂起 */ signal(SIGINT, SIG_IGN);// 终端中断 signal(SIGHUP, SIG_IGN);// 连接挂断 signal(SIGQUIT, SIG_IGN);// 终端退出 signal(SIGPIPE, SIG_IGN);// 向无读进程的管道写数据 signal(SIGTTOU, SIG_IGN);// 后台程序尝试写操作 signal(SIGTTIN, SIG_IGN);// 后台程序尝试读操作 signal(SIGTERM, SIG_IGN);// 终止 struct rlimit rl; if (getrlimit(RLIMIT_NOFILE, &rl) < 0) printf("%s: can't get file limit", "pushserver"); int i; /* pid=fork(); if(pid>0) exit(0);//是父进程,结束父进程 else if(pid< 0) exit(1);//fork失败,退出 //是第一子进程,后台继续执行 setsid();//第一子进程成为新的会话组长和进程组长 //并与控制终端分离 pid=fork(); if(pid>0) exit(0);//是第一子进程,结束第一子进程 else if(pid< 0) exit(1);//fork失败,退出 */ if ((pid = fork()) < 0) printf("%s: can't fork", cmd); else if (pid != 0) /* parent */{ printf("tuichuzhu\n"); exit(0); } printf("jinlaile\n"); setsid(); //是第二子进程,继续 //第二子进程不再是会话组长 main_pid = getpid(); writePidFile(path); for(i=3;i< NOFILE;++i)//关闭打开的文件描述符 MAXFILE 65536 close(i); /* // [3] set current path char szPath[1024]; if(getcwd(szPath, sizeof(szPath)) == NULL) { perror("getcwd"); exit(1); } else { chdir(szPath); printf("set current path succ [%s]\n", szPath); } */ //chdir("/tmp");//改变工作目录到/tmp umask(0);//重设文件创建掩模 // [6] set termianl signal signal(SIGTERM, wait_close); signal(SIGHUP, restart_server); /* * Close all open file descriptors. */ if (rl.rlim_max == RLIM_INFINITY) rl.rlim_max = 1024; for (i = 0; i < rl.rlim_max; i++) close(i); /* * Attach file descriptors 0, 1, and 2 to /dev/null. */ int fd0 = open("/dev/null", O_RDWR); int fd1 = dup(0); int fd2 = dup(0); /* * Initialize the log file. */ openlog("pushserver", LOG_CONS, LOG_DAEMON); if (fd0 != 0 || fd1 != 1 || fd2 != 2) { syslog(LOG_ERR, "unexpected file descriptors %d %d %d",fd0, fd1, fd2); exit(1); } syslog(LOG_DEBUG, "daem ok "); return; }
CChildAttachDialogMan::CChildAttachDialogMan(void) { m_arrAttachDlgInfoData.RemoveAll(); CString strIni; TCHAR strModuleFileName[MAX_PATH]; TCHAR strDriver[MAX_PATH]; TCHAR strPath[MAX_PATH]; GetModuleFileName(NULL,strModuleFileName,MAX_PATH); _tsplitpath_s(strModuleFileName, strDriver,_countof(strDriver), strPath,_countof(strPath),NULL,0,NULL,0); _tcscat_s(strDriver,_countof(strDriver),strPath); _tcscat_s(strDriver,_countof(strDriver),_T("process.ini")); strIni=strDriver; int groupCount = GetPrivateProfileInt(_T("group"),_T("count"),0,strIni); int nMaxColumns = GetPrivateProfileInt(_T("group"),_T("maxColumn"),0,strIni); if(nMaxColumns<1)nMaxColumns = 3; for (int i=0;i<groupCount;i++) { //read group INFO stGroupInfo *pInfo = new stGroupInfo; TCHAR strTemp[255]; CString strkeyName; strkeyName.Format(_T("groupname_%d"),i); GetPrivateProfileString(_T("group"),strkeyName,_T(""),strTemp,255,strIni); pInfo->nGroupID = i; pInfo->strGroupName = strTemp; pInfo->n_MaxColumns = nMaxColumns; m_mapGroupInfo[i] = pInfo; } stGroupInfo *pUnKnownGroupInfo = new stGroupInfo; pUnKnownGroupInfo->nGroupID = 20140409;//magicNum 组id 不会到这个数吧。 pUnKnownGroupInfo->strGroupName=_T("UnKnownGroup"); std::pair<std::map<int,stGroupInfo*>::iterator,bool> retInsertMap = m_mapGroupInfo.insert(std::pair<int,stGroupInfo*>(pUnKnownGroupInfo->nGroupID,pUnKnownGroupInfo)); int processCount = GetPrivateProfileInt(_T("process"),_T("count"),0,strIni); BOOL bHadUnKnownGroupInfo=FALSE; for (int i=0;i<processCount;i++) { TCHAR strTemp[255]; CString strAppName; strAppName.Format(_T("process_%d"),i); GetPrivateProfileString(strAppName,_T("path"),_T(""),strTemp,255,strIni); //这里需要去掉路径中的多余斜杠 getRealPath(strTemp); CString strExePath(strTemp); if(strExePath.IsEmpty() || strExePath.GetLength()<3)//c:\1.exe { continue; } GetPrivateProfileString(strAppName,_T("displayname"),_T(""),strTemp,255,strIni); int nGroupID = GetPrivateProfileInt(strAppName,_T("groupID"),pUnKnownGroupInfo->nGroupID,strIni); AttachDlgInfoData *lpData = new AttachDlgInfoData; lpData->strExePath = strExePath; std::map<int ,stGroupInfo*>::iterator it ; it = m_mapGroupInfo.find(nGroupID); if(it !=m_mapGroupInfo.end()) lpData->pstGroupInfo =it->second; else{ lpData->pstGroupInfo = pUnKnownGroupInfo; bHadUnKnownGroupInfo = TRUE; } lpData->nInGroupPos = lpData->pstGroupInfo->AddRef(); lpData->pAttachDlg = NULL; CMSVDlg*pDlg = new CMSVDlg(); pDlg ->Create(IDD_MSVDLG); lpData->pAttachDlg = pDlg; AddToArr(lpData); } if(!bHadUnKnownGroupInfo && retInsertMap.second){ m_mapGroupInfo.erase(retInsertMap.first); } /* normal [group] count= groupname_0= groupname_1= [process] count=1 [process_0] path= displayname= groupname= groupID= */ }