Search in sources :

Example 21 with FileInfo

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

the class ContentDiskDriver method startSearch.

/**
 * Start a new search on the filesystem using the specified searchPath that may contain
 * wildcards.
 *
 * @param sess Server session
 * @param tree Tree connection
 * @param searchPath File(s) to search for, may include wildcards.
 * @param attributes Attributes of the file(s) to search for, see class SMBFileAttribute.
 * @return SearchContext
 * @exception java.io.FileNotFoundException If the search could not be started.
 */
public SearchContext startSearch(SrvSession sess, TreeConnection tree, String searchPath, int attributes) throws FileNotFoundException {
    // Access the device context
    if (logger.isDebugEnabled()) {
        logger.debug("startSearch: " + searchPath);
    }
    ContentContext ctx = (ContentContext) tree.getContext();
    try {
        String searchFileSpec = searchPath;
        NodeRef searchRootNodeRef = ctx.getRootNode();
        FileState searchFolderState = null;
        // Create the transaction
        beginReadTransaction(sess);
        // If the state table is available see if we can speed up the search using either cached
        // file information or find the folder node to be searched without having to walk the path
        String[] paths = FileName.splitPath(searchPath);
        if (ctx.hasStateCache()) {
            if (paths[0] != null && paths[0].length() >= 1) {
                // Find the node ref for the folder being searched
                NodeRef nodeRef = getNodeForPath(tree, paths[0]);
                // Get the file state for the folder being searched
                searchFolderState = getStateForPath(tree, paths[0]);
                if (searchFolderState == null) {
                    // Create a file state for the folder
                    searchFolderState = ctx.getStateCache().findFileState(paths[0], true);
                }
                if (searchFolderState.hasFilesystemObject() == false) {
                    // Set the associated node for the folder
                    searchFolderState.setFilesystemObject(nodeRef);
                }
                if (nodeRef != null) {
                    searchRootNodeRef = nodeRef;
                    searchFileSpec = paths[1];
                    if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_SEARCH))
                        logger.debug("Search using cached noderef for path " + searchPath);
                }
            }
        }
        if (searchFileSpec.equals("*.*"))
            searchFileSpec = "*";
        // Debug
        long startTime = 0L;
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_SEARCH))
            startTime = System.currentTimeMillis();
        // Perform the search
        List<NodeRef> results = cifsHelper.getNodeRefs(searchRootNodeRef, searchFileSpec);
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_SEARCH)) {
            long endTime = System.currentTimeMillis();
            if ((endTime - startTime) > 500)
                logger.debug("Search for searchPath=" + searchPath + ", searchSpec=" + searchFileSpec + ", searchRootNode=" + searchRootNodeRef + " took " + (endTime - startTime) + "ms results=" + results.size());
        }
        // Build the search context to store the results, use the cache lookup search for wildcard searches
        SearchContext searchCtx = null;
        if (searchFileSpec.equals("*")) {
            // Use a cache lookup search context
            CacheLookupSearchContext cacheContext = new CacheLookupSearchContext(cifsHelper, results, searchFileSpec, paths[0], ctx.getStateCache(), isLockedFilesAsOffline);
            searchCtx = cacheContext;
            if (searchFolderState != null && searchFolderState.hasFilesystemObject()) {
                // Get the '.' pseudo entry file details
                FileInfo finfo = cifsHelper.getFileInformation((NodeRef) searchFolderState.getFilesystemObject(), isReadOnly, isLockedFilesAsOffline);
                if (searchFolderState != null) {
                    if (searchFolderState.hasAccessDateTime())
                        finfo.setAccessDateTime(searchFolderState.getAccessDateTime());
                    if (searchFolderState.hasChangeDateTime())
                        finfo.setChangeDateTime(searchFolderState.getChangeDateTime());
                    if (searchFolderState.hasModifyDateTime())
                        finfo.setModifyDateTime(searchFolderState.getModifyDateTime());
                }
                // Set the '.' pseudo entry details
                cacheContext.setDotInfo(finfo);
                if (searchFolderState.getPath().equals(FileName.DOS_SEPERATOR_STR)) {
                    // Searching the root folder, re-use the search folder file information for the '..' pseudo entry
                    FileInfo dotDotInfo = new FileInfo();
                    dotDotInfo.copyFrom(finfo);
                    cacheContext.setDotDotInfo(dotDotInfo);
                } else {
                    // Get the parent folder path
                    String parentPath = searchFolderState.getPath();
                    if (parentPath.endsWith(FileName.DOS_SEPERATOR_STR) && parentPath.length() > 1)
                        parentPath = parentPath.substring(0, parentPath.length() - 1);
                    int pos = parentPath.lastIndexOf(FileName.DOS_SEPERATOR_STR);
                    if (pos != -1)
                        parentPath = parentPath.substring(0, pos + 1);
                    // Get the file state for the parent path, if available
                    FileState parentState = ctx.getStateCache().findFileState(parentPath);
                    NodeRef parentNode = null;
                    if (parentState != null)
                        parentNode = (NodeRef) parentState.getFilesystemObject();
                    if (parentState == null || parentNode == null)
                        parentNode = getNodeForPath(tree, parentPath);
                    // Get the file information for the parent folder
                    finfo = cifsHelper.getFileInformation(parentNode, isReadOnly, isLockedFilesAsOffline);
                    if (parentState != null) {
                        if (parentState.hasAccessDateTime())
                            finfo.setAccessDateTime(parentState.getAccessDateTime());
                        if (parentState.hasChangeDateTime())
                            finfo.setChangeDateTime(parentState.getChangeDateTime());
                        if (parentState.hasModifyDateTime())
                            finfo.setModifyDateTime(parentState.getModifyDateTime());
                    }
                    // Set the '..' pseudo entry details
                    cacheContext.setDotDotInfo(finfo);
                }
            }
        } else {
            if (ctx.hasStateCache())
                searchCtx = new CacheLookupSearchContext(cifsHelper, results, searchFileSpec, paths[0], ctx.getStateCache(), isLockedFilesAsOffline);
            else
                searchCtx = new ContentSearchContext(cifsHelper, results, searchFileSpec, paths[0], isLockedFilesAsOffline);
        }
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_SEARCH))
            logger.debug("Started search: search path=" + searchPath + " attributes=" + attributes + ", ctx=" + searchCtx);
        return searchCtx;
    } catch (org.alfresco.repo.security.permissions.AccessDeniedException ex) {
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_SEARCH))
            logger.debug("Start search - access denied, " + searchPath);
        throw new FileNotFoundException("Start search " + searchPath);
    } catch (RuntimeException ex) {
        if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_SEARCH))
            logger.debug("Start search", ex);
        throw new FileNotFoundException("Start search " + searchPath);
    }
}
Also used : FileState(org.alfresco.jlan.server.filesys.cache.FileState) FileNotFoundException(java.io.FileNotFoundException) SearchContext(org.alfresco.jlan.server.filesys.SearchContext) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) FileInfo(org.alfresco.jlan.server.filesys.FileInfo)

Example 22 with FileInfo

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

the class ContentDiskDriverTest method testCreateFile.

/**
 * Test Create File
 */
public void testCreateFile() throws Exception {
    logger.debug("testCreatedFile");
    ServerConfiguration scfg = new ServerConfiguration("testServer");
    TestServer testServer = new TestServer("testServer", scfg);
    final SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName");
    DiskSharedDevice share = getDiskSharedDevice();
    final TreeConnection testConnection = testServer.getTreeConnection(share);
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    class TestContext {

        NodeRef testNodeRef;
    }
    ;
    final TestContext testContext = new TestContext();
    /**
     * Step 1 : Create a new file in read/write mode and add some content.
     */
    int openAction = FileAction.CreateNotExist;
    final String FILE_NAME = "testCreateFileX.new";
    final String FILE_PATH = "\\" + FILE_NAME;
    FileOpenParams params = new FileOpenParams(FILE_PATH, openAction, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
    final NetworkFile file = driver.createFile(testSession, testConnection, params);
    assertNotNull("file is null", file);
    assertFalse("file is read only, should be read-write", file.isReadOnly());
    assertFalse("file is not closed ", file.isClosed());
    RetryingTransactionCallback<Void> writeStuffCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            byte[] stuff = "Hello World".getBytes();
            driver.writeFile(testSession, testConnection, file, stuff, 0, stuff.length, 0);
            driver.closeFile(testSession, testConnection, file);
            return null;
        }
    };
    tran.doInTransaction(writeStuffCB);
    RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef companyHome = repositoryHelper.getCompanyHome();
            NodeRef newNode = nodeService.getChildByName(companyHome, ContentModel.ASSOC_CONTAINS, FILE_NAME);
            testContext.testNodeRef = newNode;
            assertNotNull("can't find new node", newNode);
            Serializable content = nodeService.getProperty(newNode, ContentModel.PROP_CONTENT);
            assertNotNull("content is null", content);
            return null;
        }
    };
    tran.doInTransaction(validateCB);
    // now validate that the new node is in the correct location and has the correct name
    FileInfo info = driver.getFileInformation(testSession, testConnection, FILE_PATH);
    assertNotNull("info is null", info);
    NodeRef n2 = getNodeForPath(testConnection, FILE_PATH);
    assertEquals("get Node For Path returned different node", testContext.testNodeRef, n2);
    /**
     * Step 2 : Negative Test Attempt to create the same file again
     */
    try {
        driver.createFile(testSession, testConnection, params);
        fail("File exists not detected");
    } catch (FileExistsException fe) {
    // expect to go here
    }
    // Clean up so we could run the test again
    driver.deleteFile(testSession, testConnection, FILE_PATH);
    /**
     * Step 3 : create a file in a new directory in read only mode
     */
    String FILE2_PATH = TEST_ROOT_DOS_PATH + FILE_PATH;
    FileOpenParams dirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, openAction, AccessMode.ReadOnly, FileAttribute.NTDirectory, 0);
    driver.createDirectory(testSession, testConnection, dirParams);
    FileOpenParams file2Params = new FileOpenParams(FILE2_PATH, openAction, AccessMode.ReadOnly, FileAttribute.NTNormal, 0);
    NetworkFile file2 = driver.createFile(testSession, testConnection, file2Params);
    // clean up so we could run the test again
    driver.deleteFile(testSession, testConnection, FILE2_PATH);
}
Also used : Serializable(java.io.Serializable) SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) FileInfo(org.alfresco.jlan.server.filesys.FileInfo) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice) FileExistsException(org.alfresco.jlan.server.filesys.FileExistsException)

Aggregations

FileInfo (org.alfresco.jlan.server.filesys.FileInfo)22 NodeRef (org.alfresco.service.cmr.repository.NodeRef)17 FileNotFoundException (java.io.FileNotFoundException)13 IOException (java.io.IOException)13 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)9 AccessDeniedException (org.alfresco.jlan.server.filesys.AccessDeniedException)8 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)8 SrvSession (org.alfresco.jlan.server.SrvSession)7 ServerConfiguration (org.alfresco.jlan.server.config.ServerConfiguration)7 DiskSharedDevice (org.alfresco.jlan.server.filesys.DiskSharedDevice)7 NetworkFile (org.alfresco.jlan.server.filesys.NetworkFile)7 TreeConnection (org.alfresco.jlan.server.filesys.TreeConnection)7 FileState (org.alfresco.jlan.server.filesys.cache.FileState)6 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)6 FileOpenParams (org.alfresco.jlan.server.filesys.FileOpenParams)5 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)5 Serializable (java.io.Serializable)4 DeviceContextException (org.alfresco.jlan.server.core.DeviceContextException)4 FileExistsException (org.alfresco.jlan.server.filesys.FileExistsException)4 Date (java.util.Date)3