use of org.alfresco.jlan.server.filesys.quota.QuotaManager in project alfresco-repository by Alfresco.
the class ContentDiskDriver2 method truncateFile.
/**
* Truncate a file to the specified size
*
* @param sess Server session
* @param tree Tree connection
* @param file Network file details
* @param size New file length
* @exception java.io.IOException The exception description.
*/
public void truncateFile(SrvSession sess, TreeConnection tree, NetworkFile file, long size) throws IOException {
// Keep track of the allocation/release size in case the file resize fails
ContentContext ctx = (ContentContext) tree.getContext();
if (logger.isDebugEnabled()) {
logger.debug("truncateFile file:" + file + ", size: " + size);
}
long allocSize = 0L;
long releaseSize = 0L;
// Check if there is a quota manager
QuotaManager quotaMgr = ctx.getQuotaManager();
if (ctx.hasQuotaManager()) {
if (file instanceof ContentNetworkFile) {
ContentNetworkFile contentFile = (ContentNetworkFile) file;
if (contentFile.hasContent() == false) {
contentFile.openContent(false, false);
}
} else if (file instanceof TempNetworkFile) {
} else {
throw new IOException("Invalid file class type, " + file.getClass().getName());
}
if (size > file.getFileSize()) {
// Calculate the space to be allocated
allocSize = size - file.getFileSize();
// Allocate space to extend the file
quotaMgr.allocateSpace(sess, tree, file, allocSize);
} else {
// Calculate the space to be released as the file is to be truncated, release the space if
// the file truncation is successful
releaseSize = file.getFileSize() - size;
}
}
if (file instanceof ContentNetworkFile) {
// Get the cached state for the file
ContentNetworkFile contentFile = (ContentNetworkFile) file;
FileState fstate = contentFile.getFileState();
if (fstate != null && size > fstate.getAllocationSize()) {
fstate.setAllocationSize(size);
}
}
if (file instanceof TempNetworkFile) {
TempNetworkFile contentFile = (TempNetworkFile) file;
FileState fstate = contentFile.getFileState();
if (fstate != null && size > fstate.getAllocationSize()) {
fstate.setAllocationSize(size);
}
}
try {
file.truncateFile(size);
} catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("unable to truncate the file + :" + file.getFullName(), ex);
}
if (allocSize > 0 && quotaMgr != null) {
quotaMgr.releaseSpace(sess, tree, file.getFileId(), null, allocSize);
}
// Rethrow the exception
throw ex;
}
if (releaseSize > 0 && quotaMgr != null) {
quotaMgr.releaseSpace(sess, tree, file.getFileId(), null, releaseSize);
}
if (logger.isDebugEnabled()) {
logger.debug("Truncated file: network file=" + file + " size=" + size);
}
}
use of org.alfresco.jlan.server.filesys.quota.QuotaManager in project alfresco-repository by Alfresco.
the class ContentDiskDriver2 method writeFile.
/**
* Write a block of data to the file.
*
* @param sess Server session
* @param tree Tree connection
* @param file Network file details
* @param buffer byte[] Data to be written
* @param bufferOffset Offset within the buffer that the data starts
* @param size int Data length
* @param fileOffset Position within the file that the data is to be written.
* @return Number of bytes actually written
* @exception java.io.IOException The exception description.
*/
public int writeFile(SrvSession sess, TreeConnection tree, NetworkFile file, byte[] buffer, int bufferOffset, int size, long fileOffset) throws IOException {
if (writeLogger.isDebugEnabled()) {
writeLogger.debug("write File:" + file + " size:" + size);
}
// Check if there is a quota manager
ContentContext ctx = (ContentContext) tree.getContext();
QuotaManager quotaMgr = ctx.getQuotaManager();
long curSize = file.getFileSize();
if (quotaMgr != null) {
// Check if the file requires extending
long extendSize = 0L;
long endOfWrite = fileOffset + size;
if (endOfWrite > curSize) {
// Calculate the amount the file must be extended
extendSize = endOfWrite - file.getFileSize();
// Allocate space for the file extend
if (writeLogger.isDebugEnabled()) {
writeLogger.debug("writeFile: allocate more space fileName:" + file.getName() + ", extendTo:" + extendSize);
}
long alloc = quotaMgr.allocateSpace(sess, tree, file, extendSize);
if (file instanceof TempNetworkFile) {
TempNetworkFile tnf = (TempNetworkFile) file;
FileState fstate = tnf.getFileState();
if (fstate != null) {
fstate.setAllocationSize(alloc);
}
}
}
}
// Write to the file
file.writeFile(buffer, size, bufferOffset, fileOffset);
if (quotaMgr != null) {
if (file.getFileSize() < curSize) {
// Release space that was freed by the write
quotaMgr.releaseSpace(sess, tree, file.getFileId(), file.getFullName(), curSize - file.getFileSize());
}
}
if (writeLogger.isDebugEnabled()) {
writeLogger.debug("Wrote bytes to file: network file=" + file + " buffer size=" + buffer.length + " size=" + size + " file offset=" + fileOffset);
}
return size;
}
use of org.alfresco.jlan.server.filesys.quota.QuotaManager in project alfresco-repository by Alfresco.
the class ContentDiskDriver method truncateFile.
/**
* Truncate a file to the specified size
*
* @param sess Server session
* @param tree Tree connection
* @param file Network file details
* @param size New file length
* @exception java.io.IOException The exception description.
*/
public void truncateFile(SrvSession sess, TreeConnection tree, NetworkFile file, long size) throws IOException {
// Keep track of the allocation/release size in case the file resize fails
ContentContext ctx = (ContentContext) tree.getContext();
long allocSize = 0L;
long releaseSize = 0L;
// Check if there is a quota manager
QuotaManager quotaMgr = ctx.getQuotaManager();
if (ctx.hasQuotaManager()) {
if (file instanceof ContentNetworkFile) {
ContentNetworkFile contentFile = (ContentNetworkFile) file;
if (contentFile.hasContent() == false)
contentFile.openContent(false, false);
} else
throw new IOException("Invalid file class type, " + file.getClass().getName());
if (size > file.getFileSize()) {
// Calculate the space to be allocated
allocSize = size - file.getFileSize();
// Allocate space to extend the file
quotaMgr.allocateSpace(sess, tree, file, allocSize);
} else {
// Calculate the space to be released as the file is to be truncated, release the space if
// the file truncation is successful
releaseSize = file.getFileSize() - size;
}
}
if (file instanceof ContentNetworkFile) {
// Get the cached state for the file
ContentNetworkFile contentFile = (ContentNetworkFile) file;
FileState fstate = contentFile.getFileState();
if (fstate != null && size > fstate.getAllocationSize())
fstate.setAllocationSize(size);
}
try {
file.truncateFile(size);
} catch (IOException ex) {
if (allocSize > 0 && quotaMgr != null)
quotaMgr.releaseSpace(sess, tree, file.getFileId(), null, allocSize);
throw ex;
}
if (releaseSize > 0 && quotaMgr != null)
quotaMgr.releaseSpace(sess, tree, file.getFileId(), null, releaseSize);
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILEIO))
logger.debug("Truncated file: network file=" + file + " size=" + size);
}
use of org.alfresco.jlan.server.filesys.quota.QuotaManager in project alfresco-repository by Alfresco.
the class ContentDiskDriver method deleteFile.
/**
* Delete the specified file.
*
* @param sess Server session
* @param tree Tree connection
* @param name NetworkFile
* @exception java.io.IOException The exception description.
*/
public void deleteFile(final SrvSession sess, final TreeConnection tree, final String name) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Delete file - " + name);
}
final ContentContext ctx = (ContentContext) tree.getContext();
try {
// Check if there is a quota manager enabled, if so then we need to save the current file size
final QuotaManager quotaMgr = ctx.getQuotaManager();
// Perform repository updates in a retryable write transaction
Callable<Void> postTxn = doInWriteTransaction(sess, new CallableIO<Callable<Void>>() {
public Callable<Void> call() throws IOException {
// Get the node and delete it
final NodeRef nodeRef = getNodeForPath(tree, name);
Callable<Void> result = null;
if (fileFolderService.exists(nodeRef)) {
// Get the size of the file being deleted
final FileInfo fInfo = quotaMgr == null ? null : getFileInformation(sess, tree, name);
// Check if the node is versionable
final boolean isVersionable = nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE);
if (logger.isDebugEnabled()) {
logger.debug("deleted file" + name);
}
fileFolderService.delete(nodeRef);
// Return the operations to perform when the transaction succeeds
result = new Callable<Void>() {
public Void call() throws Exception {
if (ctx.hasStateCache()) {
if (isVersionable == true) {
// Make sure the file state is cached for a short while, a new file may be
// renamed to the same name
// in which case we can connect the file to the previous version history
FileState delState = ctx.getStateCache().findFileState(name, true);
if (logger.isDebugEnabled()) {
logger.debug("set delete on close" + name);
}
delState.setExpiryTime(System.currentTimeMillis() + FileState.DeleteTimeout);
delState.setFileStatus(DeleteOnClose);
delState.setFilesystemObject(nodeRef);
} else {
// Remove the file state
ctx.getStateCache().removeFileState(name);
}
// Update, or create, a parent folder file state
String[] paths = FileName.splitPath(name);
if (paths[0] != null && paths[0].length() > 1) {
// Get the file state for the parent folder
FileState parentState = getStateForPath(tree, paths[0]);
if (parentState == null && ctx.hasStateCache())
parentState = ctx.getStateCache().findFileState(paths[0], true);
// Update the modification timestamp
parentState.updateModifyDateTime();
}
}
if (quotaMgr != null)
quotaMgr.releaseSpace(sess, tree, fInfo.getFileId(), name, fInfo.getSize());
return null;
}
};
}
if (logger.isDebugEnabled() && (ctx.hasDebug(AlfrescoContext.DBG_FILE) || ctx.hasDebug(AlfrescoContext.DBG_RENAME)))
logger.debug("Deleted file: " + name + ", node=" + nodeRef);
return result;
}
});
// Perform state updates after the transaction succeeds
postTxn.call();
} catch (NodeLockedException ex) {
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Delete file - access denied (locked)");
throw new AccessDeniedException("Delete " + name);
} catch (org.alfresco.repo.security.permissions.AccessDeniedException ex) {
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Delete file - access denied");
throw new AccessDeniedException("Delete " + name);
} catch (IOException ex) {
// Allow I/O Exceptions to pass through
throw ex;
} catch (Exception ex) {
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Delete file error", ex);
// Convert to a general I/O exception
IOException ioe = new IOException("Delete file " + name);
ioe.initCause(ex);
throw ioe;
}
}
use of org.alfresco.jlan.server.filesys.quota.QuotaManager in project alfresco-repository by Alfresco.
the class ContentDiskDriver2 method reduceQuota.
/**
* @param session
* @param tree
* @param file
*/
public void reduceQuota(SrvSession session, TreeConnection tree, NetworkFile file) {
if (file.hasDeleteOnClose()) {
final ContentContext ctx = (ContentContext) tree.getContext();
if (logger.isDebugEnabled()) {
logger.debug("closeFile has delete on close set");
}
if (file instanceof TempNetworkFile) {
TempNetworkFile tnf = (TempNetworkFile) file;
final QuotaManager quotaMgr = ctx.getQuotaManager();
if (quotaMgr != null) {
try {
quotaMgr.releaseSpace(session, tree, file.getFileId(), file.getName(), tnf.getFileSizeInt());
} catch (IOException e) {
logger.error(e);
}
}
}
}
}
Aggregations