~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/utils/FileUpdate/FileUpdate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- FileUpdate.cpp - Conditionally update a file -----------------------===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
//
 
8
//===----------------------------------------------------------------------===//
 
9
//
 
10
// FileUpdate is a utility for conditionally updating a file from its input
 
11
// based on whether the input differs from the output. It is used to avoid
 
12
// unnecessary modifications in a build system.
 
13
//
 
14
//===----------------------------------------------------------------------===//
 
15
 
 
16
#include "llvm/Support/CommandLine.h"
 
17
#include "llvm/Support/MemoryBuffer.h"
 
18
#include "llvm/Support/PrettyStackTrace.h"
 
19
#include "llvm/Support/raw_ostream.h"
 
20
#include "llvm/System/Signals.h"
 
21
using namespace llvm;
 
22
 
 
23
static cl::opt<bool>
 
24
Quiet("quiet", cl::desc("Don't print unnecessary status information"),
 
25
      cl::init(false));
 
26
 
 
27
static cl::opt<std::string>
 
28
InputFilename("input-file", cl::desc("Input file (defaults to stdin)"),
 
29
              cl::init("-"), cl::value_desc("filename"));
 
30
 
 
31
static cl::opt<std::string>
 
32
OutputFilename(cl::Positional, cl::desc("<output-file>"), cl::Required);
 
33
 
 
34
int main(int argc, char **argv) {
 
35
  sys::PrintStackTraceOnErrorSignal();
 
36
  PrettyStackTraceProgram X(argc, argv);
 
37
  cl::ParseCommandLineOptions(argc, argv);
 
38
 
 
39
  // Get the input data.
 
40
  std::string ErrorStr;
 
41
  MemoryBuffer *In =
 
42
    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
 
43
  if (In == 0) {
 
44
    errs() << argv[0] << ": error: Unable to get input '"
 
45
           << InputFilename << "': " << ErrorStr << '\n';
 
46
    return 1;
 
47
  }
 
48
 
 
49
  // Get the output data.
 
50
  MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), &ErrorStr);
 
51
 
 
52
  // If the output exists and the contents match, we are done.
 
53
  if (Out && In->getBufferSize() == Out->getBufferSize() &&
 
54
      memcmp(In->getBufferStart(), Out->getBufferStart(),
 
55
             Out->getBufferSize()) == 0) {
 
56
    if (!Quiet)
 
57
      outs() << argv[0] << ": Not updating '" << OutputFilename
 
58
             << "', contents match input.\n";
 
59
    return 0;
 
60
  }
 
61
 
 
62
  delete Out;
 
63
 
 
64
  // Otherwise, overwrite the output.
 
65
  if (!Quiet)
 
66
    outs() << argv[0] << ": Updating '" << OutputFilename
 
67
           << "', contents changed.\n";
 
68
  raw_fd_ostream OutStream(OutputFilename.c_str(), ErrorStr,
 
69
                           raw_fd_ostream::F_Binary);
 
70
  if (!ErrorStr.empty()) {
 
71
    errs() << argv[0] << ": Unable to write output '"
 
72
           << OutputFilename << "': " << ErrorStr << '\n';
 
73
    return 1;
 
74
  }
 
75
 
 
76
  OutStream.write(In->getBufferStart(), In->getBufferSize());
 
77
  OutStream.close();
 
78
 
 
79
  if (OutStream.has_error()) {
 
80
    errs() << argv[0] << ": Could not open output file '"
 
81
           << OutputFilename << "': " << ErrorStr << '\n';
 
82
    return 1;
 
83
  }
 
84
 
 
85
  return 0;
 
86
}