JBoolean
SyGApplication::IsMountPoint
	(
	const JCharacter*	path,
	JMountType*			type
	)
	const
{
	if (!JNameUsed(path))
		{
		return kJFalse;
		}

	UpdateMountPointList();

	const JSize count = itsMountPointList->GetElementCount();
	for (JIndex i=1; i<=count; i++)
		{
		const JMountPoint info = itsMountPointList->GetElement(i);
		if (JSameDirEntry(path, *(info.path)))
			{
			if (type != NULL)
				{
				*type = info.type;
				}
			return kJTrue;
			}
		}

	return kJFalse;
}
JBoolean
JXGetNewDirDialog::OKToDeactivate()
{
	if (!JXGetStringDialog::OKToDeactivate())
		{
		return kJFalse;
		}
	else if (Cancelled())
		{
		return kJTrue;
		}

	const JString pathName = GetNewDirName();
	if (JDirectoryExists(pathName))
		{
		(JGetUserNotification())->ReportError(JGetString(kDirectoryExistsID));
		return kJFalse;
		}
	else if (JNameUsed(pathName))
		{
		(JGetUserNotification())->ReportError(JGetString(kNameUsedID));
		return kJFalse;
		}
	else
		{
		return kJTrue;
		}
}
Пример #3
0
void
SyGAddRecentFile
	(
	const JCharacter* fullname
	)
{
	JString recentDir;
	JString filename;
	JString path;
	if (SyGGetRecentFileDirectory(&recentDir) &&
		JSplitPathAndName(fullname, &path, &filename))
		{
		const JString recentFile = JCombinePathAndName(recentDir, filename);
		if (JNameUsed(recentFile))
			{
			JRemoveFile(recentFile);
			JCreateSymbolicLink(fullname, recentFile);
			return;
			}

		// remove oldest links such that only kRecentFileCount - 1 remain

		JDirInfo* info;
		if (JDirInfo::Create(recentDir, &info))
			{
			JBoolean changed = kJFalse;

			JSize count = info->GetEntryCount();
			for (JIndex i=1; i<=count; i++)
				{
				if (info->GetEntry(i).IsBrokenLink())
					{
					JRemoveFile(info->GetEntry(i).GetFullName());
					changed = kJTrue;
					}
				}

			if (changed)
				{
				info->ForceUpdate();
				}

			count = info->GetEntryCount();
			if (count >= kRecentFileCount)
				{
				info->ChangeSort(JDirEntry::CompareModTimes, JOrderedSetT::kSortDescending);
				for (JIndex i=count; i>=kRecentFileCount; i--)
					{
					JRemoveFile(info->GetEntry(i).GetFullName());
					}
				}

			// add new entry

			JCreateSymbolicLink(fullname, recentFile);
			}
		}
}
Пример #4
0
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;
}
Пример #5
0
JBoolean
JKillDirectory
	(
	const JCharacter* dirName
	)
{
	const JCharacter* argv[] = {"rm", "-rf", dirName, NULL};
	const JError err = JExecute(argv, sizeof(argv), NULL,
								kJIgnoreConnection, NULL,
								kJIgnoreConnection, NULL,
								kJTossOutput,       NULL);
	if (err.OK())
		{
		return JNegate( JNameUsed(dirName) );
		}
	else
		{
		return kJFalse;
		}
}
Пример #6
0
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;
		}
}
Пример #7
0
JError
JRenameDirEntry
	(
	const JCharacter* oldName,
	const JCharacter* newName
	)
{
	if (JNameUsed(newName))
		{
		return JDirEntryAlreadyExists(newName);
		}
	else if (JSameDirEntry(oldName, newName))
		{
		return JNoError();
		}

	jclear_errno();
	if (rename(oldName, newName) == 0)
		{
		return JNoError();
		}

	const int err = jerrno();
	if (err == EISDIR)
		{
		return JCantRenameFileToDirectory(oldName, newName);
		}
	else if (err == EXDEV)
		{
		return JCantRenameAcrossFilesystems();
		}
	else if (err == ENOTEMPTY)
		{
		return JCantRenameToNonemptyDirectory();
		}
	else if (err == EEXIST)
		{
		return JDirEntryAlreadyExists(newName);
		}
	else if (err == EBUSY)
		{
		return JFileBusy(newName);
		}
	else if (err == EINVAL)
		{
		return JDirectoryCantBeOwnChild();
		}
	else if (err == EMLINK)
		{
		return JTooManyLinks(oldName);
		}
	else if (err == ENOTDIR)
		{
		return JComponentNotDirectory(oldName, newName);
		}
	else if (err == EFAULT)
		{
		return JSegFault();
		}
	else if (err == EACCES || err == EPERM)
		{
		return JAccessDenied(oldName, newName);
		}
	else if (err == ENAMETOOLONG)
		{
		return JNameTooLong();
		}
	else if (err == ENOENT)
		{
		return JBadPath(oldName, newName);
		}
	else if (err == ENOMEM)
		{
		return JNoKernelMemory();
		}
	else if (err == EROFS)
		{
		return JFileSystemReadOnly();
		}
	else if (err == ELOOP)
		{
		return JPathContainsLoop(oldName, newName);
		}
	else if (err == ENOSPC)
		{
		return JFileSystemFull();
		}
	else
		{
		return JUnexpectedError(err);
		}
}
Пример #8
0
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;
			}
		}
}
Пример #9
0
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;
}
Пример #10
0
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;
}