From d15d62feb5cd1b3e72aa790be9c4ee449faa0456 Mon Sep 17 00:00:00 2001 From: Zac Spitzer Date: Tue, 21 Jan 2025 13:38:03 +0100 Subject: [PATCH] LDEV-3312 use nio for fileSetAttribute readonly https://luceeserver.atlassian.net/browse/LDEV-3312 --- .../io/res/type/file/FileResource.java | 3 +- test/functions/FileSetAttribute.cfc | 7 +- test/tickets/LDEV1880.cfc | 24 ++++--- test/tickets/LDEV2410.cfc | 69 ++++++++++++++----- 4 files changed, 68 insertions(+), 35 deletions(-) diff --git a/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java b/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java index 482bce3ae8..d3ed50b681 100644 --- a/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java +++ b/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java @@ -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; diff --git a/test/functions/FileSetAttribute.cfc b/test/functions/FileSetAttribute.cfc index e288aa80b6..469f336f23 100644 --- a/test/functions/FileSetAttribute.cfc +++ b/test/functions/FileSetAttribute.cfc @@ -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 ); } @@ -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(){ @@ -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"); diff --git a/test/tickets/LDEV1880.cfc b/test/tickets/LDEV1880.cfc index 7131d14e9c..50bf7123f8 100644 --- a/test/tickets/LDEV1880.cfc +++ b/test/tickets/LDEV1880.cfc @@ -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'); @@ -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'); }); }); } diff --git a/test/tickets/LDEV2410.cfc b/test/tickets/LDEV2410.cfc index d211b61f7a..1db21b4910 100644 --- a/test/tickets/LDEV2410.cfc +++ b/test/tickets/LDEV2410.cfc @@ -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"); + } + } \ No newline at end of file