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

PredatorCZ / PreCore / 460

pending completion
460

push

github-actions-ci

PredatorCZ
try fix coverage

3204 of 6095 relevant lines covered (52.57%)

354.19 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
    Part of PreCore project
3

4
    Copyright 2021 Lukas Cone
5

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

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

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

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

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

34
static std::string localPath;
35

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

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

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

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

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

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

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

97
  return retPath + buffer;
×
98
}
99

100
void CleanCurrentTempStorage() {
×
101
  if (!localPath.empty()) {
×
102
    RemmoveAll(localPath);
103
  }
104
}
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