2 * bareboxenv.c - generate or read a barebox environment archive
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
39 void *xmalloc(size_t size
)
43 if (!(p
= malloc(size
))) {
44 printf("ERROR: out of memory\n");
51 void *xzalloc(size_t size
)
53 void *p
= xmalloc(size
);
58 /* Find out if the last character of a string matches the one given.
59 * Don't underrun the buffer if the string length is 0.
61 char* last_char_is(const char *s
, int c
)
64 size_t sz
= strlen(s
) - 1;
66 if ( (unsigned char)*s
== c
)
73 ACTION_RECURSE
= (1 << 0),
74 /* ACTION_FOLLOWLINKS = (1 << 1), - unused */
75 ACTION_DEPTHFIRST
= (1 << 2),
76 /*ACTION_REVERSE = (1 << 3), - unused */
79 int recursive_action(const char *fileName
, unsigned flags
,
80 int (*fileAction
) (const char *fileName
, struct stat
* statbuf
, void* userData
, int depth
),
81 int (*dirAction
) (const char *fileName
, struct stat
* statbuf
, void* userData
, int depth
),
82 void* userData
, const unsigned depth
);
84 #define DOT_OR_DOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2])))
86 /* concatenate path and file name to new allocation buffer,
87 * not adding '/' if path name already has '/'
89 char *concat_path_file(const char *path
, const char *filename
)
95 lc
= last_char_is(path
, '/');
96 while (*filename
== '/')
99 str
= xmalloc(strlen(path
) + (lc
==0 ? 1 : 0) + strlen(filename
) + 1);
100 sprintf(str
, "%s%s%s", path
, (lc
==NULL
? "/" : ""), filename
);
106 * This function make special for recursive actions with usage
107 * concat_path_file(path, filename)
108 * and skipping "." and ".." directory entries
111 char *concat_subpath_file(const char *path
, const char *f
)
113 if (f
&& DOT_OR_DOTDOT(f
))
115 return concat_path_file(path
, f
);
118 #include "../lib/recursive_action.c"
119 #include "../include/envfs.h"
120 #include "../lib/crc32.c"
121 #include "../lib/make_directory.c"
122 #include "../include/environment.h"
123 #include "../common/environment.c"
125 void usage(char *prgname
)
127 printf( "Usage : %s [OPTION] DIRECTORY FILE\n"
128 "Load an barebox environment sector into a directory or\n"
129 "save a directory into an barebox environment sector\n"
132 " -s save (directory -> environment sector)\n"
133 " -l load (environment sector -> directory)\n"
134 " -p <size> pad output file to given size\n",
138 int main(int argc
, char *argv
[])
141 int save
= 0, load
= 0, pad
= 0, fd
;
142 char *filename
= NULL
, *dirname
= NULL
;
144 while((opt
= getopt(argc
, argv
, "slp:")) != -1) {
153 pad
= strtoul(optarg
, NULL
, 0);
158 if (optind
+ 1 >= argc
) {
163 dirname
= argv
[optind
];
164 filename
= argv
[optind
+ 1];
166 if ((!load
&& !save
) || (load
&& save
) || !filename
|| !dirname
) {
172 fd
= open(filename
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0644);
181 if (truncate(filename
, pad
)) {
188 printf("loading env from file %s to %s\n", filename
, dirname
);
189 envfs_load(filename
, dirname
);
192 printf("saving contents of %s to file %s\n", dirname
, filename
);
193 envfs_save(filename
, dirname
);