summaryrefslogtreecommitdiff
path: root/src/shared/fileregister.cpp
blob: 98e4945284c8333dc233cffa97a5650c94d253d7 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "fileregister.h"
#include "fileentry.h"
#include "directoryentry.h"
#include "originconnection.h"
#include "filesorigin.h"
#include <log.h>

namespace MOShared
{

using namespace MOBase;

FileRegister::FileRegister(boost::shared_ptr<OriginConnection> originConnection)
  : m_OriginConnection(originConnection), m_NextIndex(0)
{
}

bool FileRegister::indexValid(FileIndex index) const
{
  std::scoped_lock lock(m_Mutex);

  if (index < m_Files.size()) {
    return (m_Files[index].get() != nullptr);
  }

  return false;
}

FileEntryPtr FileRegister::createFile(
  std::wstring name, DirectoryEntry *parent, DirectoryStats& stats)
{
  const auto index = generateIndex();
  auto p = FileEntryPtr(new FileEntry(index, std::move(name), parent));

  {
    std::scoped_lock lock(m_Mutex);

    if (index >= m_Files.size()) {
      m_Files.resize(index + 1);
    }

    m_Files[index] = p;
  }

  return p;
}

FileIndex FileRegister::generateIndex()
{
  return m_NextIndex++;
}

FileEntryPtr FileRegister::getFile(FileIndex index) const
{
  std::scoped_lock lock(m_Mutex);

  if (index < m_Files.size()) {
    return m_Files[index];
  } else {
    return {};
  }
}

bool FileRegister::removeFile(FileIndex index)
{
  std::scoped_lock lock(m_Mutex);

  if (index < m_Files.size()) {
    FileEntryPtr p;
    m_Files[index].swap(p);

    if (p) {
      unregisterFile(p);
      return true;
    }
  }

  log::error(QObject::tr("invalid file index for remove: {}").toStdString(), index);
  return false;
}

void FileRegister::removeOrigin(FileIndex index, OriginID originID)
{
  std::unique_lock lock(m_Mutex);

  if (index < m_Files.size()) {
    FileEntryPtr& p = m_Files[index];

    if (p) {
      if (p->removeOrigin(originID)) {
        m_Files[index] = {};
        lock.unlock();
        unregisterFile(p);
        return;
      }
    }
  }

  log::error(QObject::tr("invalid file index for remove (for origin): {}").toStdString(), index);
}

void FileRegister::removeOriginMulti(
  std::set<FileIndex> indices, OriginID originID)
{
  std::vector<FileEntryPtr> removedFiles;

  {
    std::scoped_lock lock(m_Mutex);

    for (auto iter = indices.begin(); iter != indices.end(); ) {
      const auto index = *iter;

      if (index < m_Files.size()) {
        const auto& p = m_Files[index];

        if (p && p->removeOrigin(originID)) {
          removedFiles.push_back(p);
          m_Files[index] = {};
          ++iter;
          continue;
        }
      }

      iter = indices.erase(iter);
    }
  }

  // optimization: this is only called when disabling an origin and in this case
  // we don't have to remove the file from the origin

  // need to remove files from their parent directories. multiple ways to go
  // about this:
  //   a) for each file, search its parents file-list (preferably by name) and
  //      remove what is found
  //   b) gather the parent directories, go through the file list for each once
  //      and remove all files that have been removed
  //
  // the latter should be faster when there are many files in few directories.
  // since this is called only when disabling an origin that is probably
  // frequently the case

  std::set<DirectoryEntry*> parents;
  for (const FileEntryPtr &file : removedFiles) {
    if (file->getParent() != nullptr) {
      parents.insert(file->getParent());
    }
  }

  for (DirectoryEntry *parent : parents) {
    parent->removeFiles(indices);
  }
}

void FileRegister::sortOrigins()
{
  std::scoped_lock lock(m_Mutex);

  for (auto&& p : m_Files) {
    if (p) {
      p->sortOrigins();
    }
  }
}

void FileRegister::unregisterFile(FileEntryPtr file)
{
  bool ignore;

  // unregister from origin
  OriginID originID = file->getOrigin(ignore);
  m_OriginConnection->getByID(originID).removeFile(file->getIndex());
  const auto& alternatives = file->getAlternatives();

  for (const auto& alt : alternatives) {
    m_OriginConnection->getByID(alt.originID()).removeFile(file->getIndex());
  }

  // unregister from directory
  if (file->getParent() != nullptr) {
    file->getParent()->removeFile(file->getIndex());
  }
}

} // namespace