1 /*
2 ** Copyright 2012-2013 Centreon
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 **
16 ** For more information : contact@centreon.com
17 */
18 
19 #include <cstdlib>
20 #include <iostream>
21 #include "com/centreon/exceptions/basic.hh"
22 #include "com/centreon/io/file_entry.hh"
23 #include "com/centreon/io/file_stream.hh"
24 
25 unsigned int const DATA_SIZE = 42;
26 
27 using namespace com::centreon;
28 
29 /**
30  *  Check size is valid.
31  *
32  *  @return EXIT_SUCCESS on success.
33  */
main()34 int main() {
35   int ret(EXIT_FAILURE);
36 
37   std::string temp(io::file_stream::temp_path());
38   try {
39     {
40       io::file_stream fs;
41       fs.open(temp, "w");
42       fs.close();
43     }
44 
45     io::file_entry entry(temp);
46     if (entry.size())
47       throw(basic_error() << "invalid file size: not empty");
48 
49     {
50       std::string data(DATA_SIZE, ' ');
51       io::file_stream fs;
52       fs.open(temp, "w");
53       fs.write(data.c_str(), data.size());
54       fs.close();
55     }
56 
57     if (entry.size())
58       throw(basic_error() << "invalid file size: not empty");
59 
60     entry.refresh();
61 
62     if (entry.size() != DATA_SIZE)
63       throw(basic_error() << "invalid file size: is empty");
64 
65     ret = EXIT_SUCCESS;
66   } catch (std::exception const& e) {
67     std::cerr << e.what() << std::endl;
68   }
69 
70   if (io::file_stream::exists(temp))
71     io::file_stream::remove(temp);
72 
73   return (ret);
74 }
75