Imported Upstream version 1.6.0
[platform/upstream/augeas.git] / src / transform.h
1 /*
2  * Copyright (C) 2009-2015 David Lutterkort
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
17  *
18  * Author: David Lutterkort <lutter@redhat.com>
19  */
20
21 #ifndef TRANSFORM_H_
22 #define TRANSFORM_H_
23
24 /*
25  * Transformers for going from file globs to path names in the tree
26  * functions are in transform.c
27  */
28
29 /* Filters for globbing files */
30 struct filter {
31     unsigned int   ref;
32     struct filter *next;
33     struct string *glob;
34     unsigned int   include : 1;
35 };
36
37 struct filter *make_filter(struct string *glb, unsigned int include);
38 void free_filter(struct filter *filter);
39
40 /* Transformers that actually run lenses on contents of files */
41 struct transform {
42     unsigned int      ref;
43     struct lens      *lens;
44     struct filter    *filter;
45 };
46
47 struct transform *make_transform(struct lens *lens, struct filter *filter);
48 void free_transform(struct transform *xform);
49
50 /*
51  * When we pass a tree for a transform, the tree must have exactly one
52  * child with label "lens" whose value is the qualified name of the lens to
53  * use, and any number of children labelled "incl" or "excl" whose values
54  * are glob patterns used to filter which files to transform.
55  */
56
57 /* Verify that the tree XFM represents a valid transform. If it does not,
58  * add an 'error' child to it.
59  *
60  * Return 0 if XFM is a valid transform, -1 otherwise.
61  */
62 int transform_validate(struct augeas *aug, struct tree *xfm);
63
64 /* Load all files matching the TRANSFORM's filter into the tree in AUG by
65  * applying the TRANSFORM's lens to their contents and putting the
66  * resulting tree under "/files" + filename. Also stores some information
67  * about filename underneath "/augeas/files" + filename
68  * If a FILE is passed, only this FILE will be loaded.
69  */
70 int transform_load(struct augeas *aug, struct tree *xfm, const char *file);
71
72 /* Return 1 if TRANSFORM applies to PATH, 0 otherwise.
73  * PATH must not include "/files/".
74  */
75 int filter_matches(struct tree *xfm, const char *path);
76
77 /* Return 1 if TRANSFORM applies to PATH, 0 otherwise. The TRANSFORM
78  * applies to PATH if (1) PATH starts with "/files/" and (2) the rest of
79  * PATH matches the transform's filter
80 */
81 int transform_applies(struct tree *xfm, const char *path);
82
83 /* Save TREE into the file corresponding to PATH. It is assumed that the
84  * TRANSFORM applies to that PATH
85  */
86 int transform_save(struct augeas *aug, struct tree *xfm,
87                    const char *path, struct tree *tree);
88
89 /* Transform TEXT into a tree and store it at PATH
90  */
91 int text_store(struct augeas *aug, const char *lens_name,
92                const char *path, const char *text);
93
94 /* Transform the tree at PATH back into TEXT_OUT, assuming TEXT_IN was
95  * used to initially generate the tree
96  */
97 int text_retrieve(struct augeas *aug, const char *lens_name,
98                   const char *path, struct tree *tree,
99                   const char *text_in, char **text_out);
100
101 /* Remove the file for TREE, either by moving it to a .augsave file or by
102  * unlinking it, depending on aug->flags. TREE must be the node underneath
103  * /augeas/files corresponding to the file to be removed.
104  *
105  * Return 0 on success, -1 on failure
106  */
107 int remove_file(struct augeas *aug, struct tree *tree);
108
109 /* Return a printable name for the transform XFM. Never returns NULL. */
110 const char *xfm_lens_name(struct tree *xfm);
111
112 /* Store a file-specific transformation error in /augeas/files/PATH/error */
113 ATTRIBUTE_FORMAT(printf, 4, 5)
114 void transform_file_error(struct augeas *aug, const char *status,
115                           const char *filename, const char *format, ...);
116 #endif
117
118
119 /*
120  * Local variables:
121  *  indent-tabs-mode: nil
122  *  c-indent-level: 4
123  *  c-basic-offset: 4
124  *  tab-width: 4
125  * End:
126  */