Skip to content

Commit

Permalink
Merge pull request #2478 from zspitzer/LDEV-3312-fileSetAttribute
Browse files Browse the repository at this point in the history
LDEV-3312 use nio for fileSetAttribute readonly
  • Loading branch information
zspitzer authored Jan 21, 2025
2 parents 52ff7e7 + d15d62f commit fe2fbc1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ public boolean setWritable(boolean value) {

try {
provider.lock(this);
Runtime.getRuntime().exec("attrib -R " + getAbsolutePath());
Path path = Paths.get(getPath());
Files.setAttribute(path, "dos:readonly", !value);
}
catch (IOException ioe) {
return false;
Expand Down
7 changes: 3 additions & 4 deletions test/functions/FileSetAttribute.cfc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
component extends="org.lucee.cfml.test.LuceeTestCase"{
function beforeAll(){
variables.base = GetDirectoryFromPath( getCurrentTemplatepath() );
variables.path = base & "fileSetAttribute";
variables.path = getTempDirectory() & "fileSetAttribute\";
if ( !directoryExists( variables.path ) ){
directoryCreate( variables.path );
}
Expand All @@ -10,7 +9,7 @@ component extends="org.lucee.cfml.test.LuceeTestCase"{
function isNotSupported() {
var isWindows =find("Windows", server.os.name );
if (isWindows > 0 ) return false;
else return true;
return true;
}

function afterAll(){
Expand Down Expand Up @@ -62,7 +61,7 @@ component extends="org.lucee.cfml.test.LuceeTestCase"{
});
});
describe( "Testcase for LDEV-2410", function() {
xit( title = "Checking changing file attribute between NORMAL and READONLY", body = function( currentSpec ) {
it( title = "Checking changing file attribute between NORMAL and READONLY", body = function( currentSpec ) {
var testFile = path & "\ro_normal_LDEV2410_#CreateUUID()#.txt";
FileWrite(testFile, "I am in normal file");

Expand Down
24 changes: 13 additions & 11 deletions test/tickets/LDEV1880.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ component extends="org.lucee.cfml.test.LuceeTestCase"{
}

function beforeAll(){
variables.base = GetDirectoryFromPath(getcurrentTemplatepath());
variables.base = getTempDirectory();
variables.path = base&"LDEV1880\example.txt";
if(!directoryExists(base&"LDEV1880")){
directoryCreate(base&'LDEV1880');
Expand Down Expand Up @@ -34,32 +34,34 @@ component extends="org.lucee.cfml.test.LuceeTestCase"{

it(title = "checking the file with Archive Attribute", body = function( currentSpec ) {
fileSetAttribute(path,'Archive');
expect(getfileinfo(path).isArchive).toBe('true');
expect( getFileInfo(path).isArchive ).toBe('true');
});

it(title = "checking the file with System Attribute", body = function( currentSpec ) {
fileSetAttribute(path,'System');
expect(getfileinfo(path).isSystem).toBe('true');
expect( getFileInfo(path).isSystem ).toBe('true');
});

it(title = "checking the file with readOnly Attribute", body = function( currentSpec ) {
fileSetAttribute(path,'readOnly');
expect(getfileinfo(path).canRead).toBe('true');
expect(getfileinfo(path).canWrite).toBe('false');
var info = getFileInfo( path );
expect( info.canRead ).toBe('true');
expect( info.canWrite ).toBe('false');
});

it(title = "checking the file with Hidden Attribute", body = function( currentSpec ) {
fileSetAttribute(path,'Hidden');
expect(getfileinfo(path).isHidden).toBe('true');
expect( getFileInfo( path ).isHidden ).toBe('true');
});

it(title = "checking the file with Normal Attribute", body = function( currentSpec ) {
fileSetAttribute(path,'Normal');
expect(getfileinfo(path).canRead).toBe('true');
expect(getfileinfo(path).canWrite).toBe('true');
expect(getfileinfo(path).isHidden).toBe('false');
expect(getfileinfo(path).isSystem).toBe('false');
expect(getfileinfo(path).isArchive).toBe('false');
var info = getFileInfo( path );
expect( info.canRead ) .toBe('true');
expect( info.canWrite ).toBe('true');
expect( info.isHidden ).toBe('false');
expect( info.isSystem ).toBe('false');
expect( info.isArchive ).toBe('false');
});
});
}
Expand Down
69 changes: 50 additions & 19 deletions test/tickets/LDEV2410.cfc
Original file line number Diff line number Diff line change
@@ -1,34 +1,65 @@
component extends="org.lucee.cfml.test.LuceeTestCase"{
function beforeAll(){
variables.base = GetDirectoryFromPath(getcurrentTemplatepath());
variables.path = base&"LDEV2410\example.txt";
if(!directoryExists(base&"LDEV2410")){
directoryCreate(base&'LDEV2410');
}
variables.testfile = getTempFile( getTempDirectory(), "LDEV-2410", ".txt" );
}

function run( testResults, testBox ){
describe( "test case for LDEV-2410", function() {

it(title = "checking the file with READONLY Attribute", body = function( currentSpec ) {
variables.myfile = FileOpen(path, "write");
FileWrite(path,"I am in readonly file");
fileSetAttribute(path,'readonly');
expect(getfileinfo(path).canRead).toBe(true);
expect(getfileinfo(path).canWrite).toBe(false);
if (!isWindows()) return;
FileWrite( testfile, "I am a writeable file" );

FileSetAttribute( testfile, 'readonly' );
var info = getFileInfo( testfile );
expect( info.canRead ).toBe( true );
expect( info.canWrite ).toBe( false );
expect (function(){
FileWrite( testfile, "I am in readonly file" );
}).toThrow();
});

it(title = "checking the file with NORMAL Attribute", body = function( currentSpec ) {
if (!isWindows()) return;
FileSetAttribute( testfile, 'normal' );
var info = getFileInfo( testfile );
expect( info.canWrite ).toBe( true );
FileWrite( testfile, "I am in normal (writable) file" );
});

it(title = "checking settting a file to readOnly on linux", body = function( currentSpec ) {
if (isWindows()) return;

var info = getFileInfo( testfile );
expect( info.canWrite ).toBe( true );
FileWrite( testfile, "I am in normal (writable) file" );

FileSetAccessMode( testfile, "444" ); // i.e. readonly
info = getFileInfo( testfile );
expect( info.canWrite ).toBe( false);
expect (function(){
FileWrite( testfile, "I am in readonly file" );
}).toThrow();

FileSetAccessMode( testfile, "644" );
info = getFileInfo( testfile );
expect( info.canWrite ).toBe( true );
expect (function(){
FileWrite( testfile, "I am in normal (writable) file" );
}).notToThrow();

});
// this fails on windows, disabling
it(title = "checking the file with NORMAL Attribute", skip=true, body = function( currentSpec ) {
fileSetAttribute(path,'normal');
FileWrite(path,"I am in normal file");
expect(getfileinfo(path).canRead).toBe(true);
expect(getfileinfo(path).canWrite).toBe(true);
});
});
}

function afterAll(){
if(directoryExists(base&"LDEV2410")){
directoryDelete(base&"LDEV2410",true);
if ( FileExists( variables.testfile ) ) {
FileDelete( variables.testfile );
}
}

private function isWindows(){
return (server.os.name contains "windows");
}

}

0 comments on commit fe2fbc1

Please sign in to comment.