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

Cfdirectory mode is now applied to new parent dirs with createPath=true LDEV-3506 #1326

Closed
wants to merge 3 commits into from
Closed
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
59 changes: 48 additions & 11 deletions core/src/main/java/lucee/commons/io/ModeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
**/
package lucee.commons.io;

import java.io.IOException;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

public final class ModeUtil {

Expand All @@ -32,7 +34,7 @@ public final class ModeUtil {

/**
* translate a string mode (777 or drwxrwxrwx to an octal value)
*
*
* @param strMode
* @return
*/
Expand All @@ -43,6 +45,41 @@ public static int toOctalMode(String strMode) throws IOException {
throw new IOException("can't translate [" + strMode + "] to a mode value");
}

public static int posixToOctalMode(Set<PosixFilePermission> filePermissions) {
int mode = 0100000;
mode += 0100 * _posixToOctalMode(
filePermissions.contains(PosixFilePermission.OWNER_READ),
filePermissions.contains(PosixFilePermission.OWNER_WRITE),
filePermissions.contains(PosixFilePermission.OWNER_EXECUTE));

//noinspection OctalInteger
mode += 010 * _posixToOctalMode(
filePermissions.contains(PosixFilePermission.GROUP_READ),
filePermissions.contains(PosixFilePermission.GROUP_WRITE),
filePermissions.contains(PosixFilePermission.GROUP_EXECUTE));

mode += _posixToOctalMode(
filePermissions.contains(PosixFilePermission.OTHERS_READ),
filePermissions.contains(PosixFilePermission.OTHERS_WRITE),
filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE));

return mode;
}

private static int _posixToOctalMode(boolean read, boolean write, boolean execute) {
int result = 0;
if (read) {
result += 4;
}
if (write) {
result += 2;
}
if (execute) {
result += 1;
}
return result;
}

private static int _toOctalMode(String strMode) {
int index;
strMode = strMode.trim().toLowerCase();
Expand All @@ -68,7 +105,7 @@ private static int _toOctalMode(String strMode) {

/**
* translate an octal mode value (73) to a string representation ("111")
*
*
* @param strMode
* @return
*/
Expand All @@ -81,7 +118,7 @@ public static String toStringMode(int octalMode) {

/**
* update a string mode with another (111+222=333 or 333+111=333 or 113+202=313)
*
*
* @param existing
* @param update
* @return
Expand All @@ -93,7 +130,7 @@ public static String updateMode(String existing, String update) throws IOExcepti

/**
* update octal mode with another
*
*
* @param existingOctal
* @param updateOctal
* @return
Expand All @@ -105,7 +142,7 @@ public static int updateMode(int existingOctal, int updateOctal) {

/**
* check mode for a specific permission
*
*
* @param role
* @param permission
* @param mode
Expand All @@ -117,7 +154,7 @@ public static boolean hasPermission(int role, int permission, int mode) {

/**
* check if mode is readable for owner
*
*
* @param octalMode
* @return
*/
Expand All @@ -127,7 +164,7 @@ public static boolean isReadable(int octalMode) {

/**
* check if mode is writeable for owner
*
*
* @param octalMode
* @return
*/
Expand All @@ -137,7 +174,7 @@ public static boolean isWritable(int octalMode) {

/**
* check if mode is executable for owner
*
*
* @param octalMode
* @return
*/
Expand Down
95 changes: 55 additions & 40 deletions core/src/main/java/lucee/commons/io/res/type/file/FileResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package lucee.commons.io.res.type.file;

import lucee.commons.io.IOUtil;
import lucee.commons.io.ModeUtil;
import lucee.commons.io.SystemUtil;
import lucee.commons.io.res.ContentType;
import lucee.commons.io.res.Resource;
import lucee.commons.io.res.ResourceProvider;
import lucee.commons.io.res.filter.ResourceFilter;
import lucee.commons.io.res.filter.ResourceNameFilter;
import lucee.commons.io.res.util.ResourceOutputStream;
import lucee.commons.io.res.util.ResourceUtil;
import lucee.commons.lang.ExceptionUtil;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
Expand All @@ -26,25 +38,18 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.CopyOption;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.List;

import lucee.commons.cli.Command;
import lucee.commons.io.IOUtil;
import lucee.commons.io.ModeUtil;
import lucee.commons.io.SystemUtil;
import lucee.commons.io.res.ContentType;
import lucee.commons.io.res.Resource;
import lucee.commons.io.res.ResourceProvider;
import lucee.commons.io.res.filter.ResourceFilter;
import lucee.commons.io.res.filter.ResourceNameFilter;
import lucee.commons.io.res.util.ResourceOutputStream;
import lucee.commons.io.res.util.ResourceUtil;
import lucee.commons.lang.ExceptionUtil;
import java.util.Set;

/**
* Implementation og Resource for the local filesystem (java.io.File)
Expand All @@ -58,7 +63,7 @@ public final class FileResource extends File implements Resource {

/**
* Constructor for the factory
*
*
* @param pathname
*/
FileResource(FileResourceProvider provider, String pathname) {
Expand All @@ -68,7 +73,7 @@ public final class FileResource extends File implements Resource {

/**
* Inner Constr constructor to create parent/child
*
*
* @param parent
* @param child
*/
Expand Down Expand Up @@ -390,40 +395,46 @@ public int getMode() {
if (SystemUtil.isUnix()) {
try {
// TODO geht nur fuer file
String line = Command.execute("ls -ld " + getPath(), false).getOutput();

line = line.trim();
line = line.substring(0, line.indexOf(' '));
// print.ln(getPath());
return ModeUtil.toOctalMode(line);
Path path = FileSystems.getDefault().getPath(getPath());
Set<PosixFilePermission> filePermissions = Files.getPosixFilePermissions(path);

return ModeUtil.posixToOctalMode(filePermissions);
}
catch (Exception e) {
}

}

int mode = SystemUtil.isWindows() && exists() ? 0111 : 0;
if (super.canRead()) mode += 0444;
if (super.canWrite()) mode += 0222;
return mode;
}

private static int getModeFromPermissions(boolean read, boolean write, boolean execute) {
int result = 0;
if (read) {
result += 4;
}
if (write) {
result += 2;
}
if (execute) {
result += 1;
}
return result;
}

@Override
public void setMode(int mode) throws IOException {
// TODO unter windows mit setReadable usw.
if (!SystemUtil.isUnix()) return;
provider.lock(this);
try {
// print.ln(ModeUtil.toStringMode(mode));
if (Runtime.getRuntime().exec(new String[] { "chmod", ModeUtil.toStringMode(mode), getPath() }).waitFor() != 0)
throw new IOException("chmod [" + ModeUtil.toStringMode(mode) + "] [" + toString() + "] failed");
}
catch (InterruptedException e) {
throw new IOException("Interrupted waiting for chmod [" + toString() + "]");
}
finally {
provider.unlock(this);
}

Path path = FileSystems.getDefault().getPath(getPath());
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString(ModeUtil.toStringMode(mode)));

provider.unlock(this);
}

@Override
Expand Down Expand Up @@ -484,7 +495,9 @@ public boolean setWritable(boolean value) {

try {
provider.lock(this);
Runtime.getRuntime().exec("attrib -R " + getAbsolutePath());
Path path = FileSystems.getDefault().getPath(getAbsolutePath());
DosFileAttributeView dosView = Files.getFileAttributeView(path, DosFileAttributeView.class);
dosView.setReadOnly(false);
}
catch (IOException ioe) {
return false;
Expand Down Expand Up @@ -733,15 +746,17 @@ public void setAttribute(short attribute, boolean value) throws IOException {
if (!SystemUtil.isWindows()) return;

provider.lock(this);
Path path = FileSystems.getDefault().getPath(getAbsolutePath());
DosFileAttributeView dosView = Files.getFileAttributeView(path, DosFileAttributeView.class);
try {
if (attribute == ATTRIBUTE_ARCHIVE) {
Files.setAttribute(this.toPath(), "dos:archive", value);
dosView.setArchive(value);
}
else if (attribute == ATTRIBUTE_HIDDEN) {
Files.setAttribute(this.toPath(), "dos:hidden", value);
dosView.setHidden(value);
}
else if (attribute == ATTRIBUTE_SYSTEM) {
Files.setAttribute(this.toPath(), "dos:system", value);
dosView.setSystem(value);
}
}
catch (IOException e) {
Expand All @@ -758,4 +773,4 @@ public boolean equals(Object other) {
if (!(other instanceof File)) return false;
return getAbsolutePath().equalsIgnoreCase(((File) other).getAbsolutePath());
}
}
}
15 changes: 15 additions & 0 deletions core/src/main/java/lucee/runtime/tag/Directory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lucee.commons.io.ModeUtil;
import lucee.commons.io.res.Resource;
Expand Down Expand Up @@ -645,7 +647,15 @@ public static void actionCreate(PageContext pc, Resource directory, String serve
}
// if(!directory.mkdirs()) throw new ApplicationException("can't create directory
// ["+directory.toString()+"]");
List<Resource> files = new ArrayList<>();
try {
if(createPath) {
Resource parent = directory.getParentResource();
while (!parent.exists()) {
files.add(parent);
parent = parent.getParentResource();
}
}
directory.createDirectory(createPath);
}
catch (IOException ioe) {
Expand All @@ -658,6 +668,11 @@ public static void actionCreate(PageContext pc, Resource directory, String serve
// Set Mode
if (mode != -1) {
try {
if(createPath) {
for(Resource file : files) {
file.setMode(mode);
}
}
directory.setMode(mode);
// FileUtil.setMode(directory,mode);
}
Expand Down