Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/fiemap.rb

Defined Under Namespace

Classes: Extent

Instance Method Summary collapse

Instance Method Details

#extentsObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'ext/fiemap/fiemap.c', line 148

static VALUE
rb_file_extents(VALUE io)
{
    unsigned int i;
    VALUE temp;
    rb_io_t *fptr;
    __u64 end, start = 0;
    struct stat statinfo;
    VALUE extents = Qnil;
    struct fiemap *p_filemap = (struct fiemap *)xmalloc(offsetof(struct fiemap, fm_extents[0]) + sizeof(struct fiemap_extent) * MAX_EXTENT);

    temp = rb_io_check_io(io);
    GetOpenFile(temp, fptr);

    rb_funcall2(io, id_flush, 0, NULL);

    if (fstat(fptr->fd, &statinfo) < 0) {
	rb_sys_fail_path(fptr->pathv);
    }

    end = statinfo.st_size;
    
    while (start < end) {
        p_filemap->fm_start  = start;
        p_filemap->fm_length = end;
        p_filemap->fm_flags = 0;
        p_filemap->fm_extent_count = MAX_EXTENT;

	if (ioctl(fptr->fd, FS_IOC_FIEMAP, p_filemap) == -1) {
	    rb_sys_fail_path(fptr->pathv);
	}

	if (NIL_P(extents)) {
	    extents = rb_ary_new_capa(p_filemap->fm_mapped_extents);
	}
	else {
	    rb_ary_resize(extents, RARRAY_LEN(extents) + p_filemap->fm_mapped_extents);
	}

        for (i=0; i < p_filemap->fm_mapped_extents; i++) {
            struct fiemap_extent extent;

            extent = p_filemap->fm_extents[i];
	    rb_ary_push(extents, extent_new(cExtent, &extent));
            start = extent.fe_logical + extent.fe_length;
        }
    }

    return extents;
}