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.
19 #define _XOPEN_SOURCE 500
22 #include "kerncompat.h"
30 #include <sys/types.h>
31 #include <lzo/lzoconf.h>
32 #include <lzo/lzo1x.h>
36 #include <sys/types.h>
37 #include <attr/xattr.h>
41 #include "print-tree.h"
42 #include "transaction.h"
49 static char fs_name[4096];
50 static char path_name[4096];
51 static int get_snaps = 0;
52 static int verbose = 0;
53 static int ignore_errors = 0;
54 static int overwrite = 0;
55 static int get_xattrs = 0;
56 static int dry_run = 0;
59 #define PAGE_CACHE_SIZE 4096
60 #define lzo1x_worst_compress(x) ((x) + ((x) / 16) + 64 + 3)
62 static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
68 memset(&strm, 0, sizeof(strm));
69 ret = inflateInit(&strm);
71 fprintf(stderr, "inflate init returnd %d\n", ret);
75 strm.avail_in = compress_len;
76 strm.next_in = (unsigned char *)inbuf;
77 strm.avail_out = decompress_len;
78 strm.next_out = (unsigned char *)outbuf;
79 ret = inflate(&strm, Z_NO_FLUSH);
80 if (ret != Z_STREAM_END) {
81 (void)inflateEnd(&strm);
82 fprintf(stderr, "failed to inflate: %d\n", ret);
86 (void)inflateEnd(&strm);
89 static inline size_t read_compress_length(unsigned char *buf)
92 memcpy(&dlen, buf, LZO_LEN);
93 return le32_to_cpu(dlen);
96 static int decompress_lzo(unsigned char *inbuf, char *outbuf, u64 compress_len,
107 if (ret != LZO_E_OK) {
108 fprintf(stderr, "lzo init returned %d\n", ret);
112 tot_len = read_compress_length(inbuf);
116 while (tot_in < tot_len) {
117 in_len = read_compress_length(inbuf);
121 new_len = lzo1x_worst_compress(PAGE_CACHE_SIZE);
122 ret = lzo1x_decompress_safe((const unsigned char *)inbuf, in_len,
123 (unsigned char *)outbuf,
124 (void *)&new_len, NULL);
125 if (ret != LZO_E_OK) {
126 fprintf(stderr, "failed to inflate: %d\n", ret);
135 *decompress_len = out_len;
140 static int decompress(char *inbuf, char *outbuf, u64 compress_len,
141 u64 *decompress_len, int compress)
144 case BTRFS_COMPRESS_ZLIB:
145 return decompress_zlib(inbuf, outbuf, compress_len,
147 case BTRFS_COMPRESS_LZO:
148 return decompress_lzo((unsigned char *)inbuf, outbuf, compress_len,
154 fprintf(stderr, "invalid compression type: %d\n", compress);
158 static int next_leaf(struct btrfs_root *root, struct btrfs_path *path)
163 struct extent_buffer *c;
164 struct extent_buffer *next = NULL;
167 for (; level < BTRFS_MAX_LEVEL; level++) {
168 if (path->nodes[level])
172 if (level == BTRFS_MAX_LEVEL)
175 slot = path->slots[level] + 1;
177 while(level < BTRFS_MAX_LEVEL) {
178 if (!path->nodes[level])
181 slot = path->slots[level] + offset;
182 c = path->nodes[level];
183 if (slot >= btrfs_header_nritems(c)) {
185 if (level == BTRFS_MAX_LEVEL)
191 reada_for_search(root, path, level, slot, 0);
193 next = read_node_slot(root, c, slot);
198 path->slots[level] = slot;
201 c = path->nodes[level];
202 free_extent_buffer(c);
203 path->nodes[level] = next;
204 path->slots[level] = 0;
208 reada_for_search(root, path, level, 0, 0);
209 next = read_node_slot(root, next, 0);
216 static int copy_one_inline(int fd, struct btrfs_path *path, u64 pos)
218 struct extent_buffer *leaf = path->nodes[0];
219 struct btrfs_file_extent_item *fi;
229 fi = btrfs_item_ptr(leaf, path->slots[0],
230 struct btrfs_file_extent_item);
231 ptr = btrfs_file_extent_inline_start(fi);
232 len = btrfs_file_extent_inline_item_len(leaf,
233 btrfs_item_nr(path->slots[0]));
234 read_extent_buffer(leaf, buf, ptr, len);
236 compress = btrfs_file_extent_compression(leaf, fi);
237 if (compress == BTRFS_COMPRESS_NONE) {
238 done = pwrite(fd, buf, len, pos);
240 fprintf(stderr, "Short inline write, wanted %d, did "
241 "%zd: %d\n", len, done, errno);
247 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
248 outbuf = malloc(ram_size);
250 fprintf(stderr, "No memory\n");
254 ret = decompress(buf, outbuf, len, &ram_size, compress);
260 done = pwrite(fd, outbuf, ram_size, pos);
262 if (done < ram_size) {
263 fprintf(stderr, "Short compressed inline write, wanted %Lu, "
264 "did %zd: %d\n", ram_size, done, errno);
271 static int copy_one_extent(struct btrfs_root *root, int fd,
272 struct extent_buffer *leaf,
273 struct btrfs_file_extent_item *fi, u64 pos)
275 struct btrfs_multi_bio *multi = NULL;
276 struct btrfs_device *device;
277 char *inbuf, *outbuf = NULL;
278 ssize_t done, total = 0;
294 compress = btrfs_file_extent_compression(leaf, fi);
295 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
296 disk_size = btrfs_file_extent_disk_num_bytes(leaf, fi);
297 ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
298 offset = btrfs_file_extent_offset(leaf, fi);
299 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
300 size_left = num_bytes;
304 printf("offset is %Lu\n", offset);
305 /* we found a hole */
309 inbuf = malloc(size_left);
311 fprintf(stderr, "No memory\n");
315 if (compress != BTRFS_COMPRESS_NONE) {
316 outbuf = malloc(ram_size);
318 fprintf(stderr, "No memory\n");
325 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
326 bytenr, &length, &multi, mirror_num, NULL);
328 fprintf(stderr, "Error mapping block %d\n", ret);
331 device = multi->stripes[0].dev;
334 dev_bytenr = multi->stripes[0].physical;
337 if (size_left < length)
340 done = pread(dev_fd, inbuf+count, length, dev_bytenr);
341 /* Need both checks, or we miss negative values due to u64 conversion */
342 if (done < 0 || done < length) {
343 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
346 /* mirror_num is 1-indexed, so num_copies is a valid mirror. */
347 if (mirror_num > num_copies) {
349 fprintf(stderr, "Exhausted mirrors trying to read\n");
352 fprintf(stderr, "Trying another mirror\n");
363 if (compress == BTRFS_COMPRESS_NONE) {
364 while (total < num_bytes) {
365 done = pwrite(fd, inbuf+total, num_bytes-total,
369 fprintf(stderr, "Error writing: %d %s\n", errno, strerror(errno));
378 ret = decompress(inbuf, outbuf, num_bytes, &ram_size, compress);
380 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
383 if (mirror_num >= num_copies) {
387 fprintf(stderr, "Trying another mirror\n");
391 while (total < ram_size) {
392 done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
405 static int ask_to_continue(const char *file)
410 printf("We seem to be looping a lot on %s, do you want to keep going "
411 "on ? (y/N): ", file);
413 ret = fgets(buf, 2, stdin);
414 if (*ret == '\n' || tolower(*ret) == 'n')
416 if (tolower(*ret) != 'y') {
417 printf("Please enter either 'y' or 'n': ");
425 static int set_file_xattrs(struct btrfs_root *root, u64 inode,
426 int fd, const char *file_name)
428 struct btrfs_key key;
429 struct btrfs_path *path;
430 struct extent_buffer *leaf;
431 struct btrfs_dir_item *di;
440 key.objectid = inode;
441 key.type = BTRFS_XATTR_ITEM_KEY;
444 path = btrfs_alloc_path();
448 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
452 leaf = path->nodes[0];
454 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
456 ret = next_leaf(root, path);
459 "Error searching for extended attributes: %d\n",
463 /* No more leaves to search */
467 leaf = path->nodes[0];
472 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
473 if (key.type != BTRFS_XATTR_ITEM_KEY || key.objectid != inode)
476 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
477 di = btrfs_item_ptr(leaf, path->slots[0],
478 struct btrfs_dir_item);
480 while (cur < total_len) {
481 len = btrfs_dir_name_len(leaf, di);
482 if (len > name_len) {
484 name = (char *) malloc(len + 1);
490 read_extent_buffer(leaf, name,
491 (unsigned long)(di + 1), len);
495 len = btrfs_dir_data_len(leaf, di);
496 if (len > data_len) {
498 data = (char *) malloc(len);
504 read_extent_buffer(leaf, data,
505 (unsigned long)(di + 1) + name_len,
509 if (fsetxattr(fd, name, data, data_len, 0)) {
513 "Error setting extended attribute %s on file %s: %s\n",
514 name, file_name, strerror(err));
517 len = sizeof(*di) + name_len + data_len;
519 di = (struct btrfs_dir_item *)((char *)di + len);
525 btrfs_free_path(path);
533 static int copy_file(struct btrfs_root *root, int fd, struct btrfs_key *key,
536 struct extent_buffer *leaf;
537 struct btrfs_path *path;
538 struct btrfs_file_extent_item *fi;
539 struct btrfs_inode_item *inode_item;
540 struct btrfs_key found_key;
547 path = btrfs_alloc_path();
549 fprintf(stderr, "Ran out of memory\n");
552 path->skip_locking = 1;
554 ret = btrfs_lookup_inode(NULL, root, path, key, 0);
556 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
557 struct btrfs_inode_item);
558 found_size = btrfs_inode_size(path->nodes[0], inode_item);
560 btrfs_release_path(path);
563 key->type = BTRFS_EXTENT_DATA_KEY;
565 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
567 fprintf(stderr, "Error searching %d\n", ret);
568 btrfs_free_path(path);
572 leaf = path->nodes[0];
574 ret = next_leaf(root, path);
576 fprintf(stderr, "Error getting next leaf %d\n",
578 btrfs_free_path(path);
580 } else if (ret > 0) {
581 /* No more leaves to search */
582 btrfs_free_path(path);
585 leaf = path->nodes[0];
589 if (loops++ >= 1024) {
590 ret = ask_to_continue(file);
595 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
597 ret = next_leaf(root, path);
599 fprintf(stderr, "Error searching %d\n", ret);
600 btrfs_free_path(path);
603 /* No more leaves to search */
604 btrfs_free_path(path);
607 leaf = path->nodes[0];
611 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
612 if (found_key.objectid != key->objectid)
614 if (found_key.type != key->type)
616 fi = btrfs_item_ptr(leaf, path->slots[0],
617 struct btrfs_file_extent_item);
618 extent_type = btrfs_file_extent_type(leaf, fi);
619 compression = btrfs_file_extent_compression(leaf, fi);
620 if (compression >= BTRFS_COMPRESS_LAST) {
621 fprintf(stderr, "Don't support compression yet %d\n",
623 btrfs_free_path(path);
627 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC)
629 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
630 ret = copy_one_inline(fd, path, found_key.offset);
632 btrfs_free_path(path);
635 } else if (extent_type == BTRFS_FILE_EXTENT_REG) {
636 ret = copy_one_extent(root, fd, leaf, fi,
639 btrfs_free_path(path);
643 printf("Weird extent type %d\n", extent_type);
649 btrfs_free_path(path);
652 ret = ftruncate(fd, (loff_t)found_size);
657 ret = set_file_xattrs(root, key->objectid, fd, file);
664 static int search_dir(struct btrfs_root *root, struct btrfs_key *key,
665 const char *output_rootdir, const char *in_dir,
668 struct btrfs_path *path;
669 struct extent_buffer *leaf;
670 struct btrfs_dir_item *dir_item;
671 struct btrfs_key found_key, location;
672 char filename[BTRFS_NAME_LEN + 1];
673 unsigned long name_ptr;
680 path = btrfs_alloc_path();
682 fprintf(stderr, "Ran out of memory\n");
685 path->skip_locking = 1;
688 key->type = BTRFS_DIR_INDEX_KEY;
690 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
692 fprintf(stderr, "Error searching %d\n", ret);
693 btrfs_free_path(path);
697 leaf = path->nodes[0];
700 printf("No leaf after search, looking for the next "
702 ret = next_leaf(root, path);
704 fprintf(stderr, "Error getting next leaf %d\n",
706 btrfs_free_path(path);
708 } else if (ret > 0) {
709 /* No more leaves to search */
711 printf("Reached the end of the tree looking "
712 "for the directory\n");
713 btrfs_free_path(path);
716 leaf = path->nodes[0];
720 if (loops++ >= 1024) {
721 printf("We have looped trying to restore files in %s "
722 "too many times to be making progress, "
723 "stopping\n", in_dir);
727 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
729 ret = next_leaf(root, path);
731 fprintf(stderr, "Error searching %d\n",
733 btrfs_free_path(path);
735 } else if (ret > 0) {
736 /* No more leaves to search */
738 printf("Reached the end of "
739 "the tree searching the"
741 btrfs_free_path(path);
744 leaf = path->nodes[0];
748 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
749 if (found_key.objectid != key->objectid) {
751 printf("Found objectid=%Lu, key=%Lu\n",
752 found_key.objectid, key->objectid);
755 if (found_key.type != key->type) {
757 printf("Found type=%u, want=%u\n",
758 found_key.type, key->type);
761 dir_item = btrfs_item_ptr(leaf, path->slots[0],
762 struct btrfs_dir_item);
763 name_ptr = (unsigned long)(dir_item + 1);
764 name_len = btrfs_dir_name_len(leaf, dir_item);
765 read_extent_buffer(leaf, filename, name_ptr, name_len);
766 filename[name_len] = '\0';
767 type = btrfs_dir_type(leaf, dir_item);
768 btrfs_dir_item_key_to_cpu(leaf, dir_item, &location);
770 /* full path from root of btrfs being restored */
771 snprintf(fs_name, 4096, "%s/%s", in_dir, filename);
773 if (mreg && REG_NOMATCH == regexec(mreg, fs_name, 0, NULL, 0))
776 /* full path from system root */
777 snprintf(path_name, 4096, "%s%s", output_rootdir, fs_name);
780 * At this point we're only going to restore directories and
781 * files, no symlinks or anything else.
783 if (type == BTRFS_FT_REG_FILE) {
788 ret = stat(path_name, &st);
791 if (verbose || !warn)
792 printf("Skipping existing file"
796 printf("If you wish to overwrite use "
797 "the -o option to overwrite\n");
804 printf("Restoring %s\n", path_name);
807 fd = open(path_name, O_CREAT|O_WRONLY, 0644);
809 fprintf(stderr, "Error creating %s: %d\n",
813 btrfs_free_path(path);
817 ret = copy_file(root, fd, &location, path_name);
822 btrfs_free_path(path);
825 } else if (type == BTRFS_FT_DIR) {
826 struct btrfs_root *search_root = root;
827 char *dir = strdup(fs_name);
830 fprintf(stderr, "Ran out of memory\n");
831 btrfs_free_path(path);
835 if (location.type == BTRFS_ROOT_ITEM_KEY) {
837 * If we are a snapshot and this is the index
838 * object to ourselves just skip it.
840 if (location.objectid ==
841 root->root_key.objectid) {
846 location.offset = (u64)-1;
847 search_root = btrfs_read_fs_root(root->fs_info,
849 if (IS_ERR(search_root)) {
851 fprintf(stderr, "Error reading "
852 "subvolume %s: %lu\n",
854 PTR_ERR(search_root));
857 btrfs_free_path(path);
858 return PTR_ERR(search_root);
862 * A subvolume will have a key.offset of 0, a
863 * snapshot will have key.offset of a transid.
865 if (search_root->root_key.offset != 0 &&
868 printf("Skipping snapshot %s\n",
872 location.objectid = BTRFS_FIRST_FREE_OBJECTID;
876 printf("Restoring %s\n", path_name);
882 ret = mkdir(path_name, 0755);
883 if (ret && errno != EEXIST) {
885 fprintf(stderr, "Error mkdiring %s: %d\n",
889 btrfs_free_path(path);
893 ret = search_dir(search_root, &location,
894 output_rootdir, dir, mreg);
899 btrfs_free_path(path);
908 printf("Done searching %s\n", in_dir);
909 btrfs_free_path(path);
913 static int do_list_roots(struct btrfs_root *root)
915 struct btrfs_key key;
916 struct btrfs_key found_key;
917 struct btrfs_disk_key disk_key;
918 struct btrfs_path *path;
919 struct extent_buffer *leaf;
920 struct btrfs_root_item ri;
921 unsigned long offset;
925 root = root->fs_info->tree_root;
926 path = btrfs_alloc_path();
928 fprintf(stderr, "Failed to alloc path\n");
934 key.type = BTRFS_ROOT_ITEM_KEY;
936 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
938 fprintf(stderr, "Failed to do search %d\n", ret);
939 btrfs_free_path(path);
944 leaf = path->nodes[0];
945 slot = path->slots[0];
946 if (slot >= btrfs_header_nritems(leaf)) {
947 ret = btrfs_next_leaf(root, path);
950 leaf = path->nodes[0];
951 slot = path->slots[0];
953 btrfs_item_key(leaf, &disk_key, slot);
954 btrfs_disk_key_to_cpu(&found_key, &disk_key);
955 if (btrfs_key_type(&found_key) != BTRFS_ROOT_ITEM_KEY) {
960 offset = btrfs_item_ptr_offset(leaf, slot);
961 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
963 btrfs_print_key(&disk_key);
964 printf(" %Lu level %d\n", btrfs_root_bytenr(&ri),
965 btrfs_root_level(&ri));
968 btrfs_free_path(path);
973 static struct btrfs_root *open_fs(const char *dev, u64 root_location,
974 int super_mirror, int list_roots)
976 struct btrfs_fs_info *fs_info = NULL;
977 struct btrfs_root *root = NULL;
981 for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
982 bytenr = btrfs_sb_offset(i);
983 fs_info = open_ctree_fs_info(dev, bytenr, root_location,
987 fprintf(stderr, "Could not open root, trying backup super\n");
994 * All we really need to succeed is reading the chunk tree, everything
995 * else we can do by hand, since we only need to read the tree root and
998 if (!extent_buffer_uptodate(fs_info->tree_root->node)) {
1001 root = fs_info->tree_root;
1003 root_location = btrfs_super_root(fs_info->super_copy);
1004 generation = btrfs_super_generation(fs_info->super_copy);
1005 root->node = read_tree_block(root, root_location,
1006 root->leafsize, generation);
1007 if (!extent_buffer_uptodate(root->node)) {
1008 fprintf(stderr, "Error opening tree root\n");
1014 if (!list_roots && !fs_info->fs_root) {
1015 struct btrfs_key key;
1017 key.objectid = BTRFS_FS_TREE_OBJECTID;
1018 key.type = BTRFS_ROOT_ITEM_KEY;
1019 key.offset = (u64)-1;
1020 fs_info->fs_root = btrfs_read_fs_root_no_cache(fs_info, &key);
1021 if (IS_ERR(fs_info->fs_root)) {
1022 fprintf(stderr, "Couldn't read fs root: %ld\n",
1023 PTR_ERR(fs_info->fs_root));
1024 close_ctree(fs_info->tree_root);
1029 if (list_roots && do_list_roots(fs_info->tree_root)) {
1030 close_ctree(fs_info->tree_root);
1034 return fs_info->fs_root;
1037 static int find_first_dir(struct btrfs_root *root, u64 *objectid)
1039 struct btrfs_path *path;
1040 struct btrfs_key found_key;
1041 struct btrfs_key key;
1046 key.type = BTRFS_DIR_INDEX_KEY;
1049 path = btrfs_alloc_path();
1051 fprintf(stderr, "Ran out of memory\n");
1055 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1057 fprintf(stderr, "Error searching %d\n", ret);
1061 if (!path->nodes[0]) {
1062 fprintf(stderr, "No leaf!\n");
1066 for (i = path->slots[0];
1067 i < btrfs_header_nritems(path->nodes[0]); i++) {
1068 btrfs_item_key_to_cpu(path->nodes[0], &found_key, i);
1069 if (found_key.type != key.type)
1072 printf("Using objectid %Lu for first dir\n",
1073 found_key.objectid);
1074 *objectid = found_key.objectid;
1079 ret = next_leaf(root, path);
1081 fprintf(stderr, "Error getting next leaf %d\n",
1084 } else if (ret > 0) {
1085 fprintf(stderr, "No more leaves\n");
1088 } while (!path->nodes[0]);
1091 printf("Couldn't find a dir index item\n");
1093 btrfs_free_path(path);
1097 static struct option long_options[] = {
1098 { "path-regex", 1, NULL, 256},
1099 { "dry-run", 0, NULL, 'D'},
1103 const char * const cmd_restore_usage[] = {
1104 "btrfs restore [options] <device> <path> | -l <device>",
1105 "Try to restore files from a damaged filesystem (unmounted)",
1108 "-x get extended attributes",
1112 "-t <location> tree location",
1113 "-f <offset> filesystem location",
1114 "-u <block> super mirror",
1115 "-r <rootid> root objectid",
1117 "-l list tree roots",
1118 "-D|--dry-run dry run (only list files that would be recovered)",
1119 "--path-regex <regex>",
1120 " restore only filenames matching regex,",
1121 " you have to use following syntax (possibly quoted):",
1122 " ^/(|home(|/username(|/Desktop(|/.*))))$",
1126 int cmd_restore(int argc, char **argv)
1128 struct btrfs_root *root;
1129 struct btrfs_key key;
1131 u64 tree_location = 0;
1132 u64 fs_location = 0;
1133 u64 root_objectid = 0;
1137 int option_index = 0;
1138 int super_mirror = 0;
1141 const char *match_regstr = NULL;
1142 int match_cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
1143 regex_t match_reg, *mreg = NULL;
1146 while ((opt = getopt_long(argc, argv, "sxviot:u:df:r:lDc", long_options,
1147 &option_index)) != -1) {
1163 tree_location = arg_strtou64(optarg);
1166 fs_location = arg_strtou64(optarg);
1169 super_mirror = arg_strtou64(optarg);
1170 if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1171 fprintf(stderr, "Super mirror not "
1180 root_objectid = arg_strtou64(optarg);
1189 match_cflags |= REG_ICASE;
1191 /* long option without single letter alternative */
1193 match_regstr = optarg;
1199 usage(cmd_restore_usage);
1203 if (!list_roots && optind + 1 >= argc)
1204 usage(cmd_restore_usage);
1205 else if (list_roots && optind >= argc)
1206 usage(cmd_restore_usage);
1208 if ((ret = check_mounted(argv[optind])) < 0) {
1209 fprintf(stderr, "Could not check mount status: %s\n",
1213 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
1217 root = open_fs(argv[optind], tree_location, super_mirror, list_roots);
1224 if (fs_location != 0) {
1225 free_extent_buffer(root->node);
1226 root->node = read_tree_block(root, fs_location, root->leafsize, 0);
1228 fprintf(stderr, "Failed to read fs location\n");
1233 memset(path_name, 0, 4096);
1235 strncpy(dir_name, argv[optind + 1], sizeof dir_name);
1236 dir_name[sizeof dir_name - 1] = 0;
1238 /* Strip the trailing / on the dir name */
1239 len = strlen(dir_name);
1240 while (len && dir_name[--len] == '/') {
1241 dir_name[len] = '\0';
1244 if (root_objectid != 0) {
1245 struct btrfs_root *orig_root = root;
1247 key.objectid = root_objectid;
1248 key.type = BTRFS_ROOT_ITEM_KEY;
1249 key.offset = (u64)-1;
1250 root = btrfs_read_fs_root(orig_root->fs_info, &key);
1252 fprintf(stderr, "Error reading root\n");
1262 ret = find_first_dir(root, &key.objectid);
1266 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1270 ret = regcomp(&match_reg, match_regstr, match_cflags);
1272 regerror(ret, &match_reg, reg_err, sizeof(reg_err));
1273 fprintf(stderr, "Regex compile failed: %s\n", reg_err);
1280 printf("This is a dry-run, no files are going to be restored\n");
1282 ret = search_dir(root, &key, dir_name, "", mreg);