• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PredatorCZ / PreCore / 461

pending completion
461

push

github-actions-ci

PredatorCZ
update readme

3204 of 6096 relevant lines covered (52.56%)

354.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/app/tmp_storage.cpp
1
/*  Spike is universal dedicated module handler
2

3
    Copyright 2021-2023 Lukas Cone
4

5
    Licensed under the Apache License, Version 2.0 (the "License");
6
    you may not use this file except in compliance with the License.
7
    You may obtain a copy of the License at
8

9
        http://www.apache.org/licenses/LICENSE-2.0
10

11
    Unless required by applicable law or agreed to in writing, software
12
    distributed under the License is distributed on an "AS IS" BASIS,
13
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
    See the License for the specific language governing permissions and
15
    limitations under the License.
16
*/
17

18
#include "spike/app/tmp_storage.hpp"
19
#include "spike/io/directory_scanner.hpp"
20
#include "spike/io/fileinfo.hpp"
21
#include "spike/io/stat.hpp"
22
#include "spike/master_printer.hpp"
23
#include <chrono>
24
#include <cinttypes>
25
#include <sstream>
26

27
#ifndef _MSC_VER
28
#include <ftw.h>
29
#else
30
#include <filesystem>
31
#endif
32

33
static std::string localPath;
34

35
void InitTempStorage() {
×
36
  auto sample = es::GetTempFilename();
37
  AFileInfo sampleParts(sample);
×
38
  auto point = std::chrono::system_clock::now() + std::chrono::hours(2);
×
39
  std::stringstream str;
×
40
  str << sampleParts.GetFolder() << "spike/";
×
41
  es::mkdir(str.str());
×
42
  str << std::hex << std::chrono::system_clock::to_time_t(point) << '/';
×
43
  localPath = str.str();
×
44
  es::mkdir(localPath);
45
}
46

47
static void RemmoveAll(const std::string &path) {
48
#ifndef _MSC_VER
49
  nftw(
×
50
      path.data(),
51
      [](const char *pathName, const struct stat *, int, struct FTW *) -> int {
×
52
        if (remove(pathName) < 0) {
×
53
          printerror("[Spike] Cannot remove: " << pathName);
×
54
          return -1;
×
55
        }
56
        return 0;
57
      },
58
      10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
59
#else
60
  std::filesystem::remove_all(std::filesystem::u8path(path));
61
#endif
62
}
63

64
void CleanTempStorages() {
×
65
  auto sample = es::GetTempFilename();
66
  AFileInfo sampleParts(sample);
×
67
  std::string workDir(std::string(sampleParts.GetFolder()) + "spike/");
×
68
  DirectoryScanner sc;
×
69
  sc.ScanFolders(workDir);
×
70
  auto point = std::chrono::system_clock::now();
×
71

72
  for (auto &s : sc) {
×
73
    std::string_view sw(s);
74
    sw.remove_prefix(workDir.size());
75
    time_t time = std::strtoull(sw.data(), nullptr, 16);
×
76
    auto expiryTimePoint = std::chrono::system_clock::from_time_t(time);
77

78
    if (point >= expiryTimePoint) {
×
79
      RemmoveAll(s);
80
    }
81
  }
82
}
83

84
std::string RequestTempFile() {
×
85
  if (localPath.empty()) {
×
86
    throw std::runtime_error(
×
87
        "InitTempStorage() not called before RequestTempFile()!");
×
88
  }
89

90
  auto point = std::chrono::steady_clock::now();
×
91
  std::string retPath = localPath;
92
  char buffer[0x20];
93
  snprintf(buffer, sizeof(buffer), "%" PRIX64,
94
           point.time_since_epoch().count());
95

96
  return retPath + buffer;
×
97
}
98

99
void CleanCurrentTempStorage() {
×
100
  if (!localPath.empty()) {
×
101
    RemmoveAll(localPath);
102
  }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc