bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
read_test_baseline.cc
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of the BES
4
5// Copyright (c) 2018 OPeNDAP, Inc.
6// Author: James Gallagher <jgallagher@opendap.org>
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21//
22// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
23
24#include "config.h"
25
26#include <fstream>
27#include <string>
28#include <vector>
29
30#include <cstdlib> // for system
31
32#include "BESInternalError.h"
33
34#include "read_test_baseline.h"
35
36using namespace std;
37
38namespace bes {
48string
49read_test_baseline(const string &fn)
50{
51 ifstream is;
52 is.open (fn.c_str(), ios::binary );
53
54 if (!is)
55 return "Could not read baseline file: "+fn;
56
57 // get length of file:
58 is.seekg (0, ios::end);
59 long length = is.tellg();
60
61 // back to start
62 is.seekg (0, ios::beg);
63
64 // allocate memory:
65 vector<char> buffer(length+1);
66
67 // read data as a block:
68 is.read (buffer.data(), length);
69 is.close();
70 buffer[length] = '\0';
71
72 return string(buffer.data());
73}
74
75void clean_cache_dir(const string &cache)
76{
77 string cache_dir = cache + "/*";
78
79 string command = string("rm ") + cache_dir + " 2>/dev/null";
80
81 int status = system(command.c_str());
82
83 // it's normal for this to 'fail' because the clean operation has already
84 // been run or because it's the first run of the tests. But, fork and waitpid
85 // should not return an error and the shell should be found.
86 if (status == -1 || status == 127)
87 throw BESInternalError("Failed to clean cache dir: " + cache_dir, __FILE__, __LINE__);
88}
89
90} // namespace bes