1 // Copyright 2021 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include "core/fxcrt/fx_folder.h" 8 9 #include <memory> 10 11 #include "build/build_config.h" 12 #include "core/fxcrt/unowned_ptr.h" 13 #include "third_party/base/memory/ptr_util.h" 14 15 #if BUILDFLAG(IS_WIN) 16 #error "built on wrong platform" 17 #endif 18 19 #include <dirent.h> 20 #include <sys/stat.h> 21 #include <unistd.h> 22 23 class FX_PosixFolder : public FX_Folder { 24 public: 25 ~FX_PosixFolder() override; 26 27 bool GetNextFile(ByteString* filename, bool* bFolder) override; 28 29 private: 30 friend class FX_Folder; 31 FX_PosixFolder(const ByteString& path, DIR* dir); 32 33 const ByteString m_Path; 34 UnownedPtr<DIR> m_Dir; 35 }; 36 OpenFolder(const ByteString & path)37std::unique_ptr<FX_Folder> FX_Folder::OpenFolder(const ByteString& path) { 38 DIR* dir = opendir(path.c_str()); 39 if (!dir) 40 return nullptr; 41 42 // Private ctor. 43 return pdfium::WrapUnique(new FX_PosixFolder(path, dir)); 44 } 45 FX_PosixFolder(const ByteString & path,DIR * dir)46FX_PosixFolder::FX_PosixFolder(const ByteString& path, DIR* dir) 47 : m_Path(path), m_Dir(dir) {} 48 ~FX_PosixFolder()49FX_PosixFolder::~FX_PosixFolder() { 50 closedir(m_Dir.ExtractAsDangling()); 51 } 52 GetNextFile(ByteString * filename,bool * bFolder)53bool FX_PosixFolder::GetNextFile(ByteString* filename, bool* bFolder) { 54 struct dirent* de = readdir(m_Dir); 55 if (!de) 56 return false; 57 58 ByteString fullpath = m_Path + "/" + de->d_name; 59 struct stat deStat; 60 if (stat(fullpath.c_str(), &deStat) < 0) 61 return false; 62 63 *filename = de->d_name; 64 *bFolder = S_ISDIR(deStat.st_mode); 65 return true; 66 } 67