~ubuntu-branches/debian/jessie/btrfs-tools/jessie

« back to all changes in this revision

Viewing changes to cmds-restore.c

  • Committer: Package Import Robot
  • Author(s): Dimitri John Ledkov
  • Date: 2014-04-19 12:12:11 UTC
  • mfrom: (1.2.12) (6.1.37 sid)
  • Revision ID: package-import@ubuntu.com-20140419121211-mski0g757tsdv4x1
Tags: 3.14.1-1
* New upstream release.
* Switch to git-dpm.
* Rebase and cleanup patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include <regex.h>
35
35
#include <getopt.h>
36
36
#include <sys/types.h>
37
 
#include <attr/xattr.h>
 
37
#include <sys/xattr.h>
38
38
 
39
39
#include "ctree.h"
40
40
#include "disk-io.h"
53
53
static int ignore_errors = 0;
54
54
static int overwrite = 0;
55
55
static int get_xattrs = 0;
 
56
static int dry_run = 0;
56
57
 
57
58
#define LZO_LEN 4
58
59
#define PAGE_CACHE_SIZE 4096
228
229
        fi = btrfs_item_ptr(leaf, path->slots[0],
229
230
                            struct btrfs_file_extent_item);
230
231
        ptr = btrfs_file_extent_inline_start(fi);
231
 
        len = btrfs_file_extent_inline_item_len(leaf,
232
 
                                        btrfs_item_nr(path->slots[0]));
 
232
        len = btrfs_file_extent_inline_len(leaf, path->slots[0], fi);
233
233
        read_extent_buffer(leaf, buf, ptr, len);
234
234
 
235
235
        compress = btrfs_file_extent_compression(leaf, fi);
296
296
        ram_size = btrfs_file_extent_ram_bytes(leaf, fi);
297
297
        offset = btrfs_file_extent_offset(leaf, fi);
298
298
        num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
299
 
        size_left = num_bytes;
300
 
        bytenr += offset;
 
299
        size_left = disk_size;
 
300
        if (compress == BTRFS_COMPRESS_NONE)
 
301
                bytenr += offset;
301
302
 
302
303
        if (offset)
303
304
                printf("offset is %Lu\n", offset);
374
375
                goto out;
375
376
        }
376
377
 
377
 
        ret = decompress(inbuf, outbuf, num_bytes, &ram_size, compress);
 
378
        ret = decompress(inbuf, outbuf, disk_size, &ram_size, compress);
378
379
        if (ret) {
379
380
                num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
380
381
                                              bytenr, length);
387
388
                goto again;
388
389
        }
389
390
 
390
 
        while (total < ram_size) {
391
 
                done = pwrite(fd, outbuf+total, ram_size-total, pos+total);
 
391
        while (total < num_bytes) {
 
392
                done = pwrite(fd, outbuf + offset + total,
 
393
                              num_bytes - total,
 
394
                              pos + total);
392
395
                if (done < 0) {
393
396
                        ret = -1;
394
397
                        goto out;
801
804
                        }
802
805
                        if (verbose)
803
806
                                printf("Restoring %s\n", path_name);
 
807
                        if (dry_run)
 
808
                                goto next;
804
809
                        fd = open(path_name, O_CREAT|O_WRONLY, 0644);
805
810
                        if (fd < 0) {
806
811
                                fprintf(stderr, "Error creating %s: %d\n",
873
878
                                printf("Restoring %s\n", path_name);
874
879
 
875
880
                        errno = 0;
876
 
                        ret = mkdir(path_name, 0755);
 
881
                        if (dry_run)
 
882
                                ret = 0;
 
883
                        else
 
884
                                ret = mkdir(path_name, 0755);
877
885
                        if (ret && errno != EEXIST) {
878
886
                                free(dir);
879
887
                                fprintf(stderr, "Error mkdiring %s: %d\n",
1090
1098
 
1091
1099
static struct option long_options[] = {
1092
1100
        { "path-regex", 1, NULL, 256},
 
1101
        { "dry-run", 0, NULL, 'D'},
1093
1102
        { NULL, 0, NULL, 0}
1094
1103
};
1095
1104
 
1105
1114
        "-t <location>   tree location",
1106
1115
        "-f <offset>     filesystem location",
1107
1116
        "-u <block>      super mirror",
1108
 
        "-r <rootid>     root objectid",
 
1117
        "-r <rootid>     root objectid",
1109
1118
        "-d              find dir",
1110
1119
        "-l              list tree roots",
 
1120
        "-D|--dry-run    dry run (only list files that would be recovered)",
1111
1121
        "--path-regex <regex>",
1112
1122
        "                restore only filenames matching regex,",
1113
1123
        "                you have to use following syntax (possibly quoted):",
1135
1145
        regex_t match_reg, *mreg = NULL;
1136
1146
        char reg_err[256];
1137
1147
 
1138
 
        while ((opt = getopt_long(argc, argv, "sxviot:u:df:r:lc", long_options,
 
1148
        while ((opt = getopt_long(argc, argv, "sxviot:u:df:r:lDc", long_options,
1139
1149
                                        &option_index)) != -1) {
1140
1150
 
1141
1151
                switch (opt) {
1152
1162
                                overwrite = 1;
1153
1163
                                break;
1154
1164
                        case 't':
1155
 
                                errno = 0;
1156
 
                                tree_location = (u64)strtoll(optarg, NULL, 10);
1157
 
                                if (errno != 0) {
1158
 
                                        fprintf(stderr, "Tree location not valid\n");
1159
 
                                        exit(1);
1160
 
                                }
 
1165
                                tree_location = arg_strtou64(optarg);
1161
1166
                                break;
1162
1167
                        case 'f':
1163
 
                                errno = 0;
1164
 
                                fs_location = (u64)strtoll(optarg, NULL, 10);
1165
 
                                if (errno != 0) {
1166
 
                                        fprintf(stderr, "Fs location not valid\n");
1167
 
                                        exit(1);
1168
 
                                }
 
1168
                                fs_location = arg_strtou64(optarg);
1169
1169
                                break;
1170
1170
                        case 'u':
1171
 
                                errno = 0;
1172
 
                                super_mirror = (int)strtol(optarg, NULL, 10);
1173
 
                                if (errno != 0 ||
1174
 
                                    super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
 
1171
                                super_mirror = arg_strtou64(optarg);
 
1172
                                if (super_mirror >= BTRFS_SUPER_MIRROR_MAX) {
1175
1173
                                        fprintf(stderr, "Super mirror not "
1176
1174
                                                "valid\n");
1177
1175
                                        exit(1);
1181
1179
                                find_dir = 1;
1182
1180
                                break;
1183
1181
                        case 'r':
1184
 
                                errno = 0;
1185
 
                                root_objectid = (u64)strtoll(optarg, NULL, 10);
1186
 
                                if (errno != 0) {
1187
 
                                        fprintf(stderr, "Root objectid not valid\n");
1188
 
                                        exit(1);
1189
 
                                }
 
1182
                                root_objectid = arg_strtou64(optarg);
1190
1183
                                break;
1191
1184
                        case 'l':
1192
1185
                                list_roots = 1;
1193
1186
                                break;
 
1187
                        case 'D':
 
1188
                                dry_run = 1;
 
1189
                                break;
1194
1190
                        case 'c':
1195
1191
                                match_cflags |= REG_ICASE;
1196
1192
                                break;
1282
1278
                mreg = &match_reg;
1283
1279
        }
1284
1280
 
 
1281
        if (dry_run)
 
1282
                printf("This is a dry-run, no files are going to be restored\n");
 
1283
 
1285
1284
        ret = search_dir(root, &key, dir_name, "", mreg);
1286
1285
 
1287
1286
out: