2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2011 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 struct cbox_blob
*cbox_blob_new(size_t size
)
31 struct cbox_blob
*p
= malloc(sizeof(struct cbox_blob
));
34 p
->data
= size
? malloc(size
) : NULL
;
39 struct cbox_blob
*cbox_blob_new_copy_data(const void *data
, size_t size
)
41 struct cbox_blob
*p
= cbox_blob_new(size
);
44 memcpy(p
, data
, size
);
48 struct cbox_blob
*cbox_blob_new_acquire_data(void *data
, size_t size
)
50 struct cbox_blob
*p
= malloc(sizeof(struct cbox_blob
));
58 static struct cbox_blob
*read_from_fd(const char *context_name
, const char *pathname
, int fd
, size_t size
, GError
**error
)
60 struct cbox_blob
*blob
= cbox_blob_new(size
+ 1);
63 g_set_error(error
, G_FILE_ERROR
, g_file_error_from_errno (errno
), "%s: cannot allocate memory for file '%s'", context_name
, pathname
);
66 uint8_t *data
= blob
->data
;
69 size_t chunk
= size
- nread
;
72 size_t nv
= read(fd
, data
+ nread
, chunk
);
77 g_set_error(error
, G_FILE_ERROR
, g_file_error_from_errno (errno
), "%s: cannot read '%s': %s", context_name
, pathname
, strerror(errno
));
78 cbox_blob_destroy(blob
);
82 } while(nread
< size
);
83 // Make sure that the content is 0-padded but still has the original size
84 // (without extra zero byte)
90 struct cbox_blob
*cbox_blob_new_from_file(const char *context_name
, struct cbox_tarfile
*tarfile
, const char *path
, const char *name
, size_t max_size
, GError
**error
)
92 gchar
*fullpath
= g_build_filename(path
, name
, NULL
);
93 struct cbox_blob
*blob
= NULL
;
96 struct cbox_taritem
*item
= cbox_tarfile_get_item_by_name(tarfile
, fullpath
, TRUE
);
99 int fd
= cbox_tarfile_openitem(tarfile
, item
);
102 blob
= read_from_fd(context_name
, fullpath
, fd
, item
->size
, error
);
103 cbox_tarfile_closeitem(tarfile
, item
, fd
);
109 int fd
= open(fullpath
, O_RDONLY
| O_LARGEFILE
);
112 uint64_t size
= lseek64(fd
, 0, SEEK_END
);
113 if (size
<= max_size
)
114 blob
= read_from_fd(context_name
, fullpath
, fd
, size
, error
);
116 g_set_error(error
, G_FILE_ERROR
, g_file_error_from_errno (errno
), "%s: file '%s' too large (%llu while max size is %u)", context_name
, fullpath
, (unsigned long long)size
, (unsigned)max_size
);
120 g_set_error(error
, G_FILE_ERROR
, g_file_error_from_errno (errno
), "%s: cannot open '%s': %s", context_name
, fullpath
, strerror(errno
));
126 void cbox_blob_destroy(struct cbox_blob
*blob
)