aboutsummaryrefslogtreecommitdiff
path: root/core/fxcrt/fx_folder_posix.cpp
blob: b4cf9f9ec681b6ce405ef657b2ca113020a4a98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2021 The PDFium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "core/fxcrt/fx_folder.h"

#include <memory>

#include "build/build_config.h"
#include "core/fxcrt/unowned_ptr.h"
#include "third_party/base/ptr_util.h"

#if BUILDFLAG(IS_WIN)
#error "built on wrong platform"
#endif

#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

class FX_PosixFolder : public FX_Folder {
 public:
  ~FX_PosixFolder() override;

  bool GetNextFile(ByteString* filename, bool* bFolder) override;

 private:
  friend class FX_Folder;
  FX_PosixFolder(const ByteString& path, DIR* dir);

  const ByteString m_Path;
  UnownedPtr<DIR> m_Dir;
};

std::unique_ptr<FX_Folder> FX_Folder::OpenFolder(const ByteString& path) {
  DIR* dir = opendir(path.c_str());
  if (!dir)
    return nullptr;

  // Private ctor.
  return pdfium::WrapUnique(new FX_PosixFolder(path, dir));
}

FX_PosixFolder::FX_PosixFolder(const ByteString& path, DIR* dir)
    : m_Path(path), m_Dir(dir) {}

FX_PosixFolder::~FX_PosixFolder() {
  closedir(m_Dir.ExtractAsDangling());
}

bool FX_PosixFolder::GetNextFile(ByteString* filename, bool* bFolder) {
  struct dirent* de = readdir(m_Dir);
  if (!de)
    return false;

  ByteString fullpath = m_Path + "/" + de->d_name;
  struct stat deStat;
  if (stat(fullpath.c_str(), &deStat) < 0)
    return false;

  *filename = de->d_name;
  *bFolder = S_ISDIR(deStat.st_mode);
  return true;
}