aboutsummaryrefslogtreecommitdiffstats
path: root/src/clone_blocks.c
blob: 362cc558cb6b1b0ffc9e68b20b3a42c1e4e16b6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "project.h"


typedef struct {
  ext2_filsys fs;
  ext2_ino_t i_num;
  struct ext2_inode *dst_i;
  ext2_extent_handle_t eh;

  blk64_t l_start;
  blk64_t l_len;

  int ret;

} context_t;

static int make_extents (ext2_filsys fs, blk64_t *b_num,
                         e2_blkcnt_t blockcnt,
                         blk64_t ref_block,
                         int ref_offset,
                         void *_dst)
{
  context_t *dst = (context_t *)_dst;

  if (dst->ret) return BLOCK_ABORT;

  if (!dst->l_len)
    dst->l_start = blockcnt;


  if ((dst->l_start + dst->l_len) != blockcnt) {
    dst->ret = allocate_extent (dst->fs, dst->i_num, dst->eh, dst->l_start, dst->l_len, EXT2_EXTENT_FLAGS_LEAF);

    if (dst->ret) return BLOCK_ABORT;

    dst->dst_i->i_blocks += dst->l_len * (dst->fs->blocksize / 512);

    dst->l_len = 1;
    dst->l_start = blockcnt;
  } else
    dst->l_len++;

  return 0;
}



int clone_blocks (ext2_filsys src_fs, ext2_filsys dst_fs, ext2_ino_t i_num, struct ext2_inode *src_i, struct ext2_inode *dst_i)
{
  context_t dst;
  struct ext2fs_extent e;
  int ret;


  dst.fs = dst_fs;
  dst.i_num = i_num;
  dst.dst_i = dst_i;
  dst.eh = NULL;
  dst.l_start = 0;
  dst.l_len = 0;
  dst.ret = 0;

  do {

    dst_i->i_flags  |= EXT4_EXTENTS_FL;

    EXT2_MOAN_FAIL (ret, ext2fs_extent_open2 (dst_fs, i_num, dst_i, &dst.eh));

    if (ret)
      break;


    EXT2_MOAN_FAIL (ret, ext2fs_block_iterate3 (src_fs, i_num, BLOCK_FLAG_DATA_ONLY | BLOCK_FLAG_READ_ONLY, 0, make_extents, &dst));

    if (ret) break;

    ret = dst.ret;

    if (ret) break;

    if (dst.l_len) {
      ret = allocate_extent (dst_fs, i_num, dst.eh, dst.l_start, dst.l_len, EXT2_EXTENT_FLAGS_LEAF);
      dst_i->i_blocks += dst.l_len * (dst_fs->blocksize / 512);

      if (ret) break;
    }

    EXT2_MOAN_FAIL (ret, ext2fs_write_inode (dst_fs, i_num, dst_i));

    if (ret)
      break;

    ret = ext2fs_extent_get (dst.eh, EXT2_EXTENT_ROOT, &e);

    if (ret == EXT2_ET_EXTENT_NO_NEXT)  {
      ret = 0;
      break;
    }

    if (ret) {
      EXT2_MOAN (ret, "ext2fs_extent_get (dst.eh, EXT2_EXTENT_ROOT, &e)");
      break;
    }


    for (;;) {
      uint64_t file_offset, file_len;


      if ((e.e_flags & EXT2_EXTENT_FLAGS_LEAF) && ! (e.e_flags & EXT2_EXTENT_FLAGS_UNINIT)) {

        file_offset = e.e_lblk;
        file_len = e.e_len;

        file_offset *= src_fs->blocksize;
        file_len *= src_fs->blocksize;


        if (clone_data (src_fs, dst_fs, i_num, src_i, dst_i, file_offset, file_len)) {
          ret = 1;
          break;
        }
      }

      ret = ext2fs_extent_get (dst.eh, EXT2_EXTENT_NEXT_LEAF, &e);

      if (ret == EXT2_ET_EXTENT_NO_NEXT)  {
        ret = 0;
        break;
      }

      if (ret) {
        EXT2_MOAN (ret, "ext2fs_extent_get (dst.eh, EXT2_EXTENT_NEXT_LEAF, &e)");
        break;
      }
    }

  } while (0);

  if (dst.eh)
    ext2fs_extent_free (dst.eh);


  return ret;
}