Search in sources :

Example 1 with QuotaManager

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);
    }
}
Also used : FileState(org.alfresco.jlan.server.filesys.cache.FileState) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) QuotaManager(org.alfresco.jlan.server.filesys.quota.QuotaManager)

Example 2 with QuotaManager

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;
}
Also used : FileState(org.alfresco.jlan.server.filesys.cache.FileState) QuotaManager(org.alfresco.jlan.server.filesys.quota.QuotaManager)

Example 3 with QuotaManager

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);
}
Also used : FileState(org.alfresco.jlan.server.filesys.cache.FileState) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) QuotaManager(org.alfresco.jlan.server.filesys.quota.QuotaManager)

Example 4 with QuotaManager

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;
    }
}
Also used : FileState(org.alfresco.jlan.server.filesys.cache.FileState) AccessDeniedException(org.alfresco.jlan.server.filesys.AccessDeniedException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) DiskFullException(org.alfresco.jlan.server.filesys.DiskFullException) FileSharingException(org.alfresco.jlan.server.filesys.FileSharingException) DeviceContextException(org.alfresco.jlan.server.core.DeviceContextException) FileExistsException(org.alfresco.jlan.server.filesys.FileExistsException) FileNotFoundException(java.io.FileNotFoundException) QuotaManagerException(org.alfresco.jlan.server.filesys.quota.QuotaManagerException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) AccessDeniedException(org.alfresco.jlan.server.filesys.AccessDeniedException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DirectoryNotEmptyException(org.alfresco.jlan.server.filesys.DirectoryNotEmptyException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.jlan.server.filesys.FileInfo) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) QuotaManager(org.alfresco.jlan.server.filesys.quota.QuotaManager)

Example 5 with QuotaManager

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);
                }
            }
        }
    }
}
Also used : ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) QuotaManager(org.alfresco.jlan.server.filesys.quota.QuotaManager)

Aggregations

QuotaManager (org.alfresco.jlan.server.filesys.quota.QuotaManager)7 IOException (java.io.IOException)5 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)5 FileState (org.alfresco.jlan.server.filesys.cache.FileState)4 FileNotFoundException (java.io.FileNotFoundException)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 DeviceContextException (org.alfresco.jlan.server.core.DeviceContextException)2 AccessDeniedException (org.alfresco.jlan.server.filesys.AccessDeniedException)2 DirectoryNotEmptyException (org.alfresco.jlan.server.filesys.DirectoryNotEmptyException)2 DiskFullException (org.alfresco.jlan.server.filesys.DiskFullException)2 FileInfo (org.alfresco.jlan.server.filesys.FileInfo)2 QuotaManagerException (org.alfresco.jlan.server.filesys.quota.QuotaManagerException)2 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 Callable (java.util.concurrent.Callable)1 FileExistsException (org.alfresco.jlan.server.filesys.FileExistsException)1 FileSharingException (org.alfresco.jlan.server.filesys.FileSharingException)1 IOControlNotImplementedException (org.alfresco.jlan.server.filesys.IOControlNotImplementedException)1 PermissionDeniedException (org.alfresco.jlan.server.filesys.PermissionDeniedException)1 SMBException (org.alfresco.jlan.smb.SMBException)1