Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDEV-5269 getTempDirectory, add unique option #2483

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,59 @@
*/
package lucee.runtime.functions.system;

import java.io.IOException;

import lucee.runtime.PageContext;
import lucee.runtime.exp.ExpressionException;
import lucee.runtime.exp.PageException;
import lucee.runtime.ext.function.Function;
import lucee.runtime.functions.other.CreateUniqueId;
import lucee.runtime.op.Decision;
import lucee.runtime.op.Caster;
import lucee.commons.io.res.Resource;
import lucee.commons.lang.StringUtil;
import lucee.commons.io.SystemUtil;

public final class GetTempDirectory implements Function {

private static final long serialVersionUID = -166719664831864953L;
private static final int MAX_RETRY_TEMP = 3;

public static String call(PageContext pc) {
String fs = System.getProperty("file.separator");
String path = pc.getConfig().getTempDirectory().getAbsolutePath();

if (path.lastIndexOf(fs) != path.length() - 1) path += fs;
return path;
}

public static String call(PageContext pc, String unique) throws PageException{
boolean isUnique = Decision.isCastableToBoolean(unique);
if (isUnique && !Caster.toBoolean(unique, false)) return call(pc);

if (isUnique) unique = "";
else if (!StringUtil.isEmpty(unique)) unique = unique.trim() + "_";
else unique = "";

String fs = System.getProperty("file.separator");
Resource path = pc.getConfig().getTempDirectory();

int max = MAX_RETRY_TEMP;
while (max-- > 0) {
Resource dir = path.getRealResource(fs + unique + Long.toString(System.currentTimeMillis(), Character.MAX_RADIX) + "_" + CreateUniqueId.invoke() );
synchronized (SystemUtil.createToken("", dir.getAbsolutePath())) {
try {
if (dir.exists()) continue;
dir.createDirectory(false);
String uniqueDir = dir.getCanonicalPath();
if (uniqueDir.lastIndexOf(fs) != uniqueDir.length() - 1) uniqueDir += fs;
return uniqueDir;
}
catch (IOException e) {
}
}
}
throw new ExpressionException("Unable to create unique temporary directory");
}

}
10 changes: 9 additions & 1 deletion core/src/main/java/resource/fld/core-base.fld
Original file line number Diff line number Diff line change
Expand Up @@ -6938,7 +6938,15 @@ if this attribute is not provided all types are returned.</description>
<function>
<name>getTempDirectory</name>
<class>lucee.runtime.functions.system.GetTempDirectory</class>
<description>Returns the full path to the currently assigned temporary directory</description>
<description>Returns the full path to the currently assigned temporary directory, optionally creating a unique temp directory</description>
<argument>
<name>unique</name>
<alias>prefix</alias>
<type>string</type>
<default>false</default>
<required>no</required>
<description>If true or not empty, creates and returns a unique temp directory, optionally prefixed</description>
</argument>
<return>
<type>string</type>
</return>
Expand Down
70 changes: 70 additions & 0 deletions test/functions/GetTempDirectory.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<cfscript>
component extends="org.lucee.cfml.test.LuceeTestCase" {

function run( testResults , testBox ) {
describe( "test case for getTempDirectory()", function() {

it (title = "getTempDirectory()", body = function( currentSpec ) {
var tmp = getTempDirectory();
var tmp2 = getTempDirectory();

expect( tmp ).toBe( tmp2 );
expect( DirectoryExists( tmp ) ).toBeTrue();
expect( right( tmp, 1 ) ).toBe( server.separator.file );
});

it (title = "getTempDirectory(false)", body = function( currentSpec ) {
var tmp = getTempDirectory( false );
var tmp2 = getTempDirectory( "false" );

expect( tmp ).toBe( tmp2 );
expect( DirectoryExists( tmp ) ).toBeTrue();
});

it (title = "getTempDirectory(unique=true)", body = function( currentSpec ) {
var tmp = getTempDirectory();
var tmp2 = getTempDirectory(unique=true);
var tmp3 = getTempDirectory(unique=true);

expect( tmp ).notToBe( tmp2 );
expect( tmp2 ).notToBe( tmp3 );

expect( DirectoryExists( tmp2 ) ).toBeTrue();
expect( DirectoryExists( tmp3 ) ).toBeTrue();
});

it (title = "getTempDirectory(true)", body = function( currentSpec ) {
var tmp = getTempDirectory( false );
var tmp2 = getTempDirectory( true );
var tmp3 = getTempDirectory( "true" );

expect( tmp ).notToBe( tmp2 );
expect( tmp2 ).notToBe( tmp3 );
expect( DirectoryExists( tmp2 ) ).toBeTrue();
expect( DirectoryExists( tmp3 ) ).toBeTrue();
});

it (title = "getTempDirectory('ldev')", body = function( currentSpec ) {
var prefix = "ldev";
var tmp = getTempDirectory( false );
var tmp2 = getTempDirectory( prefix );
var tmp3 = getTempDirectory( prefix );

expect( tmp ).notToBe( tmp2 );
expect( tmp2 ).notToBe( tmp3 );

expect( DirectoryExists( tmp2 ) ).toBeTrue();
expect( DirectoryExists( tmp3 ) ).toBeTrue();

expect( tmp2 ).toInclude( tmp & prefix );
expect( tmp3 ).toInclude( tmp & prefix );

expect( right( tmp2, 1 ) ).toBe( server.separator.file );
expect( right( tmp3, 1 ) ).toBe( server.separator.file );
});

});
}

}
</cfscript>
Loading