Apply proper owner, group and permissions when creating a new file or directory.
git-svn-id: svn://svn.code.sf.net/p/mockftpserver/code@140 531de8e6-9941-0410-b38b-9a92acbe0330
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandler.groovy
index 47bef51..291d987 100644
--- a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandler.groovy
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandler.groovy
@@ -67,6 +67,7 @@
file = new FileEntry(path)
fileSystem.add(file)
}
+ file.permissions = getUserAccount(session).defaultPermissionsForNewFile
if (contents) {
def out = file.createOutputStream(appendToOutputFile())
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/MkdCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/MkdCommandHandler.groovy
index cfac96a..dcc6a85 100644
--- a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/MkdCommandHandler.groovy
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/MkdCommandHandler.groovy
@@ -53,7 +53,10 @@
// User must have execute permission to the parent directory
verifyExecutePermission(session, parent)
- fileSystem.add(new DirectoryEntry(path))
+ DirectoryEntry dirEntry = new DirectoryEntry(path)
+ fileSystem.add(dirEntry)
+ dirEntry.permissions = getUserAccount(session).defaultPermissionsForNewDirectory
+
sendReply(session, ReplyCodes.MKD_OK, 'mkd', [path])
}
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/user/UserAccount.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/user/UserAccount.groovy
index 5bd21b2..da01241 100644
--- a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/user/UserAccount.groovy
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/user/UserAccount.groovy
@@ -16,6 +16,7 @@
package org.mockftpserver.fake.user
import org.mockftpserver.fake.filesystem.FileSystemEntry
+import org.mockftpserver.fake.filesystem.Permissions
/**
* Represents a single user account on the server, including the username, password and home
@@ -40,6 +41,8 @@
public static final DEFAULT_USER = 'system'
public static final DEFAULT_GROUP = 'users'
+ public static final DEFAULT_PERMISSIONS_FOR_NEW_FILE = new Permissions('rw-rw-rw-')
+ public static final DEFAULT_PERMISSIONS_FOR_NEW_DIRECTORY = Permissions.ALL
String username
String password
@@ -48,6 +51,8 @@
boolean passwordRequiredForLogin = true
boolean passwordCheckedDuringValidation = true
boolean accountRequiredForLogin = false
+ Permissions defaultPermissionsForNewFile = DEFAULT_PERMISSIONS_FOR_NEW_FILE
+ Permissions defaultPermissionsForNewDirectory = DEFAULT_PERMISSIONS_FOR_NEW_DIRECTORY
/**
* Return the name of the primary group to which this user belongs. If this account has no associated
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandlerTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandlerTest.groovy
index 3ccee53..cacb9d5 100644
--- a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandlerTest.groovy
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AbstractStoreFileCommandHandlerTest.groovy
@@ -16,7 +16,6 @@
package org.mockftpserver.fake.command
import org.mockftpserver.core.command.Command
-import org.mockftpserver.core.command.CommandHandler
import org.mockftpserver.core.command.CommandNames
import org.mockftpserver.core.command.ReplyCodes
import org.mockftpserver.fake.filesystem.FileEntry
@@ -24,6 +23,7 @@
import org.mockftpserver.fake.filesystem.FileSystemException
import org.mockftpserver.fake.filesystem.Permissions
+
/**
* Abstract superclass for tests of Fake CommandHandlers that store a file (STOR, STOU, APPE)
*
@@ -96,12 +96,10 @@
def outputFile = verifyOutputFile()
- def actualContents = fileSystem.getEntry(outputFile).createInputStream().text
+ FileSystemEntry fileEntry = fileSystem.getEntry(outputFile)
+ def actualContents = fileEntry.createInputStream().text
assert actualContents == contents
- }
-
- CommandHandler createCommandHandler() {
- new AppeCommandHandler()
+ assert fileEntry.permissions == userAccount.defaultPermissionsForNewFile
}
Command createValidCommand() {
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AppeCommandHandlerTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AppeCommandHandlerTest.groovy
index d26b52a..b253a96 100644
--- a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AppeCommandHandlerTest.groovy
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/AppeCommandHandlerTest.groovy
@@ -20,6 +20,7 @@
import org.mockftpserver.core.command.CommandNames
import org.mockftpserver.core.command.ReplyCodes
import org.mockftpserver.fake.filesystem.FileEntry
+import org.mockftpserver.fake.filesystem.Permissions
/**
* Tests for AppeCommandHandler
@@ -35,6 +36,7 @@
}
void testHandleCommand_AbsolutePath() {
+ userAccount.defaultPermissionsForNewFile = Permissions.NONE
testHandleCommand([FILE], 'appe', CONTENTS)
}
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/MkdCommandHandlerTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/MkdCommandHandlerTest.groovy
index 2ccd3a5..c9a7242 100644
--- a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/MkdCommandHandlerTest.groovy
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/MkdCommandHandlerTest.groovy
@@ -23,6 +23,7 @@
import org.mockftpserver.fake.filesystem.FileSystemEntry
import org.mockftpserver.fake.filesystem.FileSystemException
import org.mockftpserver.fake.filesystem.Permissions
+import org.mockftpserver.fake.user.UserAccount
/**
* Tests for MkdCommandHandler
@@ -36,11 +37,15 @@
static final PARENT = '/'
static final DIRNAME = "usr"
static final DIR = p(PARENT, DIRNAME)
+ static final PERMISSIONS = new Permissions('rwx------')
void testHandleCommand() {
+ userAccount.defaultPermissionsForNewDirectory = PERMISSIONS
handleCommand([DIR])
assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIR])
assert fileSystem.exists(DIR)
+ def dirEntry = fileSystem.getEntry(DIR)
+ assert dirEntry.permissions == PERMISSIONS
}
void testHandleCommand_PathIsRelative() {
@@ -48,6 +53,8 @@
handleCommand([DIRNAME])
assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIRNAME])
assert fileSystem.exists(DIR)
+ def dirEntry = fileSystem.getEntry(DIR)
+ assert dirEntry.permissions == UserAccount.DEFAULT_PERMISSIONS_FOR_NEW_DIRECTORY
}
void testHandleCommand_ParentDirectoryDoesNotExist() {
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/user/UserAccountTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/user/UserAccountTest.groovy
index 0ce713b..b25213c 100644
--- a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/user/UserAccountTest.groovy
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/user/UserAccountTest.groovy
@@ -164,6 +164,11 @@
testCanExecute(null, null, 'rwxrwxrw-', false)
}
+ void testDefaultPermissions() {
+ assert userAccount.defaultPermissionsForNewFile == new Permissions('rw-rw-rw-')
+ assert userAccount.defaultPermissionsForNewDirectory == Permissions.ALL
+ }
+
//--------------------------------------------------------------------------
// Helper Methods
//--------------------------------------------------------------------------