Search in sources :

Example 1 with FileState

use of org.alfresco.jlan.server.filesys.cache.FileState 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 FileState

use of org.alfresco.jlan.server.filesys.cache.FileState 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 FileState

use of org.alfresco.jlan.server.filesys.cache.FileState in project alfresco-repository by Alfresco.

the class ContentDiskDriver method getStateForPath.

/**
 * Get the file state for the specified path
 *
 * @param tree TreeConnection
 * @param path String
 * @return FileState
 * @exception FileNotFoundException
 */
public FileState getStateForPath(TreeConnection tree, String path) throws FileNotFoundException {
    // Check if there is a cached state for the path
    ContentContext ctx = (ContentContext) tree.getContext();
    FileState fstate = null;
    if (ctx.hasStateCache()) {
        // Get the file state for a file/folder
        fstate = ctx.getStateCache().findFileState(path);
    }
    return fstate;
}
Also used : FileState(org.alfresco.jlan.server.filesys.cache.FileState)

Example 4 with FileState

use of org.alfresco.jlan.server.filesys.cache.FileState in project alfresco-repository by Alfresco.

the class ContentDiskDriver method createDirectory.

/**
 * Create a new directory on this file system.
 *
 * <p>
 * WARNING : side effect - closes current transaction context.
 *
 * @param sess Server session
 * @param tree Tree connection.
 * @param params Directory create parameters
 * @exception java.io.IOException If an error occurs.
 */
public void createDirectory(SrvSession sess, final TreeConnection tree, final FileOpenParams params) throws IOException {
    final ContentContext ctx = (ContentContext) tree.getContext();
    try {
        // Access the repository in a retryable write transaction
        Pair<String, NodeRef> result = doInWriteTransaction(sess, new CallableIO<Pair<String, NodeRef>>() {

            public Pair<String, NodeRef> call() throws IOException {
                // get the device root
                NodeRef deviceRootNodeRef = ctx.getRootNode();
                String path = params.getPath();
                String parentPath = null;
                if (ctx.hasStateCache()) {
                    // See if the parent folder has a file state, we can avoid having to walk the path
                    String[] paths = FileName.splitPath(path);
                    if (paths[0] != null && paths[0].length() > 1) {
                        // Find the node ref for the folder being searched
                        NodeRef nodeRef = getNodeForPath(tree, paths[0]);
                        if (nodeRef != null) {
                            deviceRootNodeRef = nodeRef;
                            path = paths[1];
                            if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
                                logger.debug("Create file using cached noderef for path " + paths[0]);
                        }
                        parentPath = paths[0];
                    }
                }
                // Create it - the path will be created, if necessary
                NodeRef nodeRef = cifsHelper.createNode(deviceRootNodeRef, path, ContentModel.TYPE_FOLDER);
                return new Pair<String, NodeRef>(parentPath, nodeRef);
            }
        });
        // Get or create the file state for the parent folder
        FileState parentState = null;
        String parentPath = result.getFirst();
        if (parentPath != null) {
            parentState = getStateForPath(tree, parentPath);
            if (parentState == null && ctx.hasStateCache())
                parentState = ctx.getStateCache().findFileState(parentPath, true);
        }
        if (ctx.hasStateCache()) {
            FileState fstate = ctx.getStateCache().findFileState(params.getPath(), true);
            if (fstate != null) {
                // Indicate that the file is open
                fstate.setFileStatus(DirectoryExists);
                fstate.setFilesystemObject(result.getSecond());
                if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
                    logger.debug("Create folder, state=" + fstate);
            }
            if (parentState != null)
                parentState.updateModifyDateTime();
        }
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
            logger.debug("Created directory: path=" + params.getPath() + " file open params=" + params + " node=" + result.getSecond());
    } catch (org.alfresco.repo.security.permissions.AccessDeniedException ex) {
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
            logger.debug("Create directory - access denied, " + params.getFullPath());
        throw new AccessDeniedException("Create directory " + params.getFullPath());
    } catch (RuntimeException ex) {
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
            logger.debug("Create directory error", ex);
        throw new IOException("Create directory " + params.getFullPath(), ex);
    }
}
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) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Pair(org.alfresco.util.Pair)

Example 5 with FileState

use of org.alfresco.jlan.server.filesys.cache.FileState 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)

Aggregations

FileState (org.alfresco.jlan.server.filesys.cache.FileState)28 IOException (java.io.IOException)14 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)12 NodeRef (org.alfresco.service.cmr.repository.NodeRef)12 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)10 AccessDeniedException (org.alfresco.jlan.server.filesys.AccessDeniedException)9 FileNotFoundException (java.io.FileNotFoundException)8 FileStateCache (org.alfresco.jlan.server.filesys.cache.FileStateCache)8 FileInfo (org.alfresco.jlan.server.filesys.FileInfo)6 Date (java.util.Date)5 DirectoryNotEmptyException (org.alfresco.jlan.server.filesys.DirectoryNotEmptyException)4 QuotaManager (org.alfresco.jlan.server.filesys.quota.QuotaManager)4 Pair (org.alfresco.util.Pair)4 NetworkFileLegacyReferenceCount (org.alfresco.filesys.alfresco.NetworkFileLegacyReferenceCount)3 DiskFullException (org.alfresco.jlan.server.filesys.DiskFullException)3 FileAccessToken (org.alfresco.jlan.server.filesys.FileAccessToken)3 FileExistsException (org.alfresco.jlan.server.filesys.FileExistsException)3 FileSharingException (org.alfresco.jlan.server.filesys.FileSharingException)3 NetworkFile (org.alfresco.jlan.server.filesys.NetworkFile)3 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)3