Skip to content

Commit

Permalink
LDEV-3312 use nio for fileSetAttribute readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Jan 21, 2025
1 parent 52ff7e7 commit 1331771
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,6 @@ public boolean setReadable(boolean value) {

@Override
public boolean setWritable(boolean value) {
// setReadonly
if (!value) {
try {
provider.lock(this);
if (!super.setReadOnly()) throw new IOException("Can't set resource read-only");
}
catch (IOException ioe) {
return false;
}
finally {
provider.unlock(this);
}
return true;
}

if (SystemUtil.isUnix()) {
// need no lock because get/setmode has one
Expand All @@ -543,7 +529,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
39 changes: 20 additions & 19 deletions test/tickets/LDEV2410.cfc
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
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);
FileWrite( testfile, "I am in readonly 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 ) {
FileSetAttribute( testfile, 'normal' );
info = getFileInfo( testfile );
expect( info.canWrite ).toBe( true );
FileWrite( testfile, "I am in normal (writable) file" );
});
// 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 );
}
}

}

0 comments on commit 1331771

Please sign in to comment.