예제 #1
0
JError
JCreateTempFile
	(
	const JCharacter*	path,
	const JCharacter*	prefix,
	JString*			fullName
	)
{
	JString p;
	if (!JStringEmpty(path))
		{
		p = path;
		}
	else if (!JGetTempDirectory(&p))
		{
		return JDirEntryDoesNotExist("/tmp");
		}

	if (!JStringEmpty(prefix))
		{
		p = JCombinePathAndName(p, prefix);
		}
	else
		{
		p = JCombinePathAndName(p, "temp_file_");
		}

	p      += "XXXXXX";
	char* s = p.AllocateCString();

	jclear_errno();
	int fd = mkstemp(s);
	if (fd != -1)
		{
		close(fd);
		*fullName = s;
		delete [] s;
		return JNoError();
		}

	fullName->Clear();
	delete [] s;

	// EINVAL counts as unexpected

	const int err = jerrno();
	if (err == EEXIST)
		{
		return JAccessDenied(p);
		}
	else
		{
		return JUnexpectedError(err);
		}
}
예제 #2
0
JError
JCreateTempDirectory
	(
	const JCharacter*	path,
	const JCharacter*	prefix,
	JString*			fullName
	)
{
	JString p;
	if (!JStringEmpty(path))
		{
		p = path;
		}
	else if (!JGetTempDirectory(&p))
		{
		return JDirEntryDoesNotExist("/tmp");
		}

	if (!JStringEmpty(prefix))
		{
		p = JCombinePathAndName(p, prefix);
		}
	else
		{
		p = JCombinePathAndName(p, "temp_dir_");
		}

	p      += "XXXXXX";
	char* s = p.AllocateCString();

	jclear_errno();
	char* d = mkdtemp(s);
	if (d != NULL)
		{
		*fullName = s;
		JAppendDirSeparator(fullName);
		delete [] s;
		return JNoError();
		}

	fullName->Clear();
	delete [] s;

	// EINVAL counts as unexpected

	const int err = jerrno();
	if (err == EEXIST)
		{
		return JDirEntryAlreadyExists(p);
		}
	else if (err == EFAULT)
		{
		return JSegFault();
		}
	else if (err == EACCES)
		{
		return JAccessDenied(p);
		}
	else if (err == ENAMETOOLONG)
		{
		return JNameTooLong();
		}
	else if (err == ENOENT)
		{
		return JBadPath(p);
		}
	else if (err == ENOTDIR)
		{
		return JComponentNotDirectory(p);
		}
	else if (err == ENOMEM)
		{
		return JNoKernelMemory();
		}
	else if (err == EROFS)
		{
		return JFileSystemReadOnly();
		}
	else if (err == ELOOP)
		{
		return JPathContainsLoop(p);
		}
	else if (err == ENOSPC)
		{
		return JFileSystemFull();
		}
	else
		{
		return JUnexpectedError(err);
		}
}