JBoolean JSplitPathAndName ( const JCharacter* fullName, JString* path, JString* name ) { assert( !JStringEmpty(fullName) ); JString pathAndName = fullName; assert( pathAndName.GetLastCharacter() != ACE_DIRECTORY_SEPARATOR_CHAR ); JIndex i; if (pathAndName.LocateLastSubstring(ACE_DIRECTORY_SEPARATOR_STR, &i)) { *path = pathAndName.GetSubstring(1,i); *name = pathAndName.GetSubstring(i+1, pathAndName.GetLength()); JCleanPath(path); return kJTrue; } else { *path = JGetCurrentDirectory(); *name = pathAndName; return kJFalse; } }
JBoolean GMApp::NewMailbox ( const JCharacter* filename, const JBoolean openFile ) { JString path; JString name; JSplitPathAndName(filename, &path, &name); if (path.IsEmpty()) { path = JGetCurrentDirectory(); } if (!(JDirectoryExists(path) && JDirectoryReadable(path))) { JString notice = "You do not have write permissions in directory \"" + path + "\""; JGetUserNotification()->ReportError(notice); return kJFalse; } ofstream os(filename); if (!os.good()) { JString notice = "Unable to create file \"" + path + name + "\""; JGetUserNotification()->ReportError(notice); return kJFalse; } os.close(); if (openFile) { OpenMailbox(filename); } return kJTrue; }
JError JExecute ( const JCharacter* workingDirectory, const JCharacter* argv[], const JSize size, pid_t* childPID, const JExecuteAction toAction, int* toFD, const JExecuteAction fromAction, int* fromFD, const JExecuteAction errAction, int* errFD ) { const JString origPath = JGetCurrentDirectory(); JError err = JChangeDirectory(workingDirectory); if (err.OK()) { err = JExecute(argv, size, childPID, toAction, toFD, fromAction, fromFD, errAction, errFD); JChangeDirectory(origPath); } return err; }
JError JDirInfo::BuildInfo() { if (!JDirectoryReadable(*itsCWD)) { return JAccessDenied(*itsCWD); } const JString origDir = JGetCurrentDirectory(); JError err = JChangeDirectory(*itsCWD); if (!err.OK()) { return err; } // clear old information itsDirEntries->CleanOut(); // update instance variables JStripTrailingDirSeparator(itsCWD); // keep Windows happy ACE_stat stbuf; ACE_OS::stat(*itsCWD, &stbuf); itsIsValidFlag = kJTrue; itsIsWritableFlag = JDirectoryWritable(*itsCWD); itsModTime = stbuf.st_mtime; itsStatusTime = stbuf.st_ctime; JAppendDirSeparator(itsCWD); // process files in the directory JLatentPG pg(itsContentRegex == NULL ? 100 : 10); if (itsPG != NULL) { pg.SetPG(itsPG, kJFalse); } pg.VariableLengthProcessBeginning("Scanning directory...", kJTrue, kJFalse); BuildInfo1(pg); pg.ProcessFinished(); err = JChangeDirectory(origDir); assert_ok( err ); ApplyFilters(kJFalse); return JNoError(); }
JString JGetUniqueDirEntryName ( const JCharacter* path, const JCharacter* namePrefix, const JCharacter* nameSuffix, const JIndex startIndex ) { assert( !JStringEmpty(namePrefix) ); JString fullPath; if (JStringEmpty(path)) { if (!JGetTempDirectory(&fullPath)) { fullPath = JGetCurrentDirectory(); } } else { const JBoolean ok = JConvertToAbsolutePath(path, NULL, &fullPath); assert( ok ); } assert( JDirectoryExists(fullPath) ); const JString prefix = JCombinePathAndName(fullPath, namePrefix); JString name; for (JIndex i=startIndex; i<=kJIndexMax; i++) { name = prefix; if (i > 1) { name += JString(i, JString::kBase10); } if (!JStringEmpty(nameSuffix)) { name += nameSuffix; } if (!JNameUsed(name)) { break; } } return name; }
JBoolean JConvertToAbsolutePath ( const JCharacter* path, const JCharacter* base, // can be NULL JString* result ) { assert( !JStringEmpty(path) && result != NULL ); JBoolean ok = kJTrue; if (path[0] == '/') { *result = path; } else if (path[0] == '~') { ok = JExpandHomeDirShortcut(path, result); } else if (!JStringEmpty(base)) { *result = JCombinePathAndName(base, path); } else { const JString currDir = JGetCurrentDirectory(); *result = JCombinePathAndName(currDir, path); } if (ok) { return JNameUsed(*result); } else { result->Clear(); return kJFalse; } }
JBoolean JGetTrueName ( const JCharacter* name, JString* trueName ) { trueName->Clear(); if (!JNameUsed(name)) { return kJFalse; } // check if it is a directory else if (JDirectoryExists(name)) { const JString currPath = JGetCurrentDirectory(); JError err = JChangeDirectory(name); if (!err.OK()) { return kJFalse; } *trueName = JGetCurrentDirectory(); err = JChangeDirectory(currPath); assert_ok( err ); return kJTrue; } // it is a file, socket, fifo, etc. else { JString origPath, fileName; JSplitPathAndName(name, &origPath, &fileName); // get true directory JString truePath; if (!JGetTrueName(origPath, &truePath)) { return kJFalse; } // resolve symbolic link JString target; if ((JGetSymbolicLinkTarget(name, &target)).OK()) { if (JIsRelativePath(target)) { target.Prepend(truePath); } return JGetTrueName(target, trueName); } else { *trueName = JCombinePathAndName(truePath, fileName); return kJTrue; } } }
JXApplication::JXApplication ( int* argc, char* argv[], const JCharacter* appSignature, const JCharacter** defaultStringData ) : JXDirector(NULL), itsIgnoreDisplayDeletedFlag(kJFalse), itsIgnoreTaskDeletedFlag(kJFalse), itsSignature(appSignature), itsRestartCmd(argv[0]) { // initialize object itsDisplayList = new JPtrArray<JXDisplay>(JPtrArrayT::kDeleteAll); assert( itsDisplayList != NULL ); itsCurrDisplayIndex = 1; itsIdleTaskStack = new IdleTaskStack(JPtrArrayT::kDeleteAll); assert( itsIdleTaskStack != NULL ); itsIdleTasks = new JPtrArray<JXIdleTask>(JPtrArrayT::kDeleteAll); assert( itsIdleTasks != NULL ); itsCurrentTime = 0; itsMaxSleepTime = 0; itsLastIdleTime = 0; itsLastIdleTaskTime = 0; itsWaitForChildCounter = 0; itsUrgentTasks = new JPtrArray<JXUrgentTask>(JPtrArrayT::kDeleteAll); assert( itsUrgentTasks != NULL ); itsHasBlockingWindowFlag = kJFalse; itsHadBlockingWindowFlag = kJFalse; itsRequestQuitFlag = kJFalse; // if no path info specified, assume it's on exec path if (JIsRelativePath(itsRestartCmd) && itsRestartCmd.Contains(ACE_DIRECTORY_SEPARATOR_STR)) { const JString pwd = JGetCurrentDirectory(); itsRestartCmd = JCombinePathAndName(pwd, itsRestartCmd); } // initialize global objects JXCreateGlobals(this, appSignature, defaultStringData); ListenTo(JThisProcess::Instance()); // for SIGTERM // create display -- requires JXGetApplication() to work JString displayName; ParseBaseOptions(argc, argv, &displayName); JXDisplay* display; if (!JXDisplay::Create(displayName, &display)) { cerr << argv[0]; if (displayName.IsEmpty()) { cerr << ": Can't open display '" << XDisplayName(NULL) << '\''; } else { cerr << ": Can't open display '" << displayName << '\''; } cerr << endl; JThisProcess::Exit(1); } // start the timer #ifndef WIN32 itimerval timerInfo; timerInfo.it_interval.tv_sec = kTimerStart; timerInfo.it_interval.tv_usec = 0; timerInfo.it_value.tv_sec = kTimerStart; timerInfo.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &timerInfo, NULL); #endif // idle task to quit if add directors deactivated JXQuitIfAllDeactTask* task = new JXQuitIfAllDeactTask; assert( task != NULL ); task->Start(); }
JBoolean JXPathInput::InputValid() { if (itsAllowInvalidPathFlag) { return kJTrue; } else if (!JXInputField::InputValid()) { return kJFalse; } const JString& text = GetText(); if (text.IsEmpty()) // paranoia -- JXInputField should have reported { return !IsRequired(); } JString path; if (JIsRelativePath(text) && !HasBasePath()) { (JGetUserNotification())->ReportError(JGetString(kNoRelPathID)); RecalcAll(kJTrue); return kJFalse; } if (!JConvertToAbsolutePath(text, itsBasePath, &path)) { (JGetUserNotification())->ReportError(JGetString(kInvalidPathID)); RecalcAll(kJTrue); return kJFalse; } const JString currDir = JGetCurrentDirectory(); const JError err = JChangeDirectory(path); JChangeDirectory(currDir); if (err.OK()) { if (!JDirectoryReadable(path)) { (JGetUserNotification())->ReportError(JGetString(kUnreadableID)); RecalcAll(kJTrue); return kJFalse; } else if (itsRequireWriteFlag && !JDirectoryWritable(path)) { (JGetUserNotification())->ReportError(JGetString(kUnwritableID)); RecalcAll(kJTrue); return kJFalse; } else { return kJTrue; } } const JCharacter* errID; if (err == kJAccessDenied) { errID = kAccessDeniedID; } else if (err == kJBadPath) { errID = kBadPathID; } else if (err == kJComponentNotDirectory) { errID = kCompNotDirID; } else { errID = kInvalidDirID; } (JGetUserNotification())->ReportError(JGetString(errID)); RecalcAll(kJTrue); return kJFalse; }
JBoolean JProgramAvailable ( const JCharacter* programName, JString* fixedName ) { if (JStringEmpty(programName) || !JExpandHomeDirShortcut(programName, fixedName)) { return kJFalse; } if (fixedName->GetFirstCharacter() == '/') { return JFileExecutable(*fixedName); } JString fullName = programName; if (fullName.Contains("/")) { const JString dir = JGetCurrentDirectory(); fullName = JCombinePathAndName(dir, fullName); return JFileExecutable(fullName); } // check each directory in the exec path list const JCharacter* cpath = getenv("PATH"); JString path(cpath == NULL ? "" : cpath); if (theIncludeCWDOnPathFlag) { path.Prepend(".:"); } if (path.IsEmpty()) { return kJFalse; } JIndex colonIndex; while (path.LocateSubstring(":", &colonIndex)) { if (colonIndex > 1) { const JString dir = path.GetSubstring(1, colonIndex-1); fullName = JCombinePathAndName(dir, programName); if (JFileExists(fullName) && JFileExecutable(fullName)) { if (dir == ".") { fixedName->Prepend("./"); // in case we added this to PATH } return kJTrue; } } path.RemoveSubstring(1, colonIndex); } if (path.IsEmpty()) { return kJFalse; } fullName = JCombinePathAndName(path, programName); return JFileExecutable(fullName); }
JError JRemoveVCS ( const JCharacter* fullName, const JBoolean sync, JProcess** returnP ) { if (returnP != NULL) { *returnP = NULL; } if (!JNameUsed(fullName)) { return JDirEntryDoesNotExist(fullName); } JString path, name; JSplitPathAndName(fullName, &path, &name); const JString origPath = JGetCurrentDirectory(); if (JChangeDirectory(path) != kJNoError) { return JAccessDenied(path); } JVCSType type = JGetVCSType(path); JError err = JNoError(); JBoolean tryPlain = kJFalse; JString cmd; JProcess* p = NULL; if (type == kJSVNType || type == kJGitType) { const JCharacter *binary = NULL; if (type == kJSVNType) { binary = "svn rm --force "; } else if (type == kJGitType) { binary = "git rm -rf "; } cmd = binary; cmd += JPrepArgForExec(name); err = JProcess::Create(&p, cmd); if (err.OK()) { p->WaitUntilFinished(); } if (p != NULL && !p->SuccessfulFinish()) { err = JAccessDenied(fullName); tryPlain = kJTrue; } } else if (type == kJUnknownVCSType) { tryPlain = kJTrue; } else { err = JUnsupportedVCS(fullName); } if (tryPlain && JKillDirectory(fullName, sync, returnP)) { err = JNoError(); } else if (tryPlain) { err = JAccessDenied(fullName); } delete p; JChangeDirectory(origPath); return err; }
JError JRenameVCS ( const JCharacter* oldFullName, const JCharacter* newFullName ) { if (!JNameUsed(oldFullName)) { return JDirEntryDoesNotExist(oldFullName); } JString oldPath, newPath, name; JSplitPathAndName(newFullName, &newPath, &name); JSplitPathAndName(oldFullName, &oldPath, &name); // must be second const JString origPath = JGetCurrentDirectory(); if (JChangeDirectory(oldPath) != kJNoError) { return JAccessDenied(oldPath); } JVCSType type1 = JGetVCSType(oldPath); JVCSType type2 = JGetVCSType(newPath); JError err = JNoError(); JBoolean tryPlain = kJFalse; JString cmd; JProcess* p = NULL; if (type1 != type2) { tryPlain = kJTrue; } else if (type1 == kJSVNType || type1 == kJGitType) { if (type1 == kJSVNType) { cmd = "svn mv --force "; cmd += JPrepArgForExec(oldFullName); cmd += " "; cmd += JPrepArgForExec(newFullName); } else if (type1 == kJGitType) { cmd = "git mv -f "; cmd += JPrepArgForExec(name); cmd += " "; cmd += JPrepArgForExec(newFullName); } err = JProcess::Create(&p, cmd); if (err.OK()) { p->WaitUntilFinished(); } if (p != NULL && !p->SuccessfulFinish()) { err = JAccessDenied(oldFullName, newFullName); tryPlain = kJTrue; } } else if (type1 == kJUnknownVCSType) { tryPlain = kJTrue; } else { err = JUnsupportedVCS(oldFullName); } if (tryPlain && JProgramAvailable("mv")) { cmd = "mv "; cmd += JPrepArgForExec(oldFullName); cmd += " "; cmd += JPrepArgForExec(newFullName); JSimpleProcess* p1; err = JSimpleProcess::Create(&p1, cmd); p = p1; if (err.OK()) { p->WaitUntilFinished(); if (!p->SuccessfulFinish()) { err = JAccessDenied(oldFullName, newFullName); } } } else if (tryPlain) { err = JRenameDirEntry(oldFullName, newFullName); } delete p; JChangeDirectory(origPath); return err; }
JString JGetClosestDirectory ( const JCharacter* origDirName, const JBoolean requireWrite, const JCharacter* basePath ) { assert( !JStringEmpty(origDirName) ); JString workingDir; if (!JStringEmpty(basePath)) { workingDir = basePath; JAppendDirSeparator(&workingDir); } else { workingDir = JGetCurrentDirectory(); } JString dirName = origDirName; JString homeDir; JSize homeLength; if (origDirName[0] == '~' && !JExpandHomeDirShortcut(origDirName, &dirName, &homeDir, &homeLength)) { return JGetRootDirectory(); } else if (JIsRelativePath(origDirName)) { dirName.Prepend(workingDir); } assert( !JIsRelativePath(dirName) ); JString newDir, junkName; while (!JDirectoryExists(dirName) || !JCanEnterDirectory(dirName) || !JDirectoryReadable(dirName) || (requireWrite && !JDirectoryWritable(dirName))) { JStripTrailingDirSeparator(&dirName); if (JIsRootDirectory(dirName)) { break; } JSplitPathAndName(dirName, &newDir, &junkName); dirName = newDir; } // convert back to partial path, if possible if (origDirName[0] == '~' && dirName.BeginsWith(homeDir)) { dirName.ReplaceSubstring(1, homeDir.GetLength(), origDirName, homeLength); } else if (JIsRelativePath(origDirName) && dirName.GetLength() > workingDir.GetLength() && dirName.BeginsWith(workingDir)) { dirName.RemoveSubstring(1, workingDir.GetLength()); } return dirName; }