1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares a utility class for iterating over all of the files in an
16 : // archive and transforming them, before putting them back into a new
17 : // archive. Work is performed via callbacks that the client registers.
18 :
19 : #ifndef SYZYGY_AR_AR_TRANSFORM_H_
20 : #define SYZYGY_AR_AR_TRANSFORM_H_
21 :
22 : #include "base/callback.h"
23 : #include "base/logging.h"
24 : #include "base/files/file_path.h"
25 : #include "syzygy/ar/ar_common.h"
26 :
27 : namespace ar {
28 :
29 : // A class for transforming all of the object files contained in an
30 : // archive, and repackaging them into an archive.
31 : class ArTransform {
32 : public:
33 : // The type of callback that will be invoked for each object file
34 : // in the archive. If this returns true then the transform will
35 : // continue. If it returns false then the transform will terminate
36 : // with an error. Transforms modify the values in place.
37 : // |header| The header of the file.
38 : // |contents| The contents of the file.
39 : // |remove| If set to true then indicates that the file should be
40 : // removed from the archive.
41 : typedef base::Callback<bool(ParsedArFileHeader* /* header */,
42 : DataBuffer* /* contents */,
43 : bool* /* remove */)>
44 : TransformFileCallback;
45 :
46 : // Constructor.
47 E : ArTransform() { }
48 :
49 : // Applies the transform. The transform must already have been configured.
50 : // @returns true on success, false otherwise.
51 : bool Transform();
52 :
53 : // @name Mutators.
54 : // @{
55 : // Sets the input archive path.
56 : // @param input_archive The archive to be processed.
57 E : void set_input_archive(const base::FilePath& input_archive) {
58 E : DCHECK(!input_archive.empty());
59 E : input_archive_ = input_archive;
60 E : }
61 :
62 : // Sets the output archive path.
63 : // @param output_archive The archive to be produced.
64 E : void set_output_archive(const base::FilePath& output_archive) {
65 E : DCHECK(!output_archive.empty());
66 E : output_archive_ = output_archive;
67 E : }
68 :
69 : // Sets the callback.
70 : // @param callback The callback to be invoked.
71 E : void set_callback(TransformFileCallback callback) {
72 E : DCHECK(!callback.is_null());
73 E : callback_ = callback;
74 E : }
75 : // @}
76 :
77 : // @name Accessors.
78 : // @{
79 : // @returns the input archive path.
80 : const base::FilePath& input_archive() const { return input_archive_; }
81 :
82 : // @returns the output archive path.
83 : const base::FilePath& output_archive() const { return input_archive_; }
84 :
85 : // @returns the callback.
86 : TransformFileCallback callback() const { return callback_; }
87 : // @}
88 :
89 : private:
90 : base::FilePath input_archive_;
91 : base::FilePath output_archive_;
92 : TransformFileCallback callback_;
93 :
94 : DISALLOW_COPY_AND_ASSIGN(ArTransform);
95 : };
96 :
97 : // A callback adapter that allows transforms to modify the files
98 : // on disk rather than in memory. This is not thread safe.
99 : class OnDiskArTransformAdapter {
100 : public:
101 : typedef ArTransform::TransformFileCallback TransformFileCallback;
102 :
103 : // The type of callback that will be invoked for each object file
104 : // in the archive. If this returns true then the transform will
105 : // continue. If it returns false then the transform will terminate
106 : // with an error. Transforms work on temporary files on disk.
107 : // |input_path| The path of the original file on disk.
108 : // |output_path| The path where the transformed file should be written.
109 : // |header| The header of the file.
110 : // |remove| If set to true then indicates that the file should be
111 : // removed from the archive.
112 : typedef base::Callback<bool(const base::FilePath& /* input_path */,
113 : const base::FilePath& /* output_path */,
114 : ParsedArFileHeader* /* header */,
115 : bool* /* remove */)>
116 : TransformFileOnDiskCallback;
117 :
118 : // Constructor.
119 : // @param inner_callback The callback that will be invoked by the adapter.
120 : explicit OnDiskArTransformAdapter(
121 : TransformFileOnDiskCallback inner_callback);
122 :
123 : // Destructor.
124 : ~OnDiskArTransformAdapter();
125 :
126 : // @name Accessors.
127 : // @{
128 : TransformFileOnDiskCallback& inner_callback() {
129 : return inner_callback_;
130 : }
131 E : TransformFileCallback& outer_callback() {
132 E : return outer_callback_;
133 E : }
134 : // @}
135 :
136 : private:
137 : // The function that will be bound as the wrapped callback
138 : bool Transform(ParsedArFileHeader* header,
139 : DataBuffer* contents,
140 : bool* remove);
141 :
142 : // Wrapped and unwrapped callbacks.
143 : TransformFileOnDiskCallback inner_callback_;
144 : TransformFileCallback outer_callback_;
145 :
146 : // Temporary directory where files are produced.
147 : base::FilePath temp_dir_;
148 : size_t index_;
149 : };
150 :
151 : } // namespace ar
152 :
153 : #endif // SYZYGY_AR_AR_TRANSFORM_H_
|