btrfs-progs: check/original: Don't overwrite return value when we failed to repair
[btrfs-progs-unstable/devel.git] / cmds-restore.c
blobd12c1a924059227273bd7f72607e65c522ddf17a
1 /*
2 * Copyright (C) 2011 Red Hat. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include "kerncompat.h"
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <lzo/lzoconf.h>
30 #include <lzo/lzo1x.h>
31 #include <zlib.h>
32 #if BTRFSRESTORE_ZSTD
33 #include <zstd.h>
34 #endif
35 #include <regex.h>
36 #include <getopt.h>
37 #include <sys/types.h>
38 #include <sys/xattr.h>
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "print-tree.h"
43 #include "transaction.h"
44 #include "list.h"
45 #include "volumes.h"
46 #include "utils.h"
47 #include "commands.h"
48 #include "help.h"
50 static char fs_name[PATH_MAX];
51 static char path_name[PATH_MAX];
52 static char symlink_target[PATH_MAX];
53 static int get_snaps = 0;
54 static int verbose = 0;
55 static int restore_metadata = 0;
56 static int restore_symlinks = 0;
57 static int ignore_errors = 0;
58 static int overwrite = 0;
59 static int get_xattrs = 0;
60 static int dry_run = 0;
62 #define LZO_LEN 4
63 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
65 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
66 u64 decompress_len)
68 z_stream strm;
69 int ret;
71 memset(&strm, 0, sizeof(strm));
72 ret = inflateInit(&strm);
73 if (ret != Z_OK) {
74 error("zlib init returned %d", ret);
75 return -1;
78 strm.avail_in = compress_len;
79 strm.next_in = (unsigned char *)inbuf;
80 strm.avail_out = decompress_len;
81 strm.next_out = (unsigned char *)outbuf;
82 ret = inflate(&strm, Z_NO_FLUSH);
83 if (ret != Z_STREAM_END) {
84 (void)inflateEnd(&strm);
85 error("zlib inflate failed: %d", ret);
86 return -1;
89 (void)inflateEnd(&strm);
90 return 0;
92 static inline size_t read_compress_length(unsigned char *buf)
94 __le32 dlen;
95 memcpy(&dlen, buf, LZO_LEN);
96 return le32_to_cpu(dlen);
99 static int decompress_lzo(struct btrfs_root *root, unsigned char *inbuf,
100 char *outbuf, u64 compress_len, u64 *decompress_len)
102 size_t new_len;
103 size_t in_len;
104 size_t out_len = 0;
105 size_t tot_len;
106 size_t tot_in;
107 int ret;
109 ret = lzo_init();
110 if (ret != LZO_E_OK) {
111 error("lzo init returned %d", ret);
112 return -1;
115 tot_len = read_compress_length(inbuf);
116 inbuf += LZO_LEN;
117 tot_in = LZO_LEN;
119 while (tot_in < tot_len) {
120 size_t mod_page;
121 size_t rem_page;
122 in_len = read_compress_length(inbuf);
124 if ((tot_in + LZO_LEN + in_len) > tot_len) {
125 error("bad compress length %lu",
126 (unsigned long)in_len);
127 return -1;
130 inbuf += LZO_LEN;
131 tot_in += LZO_LEN;
132 new_len = lzo1x_worst_compress(root->fs_info->sectorsize);
133 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
134 (unsigned char *)outbuf,
135 (void *)&new_len, NULL);
136 if (ret != LZO_E_OK) {
137 error("lzo decompress failed: %d", ret);
138 return -1;
140 out_len += new_len;
141 outbuf += new_len;
142 inbuf += in_len;
143 tot_in += in_len;
146 * If the 4 byte header does not fit to the rest of the page we
147 * have to move to the next one, unless we read some garbage
149 mod_page = tot_in % root->fs_info->sectorsize;
150 rem_page = root->fs_info->sectorsize - mod_page;
151 if (rem_page < LZO_LEN) {
152 inbuf += rem_page;
153 tot_in += rem_page;
157 *decompress_len = out_len;
159 return 0;
162 static int decompress_zstd(const char *inbuf, char *outbuf, u64 compress_len,
163 u64 decompress_len)
165 #if !BTRFSRESTORE_ZSTD
166 error("btrfs not compiled with zstd support");
167 return -1;
168 #else
169 ZSTD_DStream *strm;
170 size_t zret;
171 int ret = 0;
172 ZSTD_inBuffer in = {inbuf, compress_len, 0};
173 ZSTD_outBuffer out = {outbuf, decompress_len, 0};
175 strm = ZSTD_createDStream();
176 if (!strm) {
177 error("zstd create failed");
178 return -1;
181 zret = ZSTD_initDStream(strm);
182 if (ZSTD_isError(zret)) {
183 error("zstd init failed: %s", ZSTD_getErrorName(zret));
184 ret = -1;
185 goto out;
188 zret = ZSTD_decompressStream(strm, &out, &in);
189 if (ZSTD_isError(zret)) {
190 error("zstd decompress failed %s\n", ZSTD_getErrorName(zret));
191 ret = -1;
192 goto out;
194 if (zret != 0) {
195 error("zstd frame incomplete");
196 ret = -1;
197 goto out;
200 out:
201 ZSTD_freeDStream(strm);
202 return ret;
203 #endif
206 static int decompress(struct btrfs_root *root, char *inbuf, char *outbuf,
207 u64 compress_len, u64 *decompress_len, int compress)
209 switch (compress) {
210 case BTRFS_COMPRESS_ZLIB:
211 return decompress_zlib(inbuf, outbuf, compress_len,
212 *decompress_len);
213 case BTRFS_COMPRESS_LZO:
214 return decompress_lzo(root, (unsigned char *)inbuf, outbuf,
215 compress_len, decompress_len);
216 case BTRFS_COMPRESS_ZSTD:
217 return decompress_zstd(inbuf, outbuf, compress_len,
218 *decompress_len);
219 default:
220 break;
223 error("invalid compression type: %d", compress);
224 return -1;
227 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
229 int slot;
230 int level = 1;
231 int offset = 1;
232 struct extent_buffer *c;
233 struct extent_buffer *next = NULL;
234 struct btrfs_fs_info *fs_info = root->fs_info;
236 again:
237 for (; level < BTRFS_MAX_LEVEL; level++) {
238 if (path->nodes[level])
239 break;
242 if (level >= BTRFS_MAX_LEVEL)
243 return 1;
245 slot = path->slots[level] + 1;
247 while(level < BTRFS_MAX_LEVEL) {
248 if (!path->nodes[level])
249 return 1;
251 slot = path->slots[level] + offset;
252 c = path->nodes[level];
253 if (slot >= btrfs_header_nritems(c)) {
254 level++;
255 if (level == BTRFS_MAX_LEVEL)
256 return 1;
257 offset = 1;
258 continue;
261 if (path->reada)
262 reada_for_search(root, path, level, slot, 0);
264 next = read_node_slot(fs_info, c, slot);
265 if (extent_buffer_uptodate(next))
266 break;
267 offset++;
269 path->slots[level] = slot;
270 while(1) {
271 level--;
272 c = path->nodes[level];
273 free_extent_buffer(c);
274 path->nodes[level] = next;
275 path->slots[level] = 0;
276 if (!level)
277 break;
278 if (path->reada)
279 reada_for_search(root, path, level, 0, 0);
280 next = read_node_slot(fs_info, next, 0);
281 if (!extent_buffer_uptodate(next))
282 goto again;
284 return 0;
287 static int copy_one_inline(struct btrfs_root *root, int fd,
288 struct btrfs_path *path, u64 pos)
290 struct extent_buffer *leaf = path->nodes[0];
291 struct btrfs_file_extent_item *fi;
292 char buf[4096];
293 char *outbuf;
294 u64 ram_size;
295 ssize_t done;
296 unsigned long ptr;
297 int ret;
298 int len;
299 int inline_item_len;
300 int compress;
302 fi = btrfs_item_ptr(leaf, path->slots[0],
303 struct btrfs_file_extent_item);
304 ptr = btrfs_file_extent_inline_start(fi);
305 len = btrfs_file_extent_ram_bytes(leaf, fi);
306 inline_item_len = btrfs_file_extent_inline_item_len(leaf, btrfs_item_nr(path->slots[0]));
307 read_extent_buffer(leaf, buf, ptr, inline_item_len);
309 compress = btrfs_file_extent_compression(leaf, fi);
310 if (compress == BTRFS_COMPRESS_NONE) {
311 done = pwrite(fd, buf, len, pos);
312 if (done < len) {
313 fprintf(stderr, "Short inline write, wanted %d, did "
314 "%zd: %d\n", len, done, errno);
315 return -1;
317 return 0;
320 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
321 outbuf = calloc(1, ram_size);
322 if (!outbuf) {
323 error("not enough memory");
324 return -ENOMEM;
327 ret = decompress(root, buf, outbuf, inline_item_len, &ram_size,
328 compress);
329 if (ret) {
330 free(outbuf);
331 return ret;
334 done = pwrite(fd, outbuf, ram_size, pos);
335 free(outbuf);
336 if (done < ram_size) {
337 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
338 "did %zd: %d\n", ram_size, done, errno);
339 return -1;
342 return 0;
345 static int copy_one_extent(struct btrfs_root *root, int fd,
346 struct extent_buffer *leaf,
347 struct btrfs_file_extent_item *fi, u64 pos)
349 struct btrfs_multi_bio *multi = NULL;
350 struct btrfs_device *device;
351 char *inbuf, *outbuf = NULL;
352 ssize_t done, total = 0;
353 u64 bytenr;
354 u64 ram_size;
355 u64 disk_size;
356 u64 num_bytes;
357 u64 length;
358 u64 size_left;
359 u64 dev_bytenr;
360 u64 offset;
361 u64 count = 0;
362 int compress;
363 int ret;
364 int dev_fd;
365 int mirror_num = 1;
366 int num_copies;
368 compress = btrfs_file_extent_compression(leaf, fi);
369 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
370 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
371 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
372 offset = btrfs_file_extent_offset(leaf, fi);
373 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
374 size_left = disk_size;
375 if (compress == BTRFS_COMPRESS_NONE)
376 bytenr += offset;
378 if (verbose && offset)
379 printf("offset is %Lu\n", offset);
380 /* we found a hole */
381 if (disk_size == 0)
382 return 0;
384 inbuf = malloc(size_left);
385 if (!inbuf) {
386 error("not enough memory");
387 return -ENOMEM;
390 if (compress != BTRFS_COMPRESS_NONE) {
391 outbuf = calloc(1, ram_size);
392 if (!outbuf) {
393 error("not enough memory");
394 free(inbuf);
395 return -ENOMEM;
398 again:
399 length = size_left;
400 ret = btrfs_map_block(root->fs_info, READ, bytenr, &length, &multi,
401 mirror_num, NULL);
402 if (ret) {
403 error("cannot map block logical %llu length %llu: %d",
404 (unsigned long long)bytenr,
405 (unsigned long long)length, ret);
406 goto out;
408 device = multi->stripes[0].dev;
409 dev_fd = device->fd;
410 device->total_ios++;
411 dev_bytenr = multi->stripes[0].physical;
412 free(multi);
414 if (size_left < length)
415 length = size_left;
417 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
418 /* Need both checks, or we miss negative values due to u64 conversion */
419 if (done < 0 || done < length) {
420 num_copies = btrfs_num_copies(root->fs_info, bytenr, length);
421 mirror_num++;
422 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
423 if (mirror_num > num_copies) {
424 ret = -1;
425 error("exhausted mirrors trying to read (%d > %d)",
426 mirror_num, num_copies);
427 goto out;
429 fprintf(stderr, "Trying another mirror\n");
430 goto again;
433 mirror_num = 1;
434 size_left -= length;
435 count += length;
436 bytenr += length;
437 if (size_left)
438 goto again;
440 if (compress == BTRFS_COMPRESS_NONE) {
441 while (total < num_bytes) {
442 done = pwrite(fd, inbuf+total, num_bytes-total,
443 pos+total);
444 if (done < 0) {
445 ret = -1;
446 error("cannot write data: %d %m", errno);
447 goto out;
449 total += done;
451 ret = 0;
452 goto out;
455 ret = decompress(root, inbuf, outbuf, disk_size, &ram_size, compress);
456 if (ret) {
457 num_copies = btrfs_num_copies(root->fs_info, bytenr, length);
458 mirror_num++;
459 if (mirror_num >= num_copies) {
460 ret = -1;
461 goto out;
463 fprintf(stderr, "Trying another mirror\n");
464 goto again;
467 while (total < num_bytes) {
468 done = pwrite(fd, outbuf + offset + total,
469 num_bytes - total,
470 pos + total);
471 if (done < 0) {
472 ret = -1;
473 goto out;
475 total += done;
477 out:
478 free(inbuf);
479 free(outbuf);
480 return ret;
483 enum loop_response {
484 LOOP_STOP,
485 LOOP_CONTINUE,
486 LOOP_DONTASK
489 static enum loop_response ask_to_continue(const char *file)
491 char buf[2];
492 char *ret;
494 printf("We seem to be looping a lot on %s, do you want to keep going "
495 "on ? (y/N/a): ", file);
496 again:
497 ret = fgets(buf, 2, stdin);
498 if (*ret == '\n' || tolower(*ret) == 'n')
499 return LOOP_STOP;
500 if (tolower(*ret) == 'a')
501 return LOOP_DONTASK;
502 if (tolower(*ret) != 'y') {
503 printf("Please enter one of 'y', 'n', or 'a': ");
504 goto again;
507 return LOOP_CONTINUE;
511 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
512 int fd, const char *file_name)
514 struct btrfs_key key;
515 struct btrfs_path path;
516 struct extent_buffer *leaf;
517 struct btrfs_dir_item *di;
518 u32 name_len = 0;
519 u32 data_len = 0;
520 u32 len = 0;
521 u32 cur, total_len;
522 char *name = NULL;
523 char *data = NULL;
524 int ret = 0;
526 btrfs_init_path(&path);
527 key.objectid = inode;
528 key.type = BTRFS_XATTR_ITEM_KEY;
529 key.offset = 0;
530 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
531 if (ret < 0)
532 goto out;
534 leaf = path.nodes[0];
535 while (1) {
536 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
537 do {
538 ret = next_leaf(root, &path);
539 if (ret < 0) {
540 error("searching for extended attributes: %d",
541 ret);
542 goto out;
543 } else if (ret) {
544 /* No more leaves to search */
545 ret = 0;
546 goto out;
548 leaf = path.nodes[0];
549 } while (!leaf);
550 continue;
553 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
554 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
555 break;
556 cur = 0;
557 total_len = btrfs_item_size_nr(leaf, path.slots[0]);
558 di = btrfs_item_ptr(leaf, path.slots[0],
559 struct btrfs_dir_item);
561 while (cur < total_len) {
562 len = btrfs_dir_name_len(leaf, di);
563 if (len > name_len) {
564 free(name);
565 name = (char *) malloc(len + 1);
566 if (!name) {
567 ret = -ENOMEM;
568 goto out;
571 read_extent_buffer(leaf, name,
572 (unsigned long)(di + 1), len);
573 name[len] = '\0';
574 name_len = len;
576 len = btrfs_dir_data_len(leaf, di);
577 if (len > data_len) {
578 free(data);
579 data = (char *) malloc(len);
580 if (!data) {
581 ret = -ENOMEM;
582 goto out;
585 read_extent_buffer(leaf, data,
586 (unsigned long)(di + 1) + name_len,
587 len);
588 data_len = len;
590 if (fsetxattr(fd, name, data, data_len, 0))
591 error("setting extended attribute %s on file %s: %m",
592 name, file_name);
594 len = sizeof(*di) + name_len + data_len;
595 cur += len;
596 di = (struct btrfs_dir_item *)((char *)di + len);
598 path.slots[0]++;
600 ret = 0;
601 out:
602 btrfs_release_path(&path);
603 free(name);
604 free(data);
606 return ret;
609 static int copy_metadata(struct btrfs_root *root, int fd,
610 struct btrfs_key *key)
612 struct btrfs_path path;
613 struct btrfs_inode_item *inode_item;
614 int ret;
616 btrfs_init_path(&path);
617 ret = btrfs_lookup_inode(NULL, root, &path, key, 0);
618 if (ret == 0) {
619 struct btrfs_timespec *bts;
620 struct timespec times[2];
622 inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
623 struct btrfs_inode_item);
625 ret = fchown(fd, btrfs_inode_uid(path.nodes[0], inode_item),
626 btrfs_inode_gid(path.nodes[0], inode_item));
627 if (ret) {
628 error("failed to change owner: %m");
629 goto out;
632 ret = fchmod(fd, btrfs_inode_mode(path.nodes[0], inode_item));
633 if (ret) {
634 error("failed to change mode: %m");
635 goto out;
638 bts = btrfs_inode_atime(inode_item);
639 times[0].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
640 times[0].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
642 bts = btrfs_inode_mtime(inode_item);
643 times[1].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
644 times[1].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
646 ret = futimens(fd, times);
647 if (ret) {
648 error("failed to set times: %m");
649 goto out;
652 out:
653 btrfs_release_path(&path);
654 return ret;
657 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
658 const char *file)
660 struct extent_buffer *leaf;
661 struct btrfs_path path;
662 struct btrfs_file_extent_item *fi;
663 struct btrfs_inode_item *inode_item;
664 struct btrfs_timespec *bts;
665 struct btrfs_key found_key;
666 int ret;
667 int extent_type;
668 int compression;
669 int loops = 0;
670 u64 found_size = 0;
671 struct timespec times[2];
672 int times_ok = 0;
674 btrfs_init_path(&path);
675 ret = btrfs_lookup_inode(NULL, root, &path, key, 0);
676 if (ret == 0) {
677 inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
678 struct btrfs_inode_item);
679 found_size = btrfs_inode_size(path.nodes[0], inode_item);
681 if (restore_metadata) {
683 * Change the ownership and mode now, set times when
684 * copyout is finished.
687 ret = fchown(fd, btrfs_inode_uid(path.nodes[0], inode_item),
688 btrfs_inode_gid(path.nodes[0], inode_item));
689 if (ret && !ignore_errors)
690 goto out;
692 ret = fchmod(fd, btrfs_inode_mode(path.nodes[0], inode_item));
693 if (ret && !ignore_errors)
694 goto out;
696 bts = btrfs_inode_atime(inode_item);
697 times[0].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
698 times[0].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
700 bts = btrfs_inode_mtime(inode_item);
701 times[1].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
702 times[1].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
703 times_ok = 1;
706 btrfs_release_path(&path);
708 key->offset = 0;
709 key->type = BTRFS_EXTENT_DATA_KEY;
711 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
712 if (ret < 0) {
713 error("searching extent data returned %d", ret);
714 goto out;
717 leaf = path.nodes[0];
718 while (!leaf) {
719 ret = next_leaf(root, &path);
720 if (ret < 0) {
721 error("cannot get next leaf: %d", ret);
722 goto out;
723 } else if (ret > 0) {
724 /* No more leaves to search */
725 ret = 0;
726 goto out;
728 leaf = path.nodes[0];
731 while (1) {
732 if (loops >= 0 && loops++ >= 1024) {
733 enum loop_response resp;
735 resp = ask_to_continue(file);
736 if (resp == LOOP_STOP)
737 break;
738 else if (resp == LOOP_CONTINUE)
739 loops = 0;
740 else if (resp == LOOP_DONTASK)
741 loops = -1;
743 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
744 do {
745 ret = next_leaf(root, &path);
746 if (ret < 0) {
747 fprintf(stderr, "Error searching %d\n", ret);
748 goto out;
749 } else if (ret) {
750 /* No more leaves to search */
751 btrfs_release_path(&path);
752 goto set_size;
754 leaf = path.nodes[0];
755 } while (!leaf);
756 continue;
758 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
759 if (found_key.objectid != key->objectid)
760 break;
761 if (found_key.type != key->type)
762 break;
763 fi = btrfs_item_ptr(leaf, path.slots[0],
764 struct btrfs_file_extent_item);
765 extent_type = btrfs_file_extent_type(leaf, fi);
766 compression = btrfs_file_extent_compression(leaf, fi);
767 if (compression >= BTRFS_COMPRESS_LAST) {
768 warning("compression type %d not supported",
769 compression);
770 ret = -1;
771 goto out;
774 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
775 goto next;
776 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
777 ret = copy_one_inline(root, fd, &path, found_key.offset);
778 if (ret)
779 goto out;
780 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
781 ret = copy_one_extent(root, fd, leaf, fi,
782 found_key.offset);
783 if (ret)
784 goto out;
785 } else {
786 warning("weird extent type %d", extent_type);
788 next:
789 path.slots[0]++;
792 btrfs_release_path(&path);
793 set_size:
794 if (found_size) {
795 ret = ftruncate(fd, (loff_t)found_size);
796 if (ret)
797 return ret;
799 if (get_xattrs) {
800 ret = set_file_xattrs(root, key->objectid, fd, file);
801 if (ret)
802 return ret;
804 if (restore_metadata && times_ok) {
805 ret = futimens(fd, times);
806 if (ret)
807 return ret;
809 return 0;
811 out:
812 btrfs_release_path(&path);
813 return ret;
817 * returns:
818 * 0 if the file exists and should be skipped.
819 * 1 if the file does NOT exist
820 * 2 if the file exists but is OK to overwrite
822 static int overwrite_ok(const char * path)
824 static int warn = 0;
825 struct stat st;
826 int ret;
828 /* don't be fooled by symlinks */
829 ret = fstatat(AT_FDCWD, path_name, &st, AT_SYMLINK_NOFOLLOW);
831 if (!ret) {
832 if (overwrite)
833 return 2;
835 if (verbose || !warn)
836 printf("Skipping existing file"
837 " %s\n", path);
838 if (!warn)
839 printf("If you wish to overwrite use -o\n");
840 warn = 1;
841 return 0;
843 return 1;
846 static int copy_symlink(struct btrfs_root *root, struct btrfs_key *key,
847 const char *file)
849 struct btrfs_path path;
850 struct extent_buffer *leaf;
851 struct btrfs_file_extent_item *extent_item;
852 struct btrfs_inode_item *inode_item;
853 u32 len;
854 u32 name_offset;
855 int ret;
856 struct btrfs_timespec *bts;
857 struct timespec times[2];
859 ret = overwrite_ok(path_name);
860 if (ret == 0)
861 return 0; /* skip this file */
863 /* symlink() can't overwrite, so unlink first */
864 if (ret == 2) {
865 ret = unlink(path_name);
866 if (ret) {
867 fprintf(stderr, "failed to unlink '%s' for overwrite\n",
868 path_name);
869 return ret;
873 btrfs_init_path(&path);
874 key->type = BTRFS_EXTENT_DATA_KEY;
875 key->offset = 0;
876 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
877 if (ret < 0)
878 goto out;
880 leaf = path.nodes[0];
881 if (!leaf) {
882 fprintf(stderr, "Error getting leaf for symlink '%s'\n", file);
883 ret = -1;
884 goto out;
887 extent_item = btrfs_item_ptr(leaf, path.slots[0],
888 struct btrfs_file_extent_item);
890 len = btrfs_file_extent_inline_item_len(leaf,
891 btrfs_item_nr(path.slots[0]));
892 if (len >= PATH_MAX) {
893 fprintf(stderr, "Symlink '%s' target length %d is longer than PATH_MAX\n",
894 fs_name, len);
895 ret = -1;
896 goto out;
899 name_offset = (unsigned long) extent_item
900 + offsetof(struct btrfs_file_extent_item, disk_bytenr);
901 read_extent_buffer(leaf, symlink_target, name_offset, len);
903 symlink_target[len] = 0;
905 if (!dry_run) {
906 ret = symlink(symlink_target, path_name);
907 if (ret < 0) {
908 fprintf(stderr, "Failed to restore symlink '%s': %m\n",
909 path_name);
910 goto out;
913 printf("SYMLINK: '%s' => '%s'\n", path_name, symlink_target);
915 ret = 0;
916 if (!restore_metadata)
917 goto out;
920 * Symlink metadata operates differently than files/directories, so do
921 * our own work here.
923 key->type = BTRFS_INODE_ITEM_KEY;
924 key->offset = 0;
926 btrfs_release_path(&path);
928 ret = btrfs_lookup_inode(NULL, root, &path, key, 0);
929 if (ret) {
930 fprintf(stderr, "Failed to lookup inode for '%s'\n", file);
931 goto out;
934 inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
935 struct btrfs_inode_item);
937 ret = fchownat(-1, file, btrfs_inode_uid(path.nodes[0], inode_item),
938 btrfs_inode_gid(path.nodes[0], inode_item),
939 AT_SYMLINK_NOFOLLOW);
940 if (ret) {
941 fprintf(stderr, "Failed to change owner: %m\n");
942 goto out;
945 bts = btrfs_inode_atime(inode_item);
946 times[0].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
947 times[0].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
949 bts = btrfs_inode_mtime(inode_item);
950 times[1].tv_sec = btrfs_timespec_sec(path.nodes[0], bts);
951 times[1].tv_nsec = btrfs_timespec_nsec(path.nodes[0], bts);
953 ret = utimensat(-1, file, times, AT_SYMLINK_NOFOLLOW);
954 if (ret)
955 fprintf(stderr, "Failed to set times: %m\n");
956 out:
957 btrfs_release_path(&path);
958 return ret;
961 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
962 const char *output_rootdir, const char *in_dir,
963 const regex_t *mreg)
965 struct btrfs_path path;
966 struct extent_buffer *leaf;
967 struct btrfs_dir_item *dir_item;
968 struct btrfs_key found_key, location;
969 char filename[BTRFS_NAME_LEN + 1];
970 unsigned long name_ptr;
971 int name_len;
972 int ret = 0;
973 int fd;
974 int loops = 0;
975 u8 type;
977 btrfs_init_path(&path);
978 key->offset = 0;
979 key->type = BTRFS_DIR_INDEX_KEY;
980 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
981 if (ret < 0) {
982 fprintf(stderr, "Error searching %d\n", ret);
983 goto out;
986 ret = 0;
988 leaf = path.nodes[0];
989 while (!leaf) {
990 if (verbose > 1)
991 printf("No leaf after search, looking for the next "
992 "leaf\n");
993 ret = next_leaf(root, &path);
994 if (ret < 0) {
995 fprintf(stderr, "Error getting next leaf %d\n",
996 ret);
997 goto out;
998 } else if (ret > 0) {
999 /* No more leaves to search */
1000 if (verbose)
1001 printf("Reached the end of the tree looking "
1002 "for the directory\n");
1003 ret = 0;
1004 goto out;
1006 leaf = path.nodes[0];
1009 while (leaf) {
1010 if (loops++ >= 1024) {
1011 printf("We have looped trying to restore files in %s "
1012 "too many times to be making progress, "
1013 "stopping\n", in_dir);
1014 break;
1017 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1018 do {
1019 ret = next_leaf(root, &path);
1020 if (ret < 0) {
1021 fprintf(stderr, "Error searching %d\n",
1022 ret);
1023 goto out;
1024 } else if (ret > 0) {
1025 /* No more leaves to search */
1026 if (verbose)
1027 printf("Reached the end of "
1028 "the tree searching the"
1029 " directory\n");
1030 ret = 0;
1031 goto out;
1033 leaf = path.nodes[0];
1034 } while (!leaf);
1035 continue;
1037 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
1038 if (found_key.objectid != key->objectid) {
1039 if (verbose > 1)
1040 printf("Found objectid=%Lu, key=%Lu\n",
1041 found_key.objectid, key->objectid);
1042 break;
1044 if (found_key.type != key->type) {
1045 if (verbose > 1)
1046 printf("Found type=%u, want=%u\n",
1047 found_key.type, key->type);
1048 break;
1050 dir_item = btrfs_item_ptr(leaf, path.slots[0],
1051 struct btrfs_dir_item);
1052 name_ptr = (unsigned long)(dir_item + 1);
1053 name_len = btrfs_dir_name_len(leaf, dir_item);
1054 read_extent_buffer(leaf, filename, name_ptr, name_len);
1055 filename[name_len] = '\0';
1056 type = btrfs_dir_type(leaf, dir_item);
1057 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
1059 /* full path from root of btrfs being restored */
1060 snprintf(fs_name, PATH_MAX, "%s/%s", in_dir, filename);
1062 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
1063 goto next;
1065 /* full path from system root */
1066 snprintf(path_name, PATH_MAX, "%s%s", output_rootdir, fs_name);
1069 * Restore directories, files, symlinks and metadata.
1071 if (type == BTRFS_FT_REG_FILE) {
1072 if (!overwrite_ok(path_name))
1073 goto next;
1075 if (verbose)
1076 printf("Restoring %s\n", path_name);
1077 if (dry_run)
1078 goto next;
1079 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
1080 if (fd < 0) {
1081 fprintf(stderr, "Error creating %s: %d\n",
1082 path_name, errno);
1083 if (ignore_errors)
1084 goto next;
1085 ret = -1;
1086 goto out;
1088 loops = 0;
1089 ret = copy_file(root, fd, &location, path_name);
1090 close(fd);
1091 if (ret) {
1092 fprintf(stderr, "Error copying data for %s\n",
1093 path_name);
1094 if (ignore_errors)
1095 goto next;
1096 goto out;
1098 } else if (type == BTRFS_FT_DIR) {
1099 struct btrfs_root *search_root = root;
1100 char *dir = strdup(fs_name);
1102 if (!dir) {
1103 fprintf(stderr, "Ran out of memory\n");
1104 ret = -ENOMEM;
1105 goto out;
1108 if (location.type == BTRFS_ROOT_ITEM_KEY) {
1110 * If we are a snapshot and this is the index
1111 * object to ourselves just skip it.
1113 if (location.objectid ==
1114 root->root_key.objectid) {
1115 free(dir);
1116 goto next;
1119 location.offset = (u64)-1;
1120 search_root = btrfs_read_fs_root(root->fs_info,
1121 &location);
1122 if (IS_ERR(search_root)) {
1123 free(dir);
1124 fprintf(stderr, "Error reading "
1125 "subvolume %s: %lu\n",
1126 path_name,
1127 PTR_ERR(search_root));
1128 if (ignore_errors)
1129 goto next;
1130 ret = PTR_ERR(search_root);
1131 goto out;
1135 * A subvolume will have a key.offset of 0, a
1136 * snapshot will have key.offset of a transid.
1138 if (search_root->root_key.offset != 0 &&
1139 get_snaps == 0) {
1140 free(dir);
1141 printf("Skipping snapshot %s\n",
1142 filename);
1143 goto next;
1145 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
1148 if (verbose)
1149 printf("Restoring %s\n", path_name);
1151 errno = 0;
1152 if (dry_run)
1153 ret = 0;
1154 else
1155 ret = mkdir(path_name, 0755);
1156 if (ret && errno != EEXIST) {
1157 free(dir);
1158 fprintf(stderr, "Error mkdiring %s: %d\n",
1159 path_name, errno);
1160 if (ignore_errors)
1161 goto next;
1162 ret = -1;
1163 goto out;
1165 loops = 0;
1166 ret = search_dir(search_root, &location,
1167 output_rootdir, dir, mreg);
1168 free(dir);
1169 if (ret) {
1170 fprintf(stderr, "Error searching %s\n",
1171 path_name);
1172 if (ignore_errors)
1173 goto next;
1174 goto out;
1176 } else if (type == BTRFS_FT_SYMLINK) {
1177 if (restore_symlinks)
1178 ret = copy_symlink(root, &location, path_name);
1179 if (ret < 0) {
1180 if (ignore_errors)
1181 goto next;
1182 btrfs_release_path(&path);
1183 return ret;
1186 next:
1187 path.slots[0]++;
1190 if (restore_metadata) {
1191 snprintf(path_name, PATH_MAX, "%s%s", output_rootdir, in_dir);
1192 fd = open(path_name, O_RDONLY);
1193 if (fd < 0) {
1194 fprintf(stderr, "ERROR: Failed to access %s to restore metadata\n",
1195 path_name);
1196 if (!ignore_errors) {
1197 ret = -1;
1198 goto out;
1200 } else {
1202 * Set owner/mode/time on the directory as well
1204 key->type = BTRFS_INODE_ITEM_KEY;
1205 ret = copy_metadata(root, fd, key);
1206 close(fd);
1207 if (ret && !ignore_errors)
1208 goto out;
1212 if (verbose)
1213 printf("Done searching %s\n", in_dir);
1214 out:
1215 btrfs_release_path(&path);
1216 return ret;
1219 static int do_list_roots(struct btrfs_root *root)
1221 struct btrfs_key key;
1222 struct btrfs_key found_key;
1223 struct btrfs_disk_key disk_key;
1224 struct btrfs_path path;
1225 struct extent_buffer *leaf;
1226 struct btrfs_root_item ri;
1227 unsigned long offset;
1228 int slot;
1229 int ret;
1231 root = root->fs_info->tree_root;
1233 btrfs_init_path(&path);
1234 key.offset = 0;
1235 key.objectid = 0;
1236 key.type = BTRFS_ROOT_ITEM_KEY;
1237 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1238 if (ret < 0) {
1239 fprintf(stderr, "Failed to do search %d\n", ret);
1240 btrfs_release_path(&path);
1241 return -1;
1244 leaf = path.nodes[0];
1246 while (1) {
1247 slot = path.slots[0];
1248 if (slot >= btrfs_header_nritems(leaf)) {
1249 ret = btrfs_next_leaf(root, &path);
1250 if (ret)
1251 break;
1252 leaf = path.nodes[0];
1253 slot = path.slots[0];
1255 btrfs_item_key(leaf, &disk_key, slot);
1256 btrfs_disk_key_to_cpu(&found_key, &disk_key);
1257 if (found_key.type != BTRFS_ROOT_ITEM_KEY) {
1258 path.slots[0]++;
1259 continue;
1262 offset = btrfs_item_ptr_offset(leaf, slot);
1263 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
1264 printf(" tree ");
1265 btrfs_print_key(&disk_key);
1266 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
1267 btrfs_root_level(&ri));
1268 path.slots[0]++;
1270 btrfs_release_path(&path);
1272 return 0;
1275 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
1276 int super_mirror, int list_roots)
1278 struct btrfs_fs_info *fs_info = NULL;
1279 struct btrfs_root *root = NULL;
1280 u64 bytenr;
1281 int i;
1283 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1284 bytenr = btrfs_sb_offset(i);
1287 * Restore won't allocate extent and doesn't care anything
1288 * in extent tree. Skip block group item search will allow
1289 * restore to be executed on heavily damaged fs.
1291 fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0,
1292 OPEN_CTREE_PARTIAL |
1293 OPEN_CTREE_NO_BLOCK_GROUPS);
1294 if (fs_info)
1295 break;
1296 fprintf(stderr, "Could not open root, trying backup super\n");
1299 if (!fs_info)
1300 return NULL;
1303 * All we really need to succeed is reading the chunk tree, everything
1304 * else we can do by hand, since we only need to read the tree root and
1305 * the fs_root.
1307 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1308 u64 generation;
1310 root = fs_info->tree_root;
1311 if (!root_location)
1312 root_location = btrfs_super_root(fs_info->super_copy);
1313 generation = btrfs_super_generation(fs_info->super_copy);
1314 root->node = read_tree_block(fs_info, root_location,
1315 generation);
1316 if (!extent_buffer_uptodate(root->node)) {
1317 fprintf(stderr, "Error opening tree root\n");
1318 close_ctree(root);
1319 return NULL;
1323 if (!list_roots && !fs_info->fs_root) {
1324 struct btrfs_key key;
1326 key.objectid = BTRFS_FS_TREE_OBJECTID;
1327 key.type = BTRFS_ROOT_ITEM_KEY;
1328 key.offset = (u64)-1;
1329 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1330 if (IS_ERR(fs_info->fs_root)) {
1331 fprintf(stderr, "Couldn't read fs root: %ld\n",
1332 PTR_ERR(fs_info->fs_root));
1333 close_ctree(fs_info->tree_root);
1334 return NULL;
1338 if (list_roots && do_list_roots(fs_info->tree_root)) {
1339 close_ctree(fs_info->tree_root);
1340 return NULL;
1343 return fs_info->fs_root;
1346 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1348 struct btrfs_path path;
1349 struct btrfs_key found_key;
1350 struct btrfs_key key;
1351 int ret = -1;
1352 int i;
1354 btrfs_init_path(&path);
1355 key.objectid = 0;
1356 key.type = BTRFS_DIR_INDEX_KEY;
1357 key.offset = 0;
1358 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1359 if (ret < 0) {
1360 fprintf(stderr, "Error searching %d\n", ret);
1361 goto out;
1364 if (!path.nodes[0]) {
1365 fprintf(stderr, "No leaf!\n");
1366 goto out;
1368 again:
1369 for (i = path.slots[0];
1370 i < btrfs_header_nritems(path.nodes[0]); i++) {
1371 btrfs_item_key_to_cpu(path.nodes[0], &found_key, i);
1372 if (found_key.type != key.type)
1373 continue;
1375 printf("Using objectid %Lu for first dir\n",
1376 found_key.objectid);
1377 *objectid = found_key.objectid;
1378 ret = 0;
1379 goto out;
1381 do {
1382 ret = next_leaf(root, &path);
1383 if (ret < 0) {
1384 fprintf(stderr, "Error getting next leaf %d\n",
1385 ret);
1386 goto out;
1387 } else if (ret > 0) {
1388 fprintf(stderr, "No more leaves\n");
1389 goto out;
1391 } while (!path.nodes[0]);
1392 if (path.nodes[0])
1393 goto again;
1394 printf("Couldn't find a dir index item\n");
1395 out:
1396 btrfs_release_path(&path);
1397 return ret;
1400 const char * const cmd_restore_usage[] = {
1401 "btrfs restore [options] <device> <path> | -l <device>",
1402 "Try to restore files from a damaged filesystem (unmounted)",
1404 "-s|--snapshots get snapshots",
1405 "-x|--xattr restore extended attributes",
1406 "-m|--metadata restore owner, mode and times",
1407 "-S|--symlink restore symbolic links",
1408 "-v|--verbose verbose",
1409 "-i|--ignore-errors ignore errors",
1410 "-o|--overwrite overwrite",
1411 "-t <bytenr> tree location",
1412 "-f <bytenr> filesystem location",
1413 "-u|--super <mirror> super mirror",
1414 "-r|--root <rootid> root objectid",
1415 "-d find dir",
1416 "-l|--list-roots list tree roots",
1417 "-D|--dry-run dry run (only list files that would be recovered)",
1418 "--path-regex <regex>",
1419 " restore only filenames matching regex,",
1420 " you have to use following syntax (possibly quoted):",
1421 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1422 "-c ignore case (--path-regex only)",
1423 NULL
1426 int cmd_restore(int argc, char **argv)
1428 struct btrfs_root *root;
1429 struct btrfs_key key;
1430 char dir_name[PATH_MAX];
1431 u64 tree_location = 0;
1432 u64 fs_location = 0;
1433 u64 root_objectid = 0;
1434 int len;
1435 int ret;
1436 int super_mirror = 0;
1437 int find_dir = 0;
1438 int list_roots = 0;
1439 const char *match_regstr = NULL;
1440 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1441 regex_t match_reg, *mreg = NULL;
1442 char reg_err[256];
1444 optind = 0;
1445 while (1) {
1446 int opt;
1447 enum { GETOPT_VAL_PATH_REGEX = 256 };
1448 static const struct option long_options[] = {
1449 { "path-regex", required_argument, NULL,
1450 GETOPT_VAL_PATH_REGEX },
1451 { "dry-run", no_argument, NULL, 'D'},
1452 { "metadata", no_argument, NULL, 'm'},
1453 { "symlinks", no_argument, NULL, 'S'},
1454 { "snapshots", no_argument, NULL, 's'},
1455 { "xattr", no_argument, NULL, 'x'},
1456 { "verbose", no_argument, NULL, 'v'},
1457 { "ignore-errors", no_argument, NULL, 'i'},
1458 { "overwrite", no_argument, NULL, 'o'},
1459 { "super", required_argument, NULL, 'u'},
1460 { "root", required_argument, NULL, 'r'},
1461 { "list-roots", no_argument, NULL, 'l'},
1462 { NULL, 0, NULL, 0}
1465 opt = getopt_long(argc, argv, "sSxviot:u:dmf:r:lDc", long_options,
1466 NULL);
1467 if (opt < 0)
1468 break;
1470 switch (opt) {
1471 case 's':
1472 get_snaps = 1;
1473 break;
1474 case 'v':
1475 verbose++;
1476 break;
1477 case 'i':
1478 ignore_errors = 1;
1479 break;
1480 case 'o':
1481 overwrite = 1;
1482 break;
1483 case 't':
1484 tree_location = arg_strtou64(optarg);
1485 break;
1486 case 'f':
1487 fs_location = arg_strtou64(optarg);
1488 break;
1489 case 'u':
1490 super_mirror = arg_strtou64(optarg);
1491 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1492 fprintf(stderr, "Super mirror not "
1493 "valid\n");
1494 exit(1);
1496 break;
1497 case 'd':
1498 find_dir = 1;
1499 break;
1500 case 'r':
1501 root_objectid = arg_strtou64(optarg);
1502 if (!is_fstree(root_objectid)) {
1503 fprintf(stderr, "objectid %llu is not a valid fs/file tree\n",
1504 root_objectid);
1505 exit(1);
1507 break;
1508 case 'l':
1509 list_roots = 1;
1510 break;
1511 case 'm':
1512 restore_metadata = 1;
1513 break;
1514 case 'S':
1515 restore_symlinks = 1;
1516 break;
1517 case 'D':
1518 dry_run = 1;
1519 break;
1520 case 'c':
1521 match_cflags |= REG_ICASE;
1522 break;
1523 case GETOPT_VAL_PATH_REGEX:
1524 match_regstr = optarg;
1525 break;
1526 case 'x':
1527 get_xattrs = 1;
1528 break;
1529 default:
1530 usage(cmd_restore_usage);
1534 if (!list_roots && check_argc_min(argc - optind, 2))
1535 usage(cmd_restore_usage);
1536 else if (list_roots && check_argc_min(argc - optind, 1))
1537 usage(cmd_restore_usage);
1539 if (fs_location && root_objectid) {
1540 fprintf(stderr, "don't use -f and -r at the same time.\n");
1541 return 1;
1544 if ((ret = check_mounted(argv[optind])) < 0) {
1545 fprintf(stderr, "Could not check mount status: %s\n",
1546 strerror(-ret));
1547 return 1;
1548 } else if (ret) {
1549 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1550 return 1;
1553 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1554 if (root == NULL)
1555 return 1;
1557 if (list_roots)
1558 goto out;
1560 if (fs_location != 0) {
1561 free_extent_buffer(root->node);
1562 root->node = read_tree_block(root->fs_info, fs_location, 0);
1563 if (!extent_buffer_uptodate(root->node)) {
1564 fprintf(stderr, "Failed to read fs location\n");
1565 ret = 1;
1566 goto out;
1570 memset(path_name, 0, PATH_MAX);
1572 if (strlen(argv[optind + 1]) >= PATH_MAX) {
1573 fprintf(stderr, "ERROR: path too long\n");
1574 ret = 1;
1575 goto out;
1577 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1578 dir_name[sizeof dir_name - 1] = 0;
1580 /* Strip the trailing / on the dir name */
1581 len = strlen(dir_name);
1582 while (len && dir_name[--len] == '/') {
1583 dir_name[len] = '\0';
1586 if (root_objectid != 0) {
1587 struct btrfs_root *orig_root = root;
1589 key.objectid = root_objectid;
1590 key.type = BTRFS_ROOT_ITEM_KEY;
1591 key.offset = (u64)-1;
1592 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1593 if (IS_ERR(root)) {
1594 fprintf(stderr, "fail to read root %llu: %s\n",
1595 root_objectid, strerror(-PTR_ERR(root)));
1596 root = orig_root;
1597 ret = 1;
1598 goto out;
1600 key.type = 0;
1601 key.offset = 0;
1604 if (find_dir) {
1605 ret = find_first_dir(root, &key.objectid);
1606 if (ret)
1607 goto out;
1608 } else {
1609 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1612 if (match_regstr) {
1613 ret = regcomp(&match_reg, match_regstr, match_cflags);
1614 if (ret) {
1615 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1616 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1617 goto out;
1619 mreg = &match_reg;
1622 if (dry_run)
1623 printf("This is a dry-run, no files are going to be restored\n");
1625 ret = search_dir(root, &key, dir_name, "", mreg);
1627 out:
1628 if (mreg)
1629 regfree(mreg);
1630 close_ctree(root);
1631 return !!ret;