From 1ea818c222b3cdd841349cb27cbcf1b4ba18a808 Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Mon, 7 Feb 2022 16:52:46 +0800 Subject: [PATCH] DocumentsProvider: unify metadata row building this also fix a case where full path are taken as display names --- .../safcephfs/CephFSDocumentsProvider.java | 84 ++++++++----------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/safcephfs/CephFSDocumentsProvider.java b/src/main/java/org/safcephfs/CephFSDocumentsProvider.java index 143e798..ce5f4bb 100644 --- a/src/main/java/org/safcephfs/CephFSDocumentsProvider.java +++ b/src/main/java/org/safcephfs/CephFSDocumentsProvider.java @@ -266,58 +266,10 @@ public class CephFSDocumentsProvider extends DocumentsProvider { } } - public Cursor queryChildDocuments(String parentDocumentId, - String[] projection, String sortOrder) + private void lstatBuildDocumentRow(String filename, String displayName, + String documentId, MatrixCursor result) throws FileNotFoundException { - MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOC_PROJECTION); - Log.v(APP_NAME, "queryChildDocuments " + parentDocumentId); - String filename = parentDocumentId.substring(parentDocumentId.indexOf("/") + 1); - String[] res = doCephOperation(() -> { - try { - return cm.listdir(filename); - } catch (FileNotFoundException e) { - Log.e(APP_NAME, "queryChildDocuments " + parentDocumentId + " not found"); - throw new FileNotFoundException(parentDocumentId + " not found"); - } - }); CephStat cs = new CephStat(); - for (String entry : res) { - Log.v(APP_NAME, "lstat " + parentDocumentId + " " + entry); - doCephOperation(() -> { - try { - cm.lstat(filename + "/" + entry, cs); - return null; - } catch (FileNotFoundException|CephNotDirectoryException e) { - Log.e(APP_NAME, "lstat: " + filename + "/" + entry + " not found"); - throw new FileNotFoundException(parentDocumentId + "/" + entry + " not found"); - } - }); - MatrixCursor.RowBuilder row = result.newRow(); - row.add(Document.COLUMN_DOCUMENT_ID, parentDocumentId + "/" + entry); - row.add(Document.COLUMN_DISPLAY_NAME, entry); - if (cs.isDir()) { - row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR); - if (!checkPermissions || (getPerm(cs) & PERM_WRITEABLE) == PERM_WRITEABLE) { - row.add(Document.COLUMN_FLAGS, Document.FLAG_DIR_SUPPORTS_CREATE); - } - } else if (cs.isFile() || cs.isSymlink()) { - row.add(Document.COLUMN_MIME_TYPE, getMime(entry)); - if (!checkPermissions || (getPerm(cs) & PERM_WRITEABLE) == PERM_WRITEABLE) { - row.add(Document.COLUMN_FLAGS, Document.FLAG_SUPPORTS_WRITE); - } - } - row.add(Document.COLUMN_SIZE, cs.size); - row.add(Document.COLUMN_LAST_MODIFIED, cs.m_time); - } - return result; - } - - public Cursor queryDocument(String documentId, String[] projection) - throws FileNotFoundException { - MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOC_PROJECTION); - String filename = documentId.substring(documentId.indexOf("/") + 1); - CephStat cs = new CephStat(); - Log.v(APP_NAME, "queryDocument " + documentId); doCephOperation(() -> { try { cm.lstat(filename, cs); @@ -329,7 +281,7 @@ public class CephFSDocumentsProvider extends DocumentsProvider { }); MatrixCursor.RowBuilder row = result.newRow(); row.add(Document.COLUMN_DOCUMENT_ID, documentId); - row.add(Document.COLUMN_DISPLAY_NAME, filename); + row.add(Document.COLUMN_DISPLAY_NAME, displayName); if (cs.isDir()) { row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR); if (!checkPermissions || (getPerm(cs) & PERM_WRITEABLE) == PERM_WRITEABLE) { @@ -343,6 +295,36 @@ public class CephFSDocumentsProvider extends DocumentsProvider { } row.add(Document.COLUMN_SIZE, cs.size); row.add(Document.COLUMN_LAST_MODIFIED, cs.m_time); + } + + public Cursor queryChildDocuments(String parentDocumentId, + String[] projection, String sortOrder) + throws FileNotFoundException { + MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOC_PROJECTION); + Log.v(APP_NAME, "queryChildDocuments " + parentDocumentId); + String filename = parentDocumentId.substring(parentDocumentId.indexOf("/") + 1); + String[] res = doCephOperation(() -> { + try { + return cm.listdir(filename); + } catch (FileNotFoundException e) { + Log.e(APP_NAME, "queryChildDocuments " + parentDocumentId + " not found"); + throw new FileNotFoundException(parentDocumentId + " not found"); + } + }); + for (String entry : res) { + lstatBuildDocumentRow(filename + "/" + entry, entry, + parentDocumentId + "/" + entry, result); + } + return result; + } + + public Cursor queryDocument(String documentId, String[] projection) + throws FileNotFoundException { + Log.v(APP_NAME, "queryDocument " + documentId); + MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOC_PROJECTION); + lstatBuildDocumentRow(documentId.substring(documentId.indexOf("/") + 1), + documentId.substring(documentId.lastIndexOf("/") + 1), + documentId, result); return result; } -- 2.43.0