-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathJavaNIODeviceContext.java
More file actions
216 lines (177 loc) · 7.37 KB
/
JavaNIODeviceContext.java
File metadata and controls
216 lines (177 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (C) 2019 GK Spencer
*
* JFileServer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JFileServer 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 JFileServer. If not, see <http://www.gnu.org/licenses/>.
*/
package org.filesys.smb.server.disk;
import org.filesys.debug.Debug;
import org.filesys.server.core.DeviceContextException;
import org.filesys.server.filesys.DiskDeviceContext;
import org.filesys.server.filesys.FileSystem;
import org.filesys.util.MemorySize;
import org.springframework.extensions.config.ConfigElement;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
/**
* Java NIO Filesystem Device Context Class
*/
public class JavaNIODeviceContext extends DiskDeviceContext {
// Constants
//
// Define the minimum large file size
private static final long MinimumLargeFileSize = MemorySize.MEGABYTE;
// Default large file size
private static final long DefaultLargeFileSize = 500 * MemorySize.MEGABYTE;
// Default trachcan folder name
private static final String TrashcanFolderName = ".Trashcan";
// Trashcan folder, used for large file deletes
private File m_trashDir;
// Large file size, require special processing for deletes/truncates
private long m_largeFileSize = DefaultLargeFileSize;
/**
* Class constructor
*
* @param args ConfigElement
* @throws DeviceContextException Error initializing the device context
*/
public JavaNIODeviceContext(ConfigElement args) throws DeviceContextException {
super();
// Initialize the database interface
initialize(args);
}
/**
* Class constructor
*
* @param name String
* @param args ConfigElement
* @throws DeviceContextException Error initializing the device context
*/
public JavaNIODeviceContext(String name, ConfigElement args) throws DeviceContextException {
super();
// Set the shared device name
setShareName(name);
// Initialize the database interface
initialize(args);
}
/**
* Initialize the Java NIO filesystem device context
*
* @param args ConfigElement
* @throws DeviceContextException Error initializing the device context
*/
protected final void initialize(ConfigElement args)
throws DeviceContextException {
// Get the device name argument
ConfigElement path = args.getChild("LocalPath");
DiskDeviceContext ctx = null;
if (path != null) {
// Validate the path and convert to an absolute path
File rootDir = new File(path.getValue());
setDeviceName( rootDir.getAbsolutePath());
// Get the large file size, for special delete/truncate processing
ConfigElement largeSize = args.getChild( "LargeFileSize");
if ( largeSize != null) {
// Parse the large file size
m_largeFileSize = MemorySize.getByteValue( largeSize.getValue());
if ( m_largeFileSize < MinimumLargeFileSize)
m_largeFileSize = MinimumLargeFileSize;
}
// Get the trashcan folder path
ConfigElement trashCanPath = args.getChild("TrashcanPath");
if ( trashCanPath != null) {
// Get the trashcan path
m_trashDir = new File( trashCanPath.getValue());
if ( m_trashDir.exists() == false)
throw new DeviceContextException("Trashcan folder does not exist - " + m_trashDir.getAbsolutePath());
else if ( m_trashDir.isFile())
throw new DeviceContextException("Trashcan path is not a folder - " + m_trashDir.getAbsolutePath());
// Make sure the trashcan folder is on the same volume as the shared folder, so we can rename deleted files
if (!isTrashcanOnSameVolume(rootDir, m_trashDir)) {
// File share and trash folders are not on the same volume
throw new DeviceContextException("File share and trash folders must be on the same volume");
}
}
else {
// Use/create a folder within the shared folder as the trashcan folder
File trashDir = new File( rootDir, TrashcanFolderName);
if ( trashDir.exists() == false) {
// Create the trashcan folder
if (trashDir.mkdir() == false)
throw new DeviceContextException("Failed to create trashcan folder - " + trashDir.getAbsolutePath());
}
// Set the trashcan path
m_trashDir = trashDir;
}
// Check if debug output is enabled
if ( args.getChild( "Debug") != null)
setDebug( true);
// Set filesystem flags
setFilesystemAttributes(FileSystem.CasePreservedNames + FileSystem.UnicodeOnDisk);
// If the path is not valid then set the filesystem as unavailable
if (rootDir.exists() == false || rootDir.isDirectory() == false || rootDir.list() == null) {
// Mark the filesystem as unavailable
setAvailable(false);
// DEBUG
if ( hasDebug())
Debug.println("Share " + getShareName() + ", local path=" + rootDir.getPath() + " unavailable");
}
}
else {
// Local path not specified
throw new DeviceContextException("LocalPath parameter not specified");
}
}
/**
* Validate that the trashcan folder resides on the same volume as the shared
* folder. This is a requirement because we want to be able to move files into
* the trashcan by simply renaming them.
* <p>
* The default implementation only uses a simple heuristic, override this method
* to provide a more advanced platform-specific check if required.
*
* @param rootDir The root directory of the shared folder
* @param trashCan The proposed trashcan directory
* @return True if the proposed trashcan directory is acceptable, false to
* signal an error
*/
protected boolean isTrashcanOnSameVolume(File rootDir, File trashCan) {
return trashCan.getAbsolutePath().startsWith(rootDir.getAbsolutePath())
|| rootDir.getParent().equalsIgnoreCase(m_trashDir.getParent());
}
/**
* Check if the trashcan folder is configured
*
* @return boolean
*/
protected final boolean hasTrashFolder() {
return m_trashDir != null ? true : false;
}
/**
* Return the trashcan folder path
*
* @return File
*/
protected final File getTrashFolder() {
return m_trashDir;
}
/**
* Return the large file size, in bytes
*
* @return long
*/
protected final long getLargeFileSize() {
return m_largeFileSize;
}
}