From c05c3763512739d6f748f28deb62185560c62de5 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 9 Oct 2018 22:58:17 +0200 Subject: [PATCH] Expose /rsFiles/requestDirDetails via JSON API This method is useful to browse own shared directries, or directries shared by friends whith browseable permission --- libretroshare/src/file_sharing/p3filelists.cc | 10 ---- libretroshare/src/ft/ftserver.cc | 11 ++-- libretroshare/src/ft/ftserver.h | 7 ++- libretroshare/src/retroshare/rsfiles.h | 30 ++++++++-- libretroshare/src/retroshare/rsflags.h | 2 +- libretroshare/src/retroshare/rstypes.h | 58 ++++++++++++++++--- 6 files changed, 89 insertions(+), 29 deletions(-) diff --git a/libretroshare/src/file_sharing/p3filelists.cc b/libretroshare/src/file_sharing/p3filelists.cc index b569016d3..d980da78b 100644 --- a/libretroshare/src/file_sharing/p3filelists.cc +++ b/libretroshare/src/file_sharing/p3filelists.cc @@ -1083,16 +1083,6 @@ int p3FileDatabase::RequestDirDetails(void *ref, DirDetails& d, FileSearchFlags return true; } -int p3FileDatabase::RequestDirDetails(const RsPeerId &/*uid*/, const std::string &/*path*/, DirDetails &/*details*/) const -{ - NOT_IMPLEMENTED(); - return 0; -} -//int p3FileDatabase::RequestDirDetails(const std::string& path, DirDetails &details) const -//{ -// NOT_IMPLEMENTED(); -// return 0; -//} uint32_t p3FileDatabase::getType(void *ref) const { RS_STACK_MUTEX(mFLSMtx) ; diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 182964446..91e66bd69 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -698,11 +698,6 @@ bool ftServer::ExtraFileMove(std::string fname, const RsFileHash& hash, uint64_t /******************** Directory Listing ************************/ /***************************************************************/ -int ftServer::RequestDirDetails(const RsPeerId& uid, const std::string& path, DirDetails &details) -{ - return mFileDatabase->RequestDirDetails(uid, path, details); -} - bool ftServer::findChildPointer(void *ref, int row, void *& result, FileSearchFlags flags) { return mFileDatabase->findChildPointer(ref,row,result,flags) ; @@ -711,6 +706,12 @@ int ftServer::RequestDirDetails(void *ref, DirDetails &details, FileSearchFlags { return mFileDatabase->RequestDirDetails(ref,details,flags) ; } + +bool ftServer::requestDirDetails( DirDetails& details, std::uintptr_t handle, + FileSearchFlags flags ) +{ + return RequestDirDetails(reinterpret_cast(handle), details, flags); +} uint32_t ftServer::getType(void *ref, FileSearchFlags /* flags */) { return mFileDatabase->getType(ref) ; diff --git a/libretroshare/src/ft/ftserver.h b/libretroshare/src/ft/ftserver.h index c15c991fc..50b526bf3 100644 --- a/libretroshare/src/ft/ftserver.h +++ b/libretroshare/src/ft/ftserver.h @@ -189,8 +189,13 @@ public: /*** * Directory Listing / Search Interface ***/ - virtual int RequestDirDetails(const RsPeerId& uid, const std::string& path, DirDetails &details); virtual int RequestDirDetails(void *ref, DirDetails &details, FileSearchFlags flags); + + /// @see RsFiles::RequestDirDetails + virtual bool requestDirDetails( + DirDetails &details, std::uintptr_t handle = 0, + FileSearchFlags flags = RS_FILE_HINTS_LOCAL ); + virtual bool findChildPointer(void *ref, int row, void *& result, FileSearchFlags flags) ; virtual uint32_t getType(void *ref,FileSearchFlags flags) ; diff --git a/libretroshare/src/retroshare/rsfiles.h b/libretroshare/src/retroshare/rsfiles.h index fd70df5d8..9344932f8 100644 --- a/libretroshare/src/retroshare/rsfiles.h +++ b/libretroshare/src/retroshare/rsfiles.h @@ -26,6 +26,7 @@ #include #include #include +#include #include "rstypes.h" #include "serialiser/rsserializable.h" @@ -423,13 +424,32 @@ public: virtual bool ExtraFileStatus(std::string localpath, FileInfo &info) = 0; virtual bool ExtraFileMove(std::string fname, const RsFileHash& hash, uint64_t size, std::string destpath) = 0; + /** + * @brief Request directory details, subsequent multiple call may be used to + * explore a whole directory tree. + * @jsonapi{development} + * @param[out] details Storage for directory details + * @param[in] handle element handle 0 for root, pass the content of + * DirDetails::child[x].ref after first call to explore deeper, be aware + * that is not a real pointer but an index used internally by RetroShare. + * @param[in] flags file search flags RS_FILE_HINTS_* + * @return false if error occurred, true otherwise + */ + virtual bool requestDirDetails( + DirDetails &details, std::uintptr_t handle = 0, + FileSearchFlags flags = RS_FILE_HINTS_LOCAL ) = 0; + /*** + * Directory Listing / Search Interface + */ + /** + * Kept for retrocompatibility, it was originally written for easier + * interaction with Qt. As soon as you can, you should prefer to use the + * version of this methodn which take `std::uintptr_t handle` as paramether. + */ + virtual int RequestDirDetails( + void* handle, DirDetails& details, FileSearchFlags flags ) = 0; - /*** - * Directory Listing / Search Interface - */ - virtual int RequestDirDetails(const RsPeerId& uid, const std::string& path, DirDetails &details) = 0; - virtual int RequestDirDetails(void *ref, DirDetails &details, FileSearchFlags flags) = 0; virtual bool findChildPointer(void *ref, int row, void *& result, FileSearchFlags flags) =0; virtual uint32_t getType(void *ref,FileSearchFlags flags) = 0; diff --git a/libretroshare/src/retroshare/rsflags.h b/libretroshare/src/retroshare/rsflags.h index 548b1641b..f6c97de10 100644 --- a/libretroshare/src/retroshare/rsflags.h +++ b/libretroshare/src/retroshare/rsflags.h @@ -23,7 +23,7 @@ template class t_RsFlags32 { public: - inline t_RsFlags32() { _bits=0; } + inline t_RsFlags32() : _bits(0) {} inline explicit t_RsFlags32(uint32_t N) : _bits(N) {} // allows initialization from a set of uint32_t inline t_RsFlags32 operator| (const t_RsFlags32& f) const { return t_RsFlags32(_bits | f._bits) ; } diff --git a/libretroshare/src/retroshare/rstypes.h b/libretroshare/src/retroshare/rstypes.h index b1b35748c..2981d6e9e 100644 --- a/libretroshare/src/retroshare/rstypes.h +++ b/libretroshare/src/retroshare/rstypes.h @@ -257,21 +257,43 @@ struct FileInfo : RsSerializable std::ostream &operator<<(std::ostream &out, const FileInfo& info); -class DirStub +/** + * Pointers in this class have no real meaning as pointers, they are used as + * indexes, internally by retroshare. + */ +struct DirStub : RsSerializable { - public: + DirStub() : type(DIR_TYPE_UNKNOWN), ref(nullptr) {} + uint8_t type; std::string name; void *ref; + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + RS_SERIAL_PROCESS(type); + RS_SERIAL_PROCESS(name); + std::uintptr_t& handle(reinterpret_cast(ref)); + RS_SERIAL_PROCESS(handle); + } }; -class DirDetails +/** + * Pointers in this class have no real meaning as pointers, they are used as + * indexes, internally by retroshare. + */ +struct DirDetails : RsSerializable { -public: - void *parent; - int prow; /* parent row */ + DirDetails() : parent(nullptr), prow(0), ref(nullptr), + type(DIR_TYPE_UNKNOWN), count(0), mtime(0), max_mtime(0) {} - void *ref; + + void* parent; + int prow; /* parent row */ + + void* ref; uint8_t type; RsPeerId id; std::string name; @@ -284,6 +306,28 @@ public: std::vector children; std::list parent_groups; // parent groups for the shared directory + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + std::uintptr_t& parentHandle(reinterpret_cast(parent)); + RS_SERIAL_PROCESS(parentHandle); + RS_SERIAL_PROCESS(prow); + std::uintptr_t& handle(reinterpret_cast(ref)); + RS_SERIAL_PROCESS(handle); + RS_SERIAL_PROCESS(type); + RS_SERIAL_PROCESS(id); + RS_SERIAL_PROCESS(name); + RS_SERIAL_PROCESS(hash); + RS_SERIAL_PROCESS(path); + RS_SERIAL_PROCESS(count); + RS_SERIAL_PROCESS(mtime); + RS_SERIAL_PROCESS(flags); + RS_SERIAL_PROCESS(max_mtime); + RS_SERIAL_PROCESS(children); + RS_SERIAL_PROCESS(parent_groups); + } }; std::ostream &operator<<(std::ostream &out, const DirDetails& details);