File: vtkEmscriptenTestUtilities.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (93 lines) | stat: -rw-r--r-- 3,293 bytes parent folder | download
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#ifdef __EMSCRIPTEN__

#include "vtkEmscriptenTestUtilities.h"

#include <cstdlib>                // for free
#include <emscripten.h>           // for EM_ASM macro
#include <iostream>               // for cerr, cout
#include <vtksys/SystemTools.hxx> // for filesystem utilities

VTK_ABI_NAMESPACE_BEGIN

/**
 * Given a path to a file "a/b/c/d.ext" on the server hosting webassembly runtime/page,
 * this method preloads it into the sandbox at the given `sandboxedFilePath`
 */
void vtkEmscriptenTestUtilities::PreloadDataFile(
  const std::string& hostFilePath, const std::string& sandboxedFilePath)
{
  FILE* f = nullptr;
  // This function preloads the file and it's size in bytes into wasm memory from javascript.
  // It returns a pointer to a PreloadData.
  auto* payload =
    reinterpret_cast<PreloadDescriptor*>(vtkPreloadDataFileIntoMemory(hostFilePath.c_str()));
  if (payload != nullptr)
  {
    const auto dirName = vtksys::SystemTools::GetFilenamePath(sandboxedFilePath);
    if (!dirName.empty() && dirName != "/")
    {
      const auto status = vtksys::SystemTools::MakeDirectory(dirName);
      if (!status.IsSuccess())
      {
        std::cerr << "Failed to create parent directory for " << sandboxedFilePath << '\n';
        free(payload->buffer);
        free(payload);
        return;
      }
      else
      {
        std::cout << "Created directory for " << sandboxedFilePath << '\n';
      }
    }
    f = fopen(sandboxedFilePath.c_str(), "wb+");
    if (f != nullptr)
    {
      auto written = fwrite(payload->buffer, sizeof(payload->buffer[0]), payload->size, f);
      fclose(f);
      if (written != payload->size)
      {
        std::cerr << "Failed to preload " << payload->size << " bytes from " << hostFilePath
                  << " into " << sandboxedFilePath << "\n";
      }
      else
      {
        std::cout << "Preloaded " << payload->size << " bytes from " << hostFilePath << " into "
                  << sandboxedFilePath << "\n";
      }
    }
    else
    {
      std::cerr << "Failed to create sandboxed " << sandboxedFilePath << " for " << hostFilePath
                << '\n';
    }
    free(payload->buffer);
    free(payload);
  }
}

/**
 * Given a path to a file "a/b/c/d.ext" on the server hosting webassembly runtime/page,
 * this method preloads it into the sandbox at "/d.ext" and returns "/d.ext"
 */
std::string vtkEmscriptenTestUtilities::PreloadDataFile(const std::string& hostFilePath)
{
  std::string sandboxedFilePath = vtksys::SystemTools::GetFilenameName(hostFilePath);
  vtkEmscriptenTestUtilities::PreloadDataFile(hostFilePath, sandboxedFilePath);
  return sandboxedFilePath;
}

/**
 * This method helps to write files outside of the webassembly sandbox.
 * Writes `n` number of bytes from `data` into a file called `hostFilePath` on the server's file
 * system. The server is a system which hosts the webassembly runtime/page.
 */
void vtkEmscriptenTestUtilities::DumpFile(
  const std::string& hostFilePath, const void* data, size_t n)
{
  const auto* bytes = reinterpret_cast<const uint8_t*>(data);
  vtkDumpFile(hostFilePath.c_str(), bytes, n);
}
VTK_ABI_NAMESPACE_END
#endif // __EMSCRIPTEN__