2013-11-21 02:12:59 +00:00
|
|
|
/*
|
|
|
|
Serval DNA Rhizome storage
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2013 Serval Project Inc.
|
2013-11-21 02:12:59 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2014-06-04 07:23:41 +00:00
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
#include <assert.h>
|
2014-06-23 01:38:01 +00:00
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
|
|
|
# include <sys/statvfs.h>
|
2014-06-26 02:47:36 +00:00
|
|
|
#else
|
|
|
|
# if defined(HAVE_SYS_STAT_H) && defined(HAVE_SYS_VFS_H)
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <sys/vfs.h>
|
|
|
|
# define statvfs statfs
|
|
|
|
# endif
|
2014-06-23 01:38:01 +00:00
|
|
|
#endif
|
2012-12-17 05:11:27 +00:00
|
|
|
#include "serval.h"
|
|
|
|
#include "rhizome.h"
|
|
|
|
#include "conf.h"
|
2013-01-08 06:50:42 +00:00
|
|
|
#include "strlcpy.h"
|
2012-12-17 05:11:27 +00:00
|
|
|
|
|
|
|
#define RHIZOME_BUFFER_MAXIMUM_SIZE (1024*1024)
|
|
|
|
|
2013-12-30 04:26:50 +00:00
|
|
|
uint64_t rhizome_copy_file_to_blob(int fd, uint64_t id, size_t size);
|
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
int rhizome_exists(const rhizome_filehash_t *hashp)
|
2013-09-30 06:36:23 +00:00
|
|
|
{
|
2013-12-11 00:41:34 +00:00
|
|
|
uint64_t gotfile = 0;
|
|
|
|
if (sqlite_exec_uint64(&gotfile, "SELECT COUNT(*) FROM FILES WHERE id = ? and datavalid = 1;", RHIZOME_FILEHASH_T, hashp, END) != 1)
|
2012-12-17 05:11:27 +00:00
|
|
|
return 0;
|
2014-04-07 01:40:20 +00:00
|
|
|
if (gotfile==0)
|
|
|
|
return 0;
|
|
|
|
uint64_t blob_rowid;
|
|
|
|
if (sqlite_exec_uint64(&blob_rowid,
|
|
|
|
"SELECT rowid "
|
|
|
|
"FROM FILEBLOBS "
|
|
|
|
"WHERE id = ?", RHIZOME_FILEHASH_T, hashp, END) == -1)
|
|
|
|
return 0;
|
|
|
|
if (blob_rowid !=0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// No row in FILEBLOBS, look for an external blob file.
|
|
|
|
char blob_path[1024];
|
|
|
|
if (!FORMF_RHIZOME_STORE_PATH(blob_path, "%s/%s", RHIZOME_BLOB_SUBDIR, alloca_tohex_rhizome_filehash_t(*hashp)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (stat(blob_path, &st) == -1)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2012-12-17 05:11:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 04:26:50 +00:00
|
|
|
/* Creates a row in the FILEBLOBS table and return the ROWID. Returns 0 if unsuccessful (error
|
|
|
|
* logged).
|
|
|
|
*/
|
2014-06-04 07:23:41 +00:00
|
|
|
static uint64_t rhizome_create_fileblob(sqlite_retry_state *retry, uint64_t id, size_t size)
|
2013-12-30 04:26:50 +00:00
|
|
|
{
|
|
|
|
if (sqlite_exec_void_retry(
|
|
|
|
retry,
|
|
|
|
"INSERT OR REPLACE INTO FILEBLOBS(id,data) VALUES(?,?)",
|
|
|
|
UINT64_TOSTR, id,
|
|
|
|
ZEROBLOB, (int)size,
|
|
|
|
END) == -1
|
|
|
|
) {
|
|
|
|
WHYF("Failed to create blob, size=%zu, id=%"PRIu64, size, id);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-04 07:23:41 +00:00
|
|
|
uint64_t rowid = sqlite3_last_insert_rowid(rhizome_db);
|
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("Inserted fileblob rowid=%"PRId64" for id='%"PRIu64"'", rowid, id);
|
|
|
|
return rowid;
|
2013-12-30 04:26:50 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 07:55:15 +00:00
|
|
|
static int rhizome_delete_external(const char *id)
|
|
|
|
{
|
|
|
|
// attempt to remove any external blob
|
|
|
|
char blob_path[1024];
|
|
|
|
if (!FORMF_RHIZOME_STORE_PATH(blob_path, "%s/%s", RHIZOME_BLOB_SUBDIR, id))
|
|
|
|
return -1;
|
|
|
|
if (unlink(blob_path) == -1) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
return WHYF_perror("unlink(%s)", alloca_str_toprint(blob_path));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("Deleted blob file %s", blob_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rhizome_delete_file_id_retry(sqlite_retry_state *retry, const char *id)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
rhizome_delete_external(id);
|
|
|
|
sqlite3_stmt *statement = sqlite_prepare_bind(retry, "DELETE FROM fileblobs WHERE id = ?", STATIC_TEXT, id, END);
|
|
|
|
if (!statement || sqlite_exec_retry(retry, statement) == -1)
|
|
|
|
ret = -1;
|
|
|
|
statement = sqlite_prepare_bind(retry, "DELETE FROM files WHERE id = ?", STATIC_TEXT, id, END);
|
|
|
|
if (!statement || sqlite_exec_retry(retry, statement) == -1)
|
|
|
|
ret = -1;
|
|
|
|
return ret == -1 ? -1 : sqlite3_changes(rhizome_db) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rhizome_delete_payload_retry(sqlite_retry_state *retry, const rhizome_bid_t *bidp)
|
|
|
|
{
|
|
|
|
strbuf fh = strbuf_alloca(RHIZOME_FILEHASH_STRLEN + 1);
|
|
|
|
int rows = sqlite_exec_strbuf_retry(retry, fh, "SELECT filehash FROM manifests WHERE id = ?", RHIZOME_BID_T, bidp, END);
|
|
|
|
if (rows == -1)
|
|
|
|
return -1;
|
|
|
|
if (rows && rhizome_delete_file_id_retry(retry, strbuf_str(fh)) == -1)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a bundle's payload (file) from the database, given its manifest ID, leaving its manifest
|
|
|
|
* untouched if present.
|
|
|
|
*
|
|
|
|
* Returns 0 if manifest is found, its payload is found and removed
|
|
|
|
* Returns 1 if manifest or payload is not found
|
|
|
|
* Returns -1 on error
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
int rhizome_delete_payload(const rhizome_bid_t *bidp)
|
|
|
|
{
|
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
|
|
|
return rhizome_delete_payload_retry(&retry, bidp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rhizome_delete_file_id(const char *id)
|
|
|
|
{
|
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
|
|
|
return rhizome_delete_file_id_retry(&retry, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a file from the database, given its file hash.
|
|
|
|
*
|
|
|
|
* Returns 0 if file is found and removed
|
|
|
|
* Returns 1 if file is not found
|
|
|
|
* Returns -1 on error
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
int rhizome_delete_file(const rhizome_filehash_t *hashp)
|
|
|
|
{
|
|
|
|
return rhizome_delete_file_id(alloca_tohex_rhizome_filehash_t(*hashp));
|
|
|
|
}
|
|
|
|
|
2014-06-23 01:38:01 +00:00
|
|
|
static uint64_t store_get_free_space()
|
|
|
|
{
|
|
|
|
const char *fake_space = getenv("SERVALD_FREE_SPACE");
|
|
|
|
if (fake_space)
|
|
|
|
return atol(fake_space);
|
2014-06-26 02:47:36 +00:00
|
|
|
#if defined(HAVE_SYS_STATVFS_H) || (defined(HAVE_SYS_STAT_H) && defined(HAVE_SYS_VFS_H))
|
2014-06-23 01:38:01 +00:00
|
|
|
char store_path[1024];
|
|
|
|
if (!FORMF_RHIZOME_STORE_PATH(store_path, "rhizome.db"))
|
|
|
|
return UINT64_MAX;
|
|
|
|
struct statvfs stats;
|
|
|
|
if (statvfs(store_path, &stats)==-1){
|
|
|
|
WARNF_perror("statvfs(%s)", store_path);
|
|
|
|
return UINT64_MAX;
|
|
|
|
}
|
|
|
|
return stats.f_bsize * stats.f_bfree;
|
|
|
|
#else
|
|
|
|
return UINT64_MAX;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t store_space_limit(uint64_t current_size)
|
|
|
|
{
|
|
|
|
uint64_t limit = config.rhizome.database_size;
|
|
|
|
|
|
|
|
if (config.rhizome.min_free_space!=0){
|
|
|
|
uint64_t free_space = store_get_free_space();
|
|
|
|
if (free_space < config.rhizome.min_free_space){
|
|
|
|
if (current_size + free_space < config.rhizome.min_free_space)
|
|
|
|
limit = 0;
|
|
|
|
else
|
|
|
|
limit = current_size + free_space - config.rhizome.min_free_space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:55:15 +00:00
|
|
|
// TODO readonly version?
|
2014-06-19 04:28:01 +00:00
|
|
|
static enum rhizome_payload_status store_make_space(uint64_t bytes, struct rhizome_cleanup_report *report)
|
2014-06-18 07:55:15 +00:00
|
|
|
{
|
|
|
|
uint64_t external_bytes;
|
|
|
|
uint64_t db_page_size;
|
|
|
|
uint64_t db_page_count;
|
|
|
|
uint64_t db_free_page_count;
|
|
|
|
|
2014-06-23 01:38:01 +00:00
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
2014-06-18 07:55:15 +00:00
|
|
|
|
|
|
|
// No limit?
|
2014-06-23 01:38:01 +00:00
|
|
|
if (config.rhizome.database_size==UINT64_MAX && config.rhizome.min_free_space==0)
|
2014-06-18 07:55:15 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_NEW;
|
|
|
|
|
|
|
|
// TODO index external_bytes calculation and/or cache result
|
|
|
|
|
2014-06-19 04:28:01 +00:00
|
|
|
if ( sqlite_exec_uint64_retry(&retry, &db_page_size, "PRAGMA page_size;", END) == -1LL
|
|
|
|
|| sqlite_exec_uint64_retry(&retry, &db_page_count, "PRAGMA page_count;", END) == -1LL
|
|
|
|
|| sqlite_exec_uint64_retry(&retry, &db_free_page_count, "PRAGMA freelist_count;", END) == -1LL
|
|
|
|
|| sqlite_exec_uint64_retry(&retry, &external_bytes,
|
2014-06-18 07:55:15 +00:00
|
|
|
"SELECT SUM(length) "
|
|
|
|
"FROM FILES "
|
|
|
|
"WHERE NOT EXISTS( "
|
|
|
|
"SELECT 1 "
|
|
|
|
"FROM FILEBLOBS "
|
|
|
|
"WHERE FILES.ID = FILEBLOBS.ID "
|
|
|
|
");", END) == -1LL
|
|
|
|
)
|
|
|
|
return WHY("Cannot measure database used bytes");
|
|
|
|
|
|
|
|
uint64_t db_used = external_bytes + db_page_size * (db_page_count - db_free_page_count);
|
2014-06-23 01:38:01 +00:00
|
|
|
const uint64_t limit = store_space_limit(db_used);
|
2014-06-18 07:55:15 +00:00
|
|
|
|
2014-06-23 01:38:01 +00:00
|
|
|
if (bytes && bytes >= limit){
|
2014-06-18 07:55:15 +00:00
|
|
|
if (config.debug.rhizome)
|
|
|
|
DEBUGF("Not enough space for %"PRIu64". Used; %"PRIu64" = %"PRIu64" + %"PRIu64" * (%"PRIu64" - %"PRIu64"), Limit; %"PRIu64,
|
|
|
|
bytes, db_used, external_bytes, db_page_size, db_page_count, db_free_page_count, limit);
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_TOO_BIG;
|
|
|
|
}
|
|
|
|
|
2014-06-19 04:28:01 +00:00
|
|
|
// vacuum database pages if we're already using too much free space
|
|
|
|
if (external_bytes + db_page_size * db_page_count > limit)
|
|
|
|
rhizome_vacuum_db(&retry);
|
|
|
|
|
2014-06-18 07:55:15 +00:00
|
|
|
// If there is enough space, do nothing
|
|
|
|
if (db_used + bytes <= limit)
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_NEW;
|
|
|
|
|
|
|
|
// penalise new things by 10 minutes to reduce churn
|
|
|
|
time_ms_t cost = gettime_ms() - 60000 - bytes;
|
|
|
|
|
|
|
|
// query files by age, penalise larger files so they are removed earlier
|
|
|
|
sqlite3_stmt *statement = sqlite_prepare_bind(&retry,
|
|
|
|
"SELECT id, length, inserttime FROM FILES ORDER BY (inserttime - length)",
|
|
|
|
END);
|
|
|
|
if (!statement)
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
2014-09-08 07:44:55 +00:00
|
|
|
|
|
|
|
int r=0;
|
|
|
|
while (db_used + bytes > limit && (r=sqlite_step_retry(&retry, statement)) == SQLITE_ROW) {
|
2014-06-18 07:55:15 +00:00
|
|
|
const char *id=(const char *) sqlite3_column_text(statement, 0);
|
|
|
|
uint64_t length = sqlite3_column_int(statement, 1);
|
|
|
|
time_ms_t inserttime = sqlite3_column_int64(statement, 2);
|
|
|
|
|
|
|
|
time_ms_t cost_existing = inserttime - length;
|
|
|
|
|
|
|
|
if (config.debug.rhizome)
|
|
|
|
DEBUGF("Considering dropping file %s, size %"PRId64" cost %"PRId64" vs %"PRId64" to add %"PRId64" new bytes",
|
|
|
|
id, length, cost, cost_existing, bytes);
|
|
|
|
// don't allow the new file, we've got more important things to store
|
2014-06-23 01:38:01 +00:00
|
|
|
if (bytes && cost < cost_existing)
|
2014-06-18 07:55:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// drop the existing content and recalculate used space
|
|
|
|
if (rhizome_delete_external(id)==0)
|
|
|
|
external_bytes -= length;
|
|
|
|
|
|
|
|
sqlite3_stmt *s = sqlite_prepare_bind(&retry, "DELETE FROM fileblobs WHERE id = ?", STATIC_TEXT, id, END);
|
|
|
|
if (s)
|
|
|
|
sqlite_exec_retry(&retry, s);
|
|
|
|
s = sqlite_prepare_bind(&retry, "DELETE FROM files WHERE id = ?", STATIC_TEXT, id, END);
|
|
|
|
if (s)
|
|
|
|
sqlite_exec_retry(&retry, s);
|
|
|
|
|
2014-06-19 04:28:01 +00:00
|
|
|
sqlite_exec_uint64_retry(&retry, &db_page_count, "PRAGMA page_count;", END);
|
|
|
|
sqlite_exec_uint64_retry(&retry, &db_free_page_count, "PRAGMA freelist_count;", END);
|
|
|
|
if (report)
|
|
|
|
report->deleted_expired_files++;
|
2014-06-18 07:55:15 +00:00
|
|
|
db_used = external_bytes + db_page_size * (db_page_count - db_free_page_count);
|
|
|
|
}
|
|
|
|
sqlite3_finalize(statement);
|
2014-06-19 04:28:01 +00:00
|
|
|
|
|
|
|
rhizome_vacuum_db(&retry);
|
2014-06-18 07:55:15 +00:00
|
|
|
|
|
|
|
if (db_used + bytes <= limit)
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_NEW;
|
2014-09-08 07:44:55 +00:00
|
|
|
|
|
|
|
if (sqlite_code_ok(r)){
|
|
|
|
if (config.debug.rhizome)
|
|
|
|
DEBUGF("Not enough space for %"PRIu64". Used; %"PRIu64" = %"PRIu64" + %"PRIu64" * (%"PRIu64" - %"PRIu64"), Limit; %"PRIu64,
|
|
|
|
bytes, db_used, external_bytes, db_page_size, db_page_count, db_free_page_count, limit);
|
|
|
|
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_EVICTED;
|
|
|
|
}
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
2014-06-18 07:55:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 04:28:01 +00:00
|
|
|
int rhizome_store_cleanup(struct rhizome_cleanup_report *report)
|
|
|
|
{
|
|
|
|
return store_make_space(0, report);
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:55:15 +00:00
|
|
|
enum rhizome_payload_status rhizome_open_write(struct rhizome_write *write, const rhizome_filehash_t *expectedHashp, uint64_t file_length)
|
2013-10-03 05:45:30 +00:00
|
|
|
{
|
2015-03-27 18:38:07 +00:00
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("file_length=%"PRIu64, file_length);
|
|
|
|
|
2013-12-30 04:26:50 +00:00
|
|
|
if (file_length == 0)
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_EMPTY;
|
2013-12-27 18:56:22 +00:00
|
|
|
|
2013-06-18 06:58:26 +00:00
|
|
|
write->blob_fd=-1;
|
2014-06-04 07:23:41 +00:00
|
|
|
write->sql_blob=NULL;
|
2013-07-18 07:34:12 +00:00
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
if (expectedHashp){
|
|
|
|
if (rhizome_exists(expectedHashp))
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_STORED;
|
2013-10-10 07:53:06 +00:00
|
|
|
write->id = *expectedHashp;
|
2012-12-17 05:11:27 +00:00
|
|
|
write->id_known=1;
|
|
|
|
}else{
|
|
|
|
write->id_known=0;
|
|
|
|
}
|
2014-06-18 07:55:15 +00:00
|
|
|
|
|
|
|
if (file_length!=RHIZOME_SIZE_UNSET){
|
2014-06-19 04:28:01 +00:00
|
|
|
enum rhizome_payload_status status = store_make_space(file_length, NULL);
|
2014-06-18 07:55:15 +00:00
|
|
|
if (status != RHIZOME_PAYLOAD_STATUS_NEW)
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-06-28 08:06:06 +00:00
|
|
|
static unsigned id=0;
|
|
|
|
write->temp_id = (getpid()<<16) + id++;
|
2012-12-17 05:11:27 +00:00
|
|
|
|
|
|
|
write->file_length = file_length;
|
|
|
|
write->file_offset = 0;
|
2013-07-18 07:34:12 +00:00
|
|
|
write->written_offset = 0;
|
2012-12-17 05:11:27 +00:00
|
|
|
SHA512_Init(&write->sha512_context);
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_NEW;
|
2012-12-17 05:11:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 04:22:42 +00:00
|
|
|
/* blob_open / close will lock the database, this is bad for other processes that might attempt to
|
|
|
|
* use it at the same time. However, opening a blob has about O(n^2) performance.
|
|
|
|
* */
|
|
|
|
|
|
|
|
// encrypt and hash data, data buffers must be passed in file order.
|
2014-06-28 08:06:06 +00:00
|
|
|
static int prepare_data(struct rhizome_write *write_state, uint8_t *buffer, size_t data_size)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
{
|
|
|
|
if (data_size <= 0)
|
2013-07-18 04:22:42 +00:00
|
|
|
return WHY("No content supplied");
|
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
/* Make sure we aren't being asked to write more data than we expected */
|
2013-12-30 04:26:50 +00:00
|
|
|
if ( write_state->file_length != RHIZOME_SIZE_UNSET
|
|
|
|
&& write_state->file_offset + data_size > write_state->file_length
|
|
|
|
)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
return WHYF("Too much content supplied, %"PRIu64" + %zu > %"PRIu64,
|
2013-07-18 04:22:42 +00:00
|
|
|
write_state->file_offset, data_size, write_state->file_length);
|
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
if (write_state->crypt){
|
2013-07-22 05:34:02 +00:00
|
|
|
if (rhizome_crypt_xor_block(
|
|
|
|
buffer, data_size,
|
|
|
|
write_state->file_offset + write_state->tail,
|
|
|
|
write_state->key, write_state->nonce))
|
2013-07-18 04:22:42 +00:00
|
|
|
return -1;
|
2012-12-21 00:23:47 +00:00
|
|
|
}
|
2012-12-18 00:21:12 +00:00
|
|
|
|
2013-07-18 04:22:42 +00:00
|
|
|
SHA512_Update(&write_state->sha512_context, buffer, data_size);
|
|
|
|
write_state->file_offset+=data_size;
|
2013-07-18 07:34:12 +00:00
|
|
|
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
DEBUGF("Processed %"PRIu64" of %"PRIu64, write_state->file_offset, write_state->file_length);
|
2013-07-18 04:22:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open database locks
|
2013-12-30 04:26:50 +00:00
|
|
|
static int write_get_lock(struct rhizome_write *write_state)
|
|
|
|
{
|
2014-06-04 07:23:41 +00:00
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
if (write_state->blob_fd != -1 || write_state->sql_blob)
|
2013-07-18 04:22:42 +00:00
|
|
|
return 0;
|
2014-06-04 07:23:41 +00:00
|
|
|
|
|
|
|
if (write_state->file_length == RHIZOME_SIZE_UNSET ||
|
|
|
|
write_state->file_length > config.rhizome.max_blob_size){
|
|
|
|
char blob_path[1024];
|
|
|
|
if (!FORMF_RHIZOME_STORE_PATH(blob_path, "%s/%"PRIu64, RHIZOME_BLOB_SUBDIR, write_state->temp_id))
|
|
|
|
return -1;
|
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("Attempting to put blob for id='%"PRIu64"' in %s", write_state->temp_id, blob_path);
|
|
|
|
if ((write_state->blob_fd = open(blob_path, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) {
|
|
|
|
WHYF("Failed to create payload file, id='%"PRIu64"'", write_state->temp_id);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("Writing to new blob file %s (fd=%d)", blob_path, write_state->blob_fd);
|
|
|
|
}else{
|
|
|
|
// use an explicit transaction so we can delay I/O failures until COMMIT so they can be retried.
|
|
|
|
if (sqlite_exec_void_retry(&retry, "BEGIN TRANSACTION;", END) == -1)
|
|
|
|
return -1;
|
|
|
|
if (write_state->blob_rowid == 0){
|
|
|
|
write_state->blob_rowid = rhizome_create_fileblob(&retry, write_state->temp_id, write_state->file_length);
|
|
|
|
if (write_state->blob_rowid == 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (sqlite_blob_open_retry(&retry, "main", "FILEBLOBS", "data", write_state->blob_rowid, 1 /* read/write */, &write_state->sql_blob) == -1)
|
|
|
|
goto fail;
|
|
|
|
}
|
2013-12-30 04:26:50 +00:00
|
|
|
return 0;
|
2014-06-04 07:23:41 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
sqlite_exec_void_retry(&retry, "ROLLBACK;", END);
|
|
|
|
return -1;
|
2013-07-18 04:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write data to disk
|
2014-06-28 08:06:06 +00:00
|
|
|
static int write_data(struct rhizome_write *write_state, uint64_t file_offset, uint8_t *buffer, size_t data_size)
|
2013-10-17 12:47:41 +00:00
|
|
|
{
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
DEBUGF("write_state->file_length=%"PRIu64" file_offset=%"PRIu64, write_state->file_length, file_offset);
|
|
|
|
|
2013-07-18 04:22:42 +00:00
|
|
|
if (data_size<=0)
|
|
|
|
return 0;
|
2013-07-18 07:34:12 +00:00
|
|
|
|
|
|
|
if (file_offset != write_state->written_offset)
|
|
|
|
WARNF("Writing file data out of order! [%"PRId64",%"PRId64"]", file_offset, write_state->written_offset);
|
2013-07-18 04:22:42 +00:00
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
if (write_state->blob_fd != -1) {
|
2013-12-10 06:51:35 +00:00
|
|
|
size_t ofs = 0;
|
2013-02-20 03:59:08 +00:00
|
|
|
// keep trying until all of the data is written.
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (lseek64(write_state->blob_fd, (off64_t) file_offset, SEEK_SET) == -1)
|
|
|
|
return WHYF_perror("lseek64(%d,%"PRIu64",SEEK_SET)", write_state->blob_fd, file_offset);
|
2013-12-10 06:51:35 +00:00
|
|
|
while (ofs < data_size){
|
|
|
|
ssize_t r = write(write_state->blob_fd, buffer + ofs, (size_t)(data_size - ofs));
|
|
|
|
if (r == -1)
|
2013-07-18 04:22:42 +00:00
|
|
|
return WHY_perror("write");
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-10 06:51:35 +00:00
|
|
|
DEBUGF("Wrote %zd bytes to fd %d", (size_t)r, write_state->blob_fd);
|
|
|
|
ofs += (size_t)r;
|
2012-12-27 04:45:23 +00:00
|
|
|
}
|
2013-02-20 03:59:08 +00:00
|
|
|
}else{
|
2013-07-18 04:22:42 +00:00
|
|
|
if (!write_state->sql_blob)
|
|
|
|
return WHY("Must call write_get_lock() before write_data()");
|
2013-02-20 03:59:08 +00:00
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
2013-12-30 04:26:50 +00:00
|
|
|
if (sqlite_blob_write_retry(&retry, write_state->sql_blob, buffer, data_size, file_offset) == -1)
|
|
|
|
return -1;
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
2013-07-18 07:34:12 +00:00
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
write_state->written_offset = file_offset + data_size;
|
2013-07-18 07:34:12 +00:00
|
|
|
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
DEBUGF("Wrote %"PRIu64" of %"PRIu64, file_offset + data_size, write_state->file_length);
|
2013-07-18 04:22:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close database locks
|
2013-12-27 18:56:22 +00:00
|
|
|
static int write_release_lock(struct rhizome_write *write_state)
|
|
|
|
{
|
2013-08-02 04:32:56 +00:00
|
|
|
int ret=0;
|
|
|
|
if (write_state->sql_blob){
|
2013-12-30 04:26:50 +00:00
|
|
|
ret = sqlite_blob_close(write_state->sql_blob);
|
2013-08-02 04:32:56 +00:00
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
2013-10-03 05:45:30 +00:00
|
|
|
if (sqlite_exec_void_retry(&retry, "COMMIT;", END) == -1)
|
2013-08-02 04:32:56 +00:00
|
|
|
ret=-1;
|
2014-06-04 07:23:41 +00:00
|
|
|
write_state->sql_blob=NULL;
|
2013-08-02 04:32:56 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2013-07-18 04:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write data buffers in any order, the data will be cached and streamed into the database in file order.
|
|
|
|
// Though there is an upper bound on the amount of cached data
|
2014-06-28 08:06:06 +00:00
|
|
|
int rhizome_random_write(struct rhizome_write *write_state, uint64_t offset, uint8_t *buffer, size_t data_size)
|
2013-10-17 12:47:41 +00:00
|
|
|
{
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
DEBUGF("write_state->file_length=%"PRIu64" offset=%"PRIu64, write_state->file_length, offset);
|
2013-12-30 04:26:50 +00:00
|
|
|
if ( write_state->file_length != RHIZOME_SIZE_UNSET
|
2014-06-28 08:06:06 +00:00
|
|
|
&& offset >= write_state->file_length)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( write_state->file_length != RHIZOME_SIZE_UNSET
|
|
|
|
&& offset + data_size > write_state->file_length)
|
2013-07-17 06:17:35 +00:00
|
|
|
data_size = write_state->file_length - offset;
|
2013-07-18 07:34:12 +00:00
|
|
|
|
|
|
|
struct rhizome_write_buffer **ptr = &write_state->buffer_list;
|
|
|
|
int ret=0;
|
|
|
|
int should_write = 0;
|
2014-06-04 07:23:41 +00:00
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
// if we are writing to a file, or already have the sql blob open, or are finishing, write as much
|
|
|
|
// as we can.
|
2014-06-04 07:23:41 +00:00
|
|
|
if (write_state->blob_fd != -1 ||
|
|
|
|
write_state->sql_blob ||
|
|
|
|
buffer == NULL ||
|
|
|
|
write_state->file_length > config.rhizome.max_blob_size ||
|
|
|
|
write_state->file_offset > config.rhizome.max_blob_size) {
|
2013-07-18 07:34:12 +00:00
|
|
|
should_write = 1;
|
2014-06-04 07:23:41 +00:00
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("Attempting to write (fd=%d, blob=%p, buffer=%p, len=%"PRId64", offset=%"PRId64")",
|
|
|
|
write_state->blob_fd, write_state->sql_blob, buffer,
|
|
|
|
write_state->file_length, write_state->file_offset);
|
|
|
|
} else {
|
2013-08-26 06:07:14 +00:00
|
|
|
// cache up to RHIZOME_BUFFER_MAXIMUM_SIZE or file length before attempting to write everything in one go.
|
|
|
|
// (Not perfect if the range overlaps)
|
2013-10-17 12:47:41 +00:00
|
|
|
uint64_t new_size = write_state->written_offset + write_state->buffer_size + data_size;
|
2013-12-30 04:26:50 +00:00
|
|
|
if ( (write_state->file_length != RHIZOME_SIZE_UNSET && new_size >= write_state->file_length)
|
|
|
|
|| new_size >= RHIZOME_BUFFER_MAXIMUM_SIZE
|
|
|
|
)
|
2013-07-18 07:34:12 +00:00
|
|
|
should_write = 1;
|
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
uint64_t last_offset = write_state->written_offset;
|
2013-07-18 07:34:12 +00:00
|
|
|
|
2013-07-17 06:17:35 +00:00
|
|
|
while(1){
|
2013-07-18 07:34:12 +00:00
|
|
|
|
|
|
|
// can we process this existing data block now?
|
2013-07-17 06:17:35 +00:00
|
|
|
if (*ptr && (*ptr)->offset == write_state->file_offset){
|
2013-07-18 07:34:12 +00:00
|
|
|
if (prepare_data(write_state, (*ptr)->data, (*ptr)->data_size)){
|
|
|
|
ret=-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if existing data should be written, do so now
|
|
|
|
if (should_write && *ptr && (*ptr)->offset == write_state->written_offset){
|
2013-07-17 06:17:35 +00:00
|
|
|
struct rhizome_write_buffer *n=*ptr;
|
2013-07-18 07:34:12 +00:00
|
|
|
if (write_get_lock(write_state)){
|
|
|
|
ret=-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (write_data(write_state, n->offset, n->data, n->data_size)){
|
|
|
|
ret=-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
write_state->buffer_size-=n->data_size;
|
|
|
|
last_offset = n->offset + n->data_size;
|
2013-07-17 06:17:35 +00:00
|
|
|
*ptr=n->_next;
|
|
|
|
free(n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:34:12 +00:00
|
|
|
// skip over incoming data that we've already received
|
2013-07-17 06:17:35 +00:00
|
|
|
if (offset < last_offset){
|
2014-01-06 02:06:00 +00:00
|
|
|
uint64_t delta = last_offset - offset;
|
2013-07-17 06:17:35 +00:00
|
|
|
if (delta >= data_size)
|
2013-07-18 07:34:12 +00:00
|
|
|
break;
|
2013-07-17 06:17:35 +00:00
|
|
|
data_size -= delta;
|
|
|
|
offset+=delta;
|
|
|
|
buffer+=delta;
|
|
|
|
}
|
|
|
|
|
2014-06-28 08:06:06 +00:00
|
|
|
// no new data? we can just stop now.
|
2013-07-17 06:17:35 +00:00
|
|
|
if (data_size<=0)
|
2013-07-18 07:34:12 +00:00
|
|
|
break;
|
|
|
|
|
2013-07-17 06:17:35 +00:00
|
|
|
if (!*ptr || offset < (*ptr)->offset){
|
|
|
|
// found the insert position in the list
|
2013-10-17 12:47:41 +00:00
|
|
|
size_t size = data_size;
|
2013-07-17 06:17:35 +00:00
|
|
|
|
|
|
|
// allow for buffers to overlap, we may need to split the incoming buffer into multiple pieces.
|
|
|
|
if (*ptr && offset+size > (*ptr)->offset)
|
|
|
|
size = (*ptr)->offset - offset;
|
|
|
|
|
2014-06-28 08:06:06 +00:00
|
|
|
// should we process the incoming data block now?
|
|
|
|
if (offset == write_state->file_offset){
|
|
|
|
if (prepare_data(write_state, buffer, size)){
|
|
|
|
ret=-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-26 06:07:14 +00:00
|
|
|
if (should_write && offset == write_state->written_offset){
|
2013-07-18 07:34:12 +00:00
|
|
|
if (write_get_lock(write_state)){
|
|
|
|
ret=-1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (write_data(write_state, offset, buffer, size)){
|
|
|
|
ret=-1;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-17 06:17:35 +00:00
|
|
|
// we need to go around the loop again to re-test if this buffer can now be written
|
|
|
|
}else{
|
|
|
|
// impose a limit on the total amount of cached data
|
2013-07-18 07:34:12 +00:00
|
|
|
if (write_state->buffer_size + size > RHIZOME_BUFFER_MAXIMUM_SIZE)
|
|
|
|
size = RHIZOME_BUFFER_MAXIMUM_SIZE - write_state->buffer_size;
|
2013-07-17 06:17:35 +00:00
|
|
|
if (size<=0)
|
2013-07-18 07:34:12 +00:00
|
|
|
break;
|
2013-07-17 06:17:35 +00:00
|
|
|
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-10-17 12:47:41 +00:00
|
|
|
DEBUGF("Caching block @%"PRId64", %zu", offset, size);
|
2013-07-17 06:17:35 +00:00
|
|
|
struct rhizome_write_buffer *i = emalloc(size + sizeof(struct rhizome_write_buffer));
|
2013-07-18 07:34:12 +00:00
|
|
|
if (!i){
|
|
|
|
ret=-1;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-17 06:17:35 +00:00
|
|
|
i->offset = offset;
|
|
|
|
i->buffer_size = i->data_size = size;
|
|
|
|
bcopy(buffer, i->data, size);
|
|
|
|
i->_next = *ptr;
|
2013-07-18 07:34:12 +00:00
|
|
|
write_state->buffer_size += size;
|
2013-07-17 06:17:35 +00:00
|
|
|
*ptr = i;
|
|
|
|
// if there's any overlap of this buffer and the current one, we may need to add another buffer.
|
|
|
|
ptr = &((*ptr)->_next);
|
|
|
|
}
|
|
|
|
data_size -= size;
|
|
|
|
offset+=size;
|
|
|
|
buffer+=size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_offset = (*ptr)->offset + (*ptr)->data_size;
|
|
|
|
ptr = &((*ptr)->_next);
|
|
|
|
}
|
2013-08-02 04:32:56 +00:00
|
|
|
if (write_release_lock(write_state))
|
|
|
|
ret=-1;
|
2013-07-18 07:34:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-28 08:06:06 +00:00
|
|
|
int rhizome_write_buffer(struct rhizome_write *write_state, uint8_t *buffer, size_t data_size)
|
2013-10-17 12:47:41 +00:00
|
|
|
{
|
2013-07-18 07:34:12 +00:00
|
|
|
return rhizome_random_write(write_state, write_state->file_offset, buffer, data_size);
|
2013-07-05 06:54:50 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 05:25:32 +00:00
|
|
|
/* If file_length is known, then expects file to be at least file_length in size, ignoring anything
|
|
|
|
* longer than that. Returns 0 if successful, -1 if error (logged).
|
|
|
|
*/
|
2013-12-27 18:56:22 +00:00
|
|
|
int rhizome_write_file(struct rhizome_write *write, const char *filename)
|
|
|
|
{
|
2013-12-30 05:25:32 +00:00
|
|
|
int fd = open(filename, O_RDONLY);
|
|
|
|
if (fd == -1)
|
|
|
|
return WHYF_perror("open(%s,O_RDONLY)", alloca_str_toprint(filename));
|
2013-07-17 06:17:35 +00:00
|
|
|
unsigned char buffer[RHIZOME_CRYPT_PAGE_SIZE];
|
2014-06-04 07:23:41 +00:00
|
|
|
int ret=0;
|
|
|
|
while (write->file_length == RHIZOME_SIZE_UNSET || write->file_offset < write->file_length) {
|
|
|
|
size_t size = sizeof buffer;
|
|
|
|
if (write->file_length != RHIZOME_SIZE_UNSET && write->file_offset + size > write->file_length)
|
|
|
|
size = write->file_length - write->file_offset;
|
|
|
|
ssize_t r = read(fd, buffer, size);
|
|
|
|
if (r == -1) {
|
|
|
|
ret = WHYF_perror("read(%d,%p,%zu)", fd, buffer, size);
|
|
|
|
break;
|
2012-12-18 00:21:12 +00:00
|
|
|
}
|
2014-06-04 07:23:41 +00:00
|
|
|
if (write->file_length != RHIZOME_SIZE_UNSET && (size_t) r != size) {
|
|
|
|
ret = WHYF("file truncated - read(%d,%p,%zu) returned %zu", fd, buffer, size, (size_t) r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r && rhizome_write_buffer(write, buffer, (size_t) r)) {
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((size_t) r != size)
|
|
|
|
break;
|
2012-12-18 00:21:12 +00:00
|
|
|
}
|
2013-08-02 04:32:56 +00:00
|
|
|
if (write_release_lock(write))
|
2013-12-30 05:25:32 +00:00
|
|
|
ret = -1;
|
|
|
|
close(fd);
|
2013-07-17 06:17:35 +00:00
|
|
|
return ret;
|
2012-12-18 00:21:12 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
void rhizome_fail_write(struct rhizome_write *write)
|
2013-09-30 06:36:23 +00:00
|
|
|
{
|
2013-10-16 07:55:58 +00:00
|
|
|
if (write->blob_fd != -1){
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-06-18 06:58:26 +00:00
|
|
|
DEBUGF("Closing and removing fd %d", write->blob_fd);
|
2013-02-20 03:59:08 +00:00
|
|
|
close(write->blob_fd);
|
2013-06-18 06:58:26 +00:00
|
|
|
write->blob_fd=-1;
|
2014-06-04 07:23:41 +00:00
|
|
|
char blob_path[1024];
|
|
|
|
if (FORMF_RHIZOME_STORE_PATH(blob_path, "%s/%"PRIu64, RHIZOME_BLOB_SUBDIR, write->temp_id)){
|
|
|
|
unlink(blob_path);
|
|
|
|
}
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
2013-07-18 04:22:42 +00:00
|
|
|
write_release_lock(write);
|
2014-06-04 07:23:41 +00:00
|
|
|
if (write->blob_rowid){
|
|
|
|
sqlite_exec_void_loglevel(LOG_LEVEL_WARN, "DELETE FROM FILEBLOBS WHERE rowid = ?;",
|
|
|
|
INT64, write->blob_rowid, END);
|
|
|
|
}
|
2013-07-18 07:34:12 +00:00
|
|
|
while(write->buffer_list){
|
|
|
|
struct rhizome_write_buffer *n=write->buffer_list;
|
|
|
|
write->buffer_list=n->_next;
|
|
|
|
free(n);
|
|
|
|
}
|
2012-12-17 05:11:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_finish_write(struct rhizome_write *write)
|
2013-09-30 06:36:23 +00:00
|
|
|
{
|
2015-03-27 18:38:07 +00:00
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("blob_fd=%d file_offset=%"PRIu64"", write->blob_fd, write->file_offset);
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = RHIZOME_PAYLOAD_STATUS_NEW;
|
2014-06-04 07:23:41 +00:00
|
|
|
|
2014-07-10 11:49:42 +00:00
|
|
|
// Once the whole file has been processed, we should finally know its length
|
2014-06-04 07:23:41 +00:00
|
|
|
if (write->file_length == RHIZOME_SIZE_UNSET) {
|
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("Wrote %"PRIu64" bytes, set file_length", write->file_offset);
|
|
|
|
write->file_length = write->file_offset;
|
2014-07-10 11:49:42 +00:00
|
|
|
if (write->file_length == 0)
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_EMPTY;
|
|
|
|
else {
|
|
|
|
status = store_make_space(write->file_length, NULL);
|
|
|
|
if (status != RHIZOME_PAYLOAD_STATUS_NEW)
|
|
|
|
goto failure;
|
|
|
|
}
|
2014-06-04 07:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// flush out any remaining buffered pieces to disk
|
2013-07-18 07:34:12 +00:00
|
|
|
if (write->buffer_list){
|
2013-12-27 18:56:22 +00:00
|
|
|
if (rhizome_random_write(write, 0, NULL, 0)) {
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-07-18 07:34:12 +00:00
|
|
|
goto failure;
|
2013-12-27 18:56:22 +00:00
|
|
|
}
|
|
|
|
if (write->buffer_list) {
|
2013-07-18 07:34:12 +00:00
|
|
|
WHYF("Buffer was not cleared");
|
2013-12-27 18:56:22 +00:00
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-07-18 07:34:12 +00:00
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
}
|
2014-06-04 07:23:41 +00:00
|
|
|
|
|
|
|
if (write->file_offset < write->file_length) {
|
|
|
|
WHYF("Only wrote %"PRIu64" bytes, expected %"PRIu64, write->file_offset, write->file_length);
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_WRONG_SIZE;
|
|
|
|
goto failure;
|
|
|
|
}
|
2014-06-28 08:06:06 +00:00
|
|
|
assert(write->file_offset == write->file_length);
|
2014-06-04 07:23:41 +00:00
|
|
|
|
2014-07-10 11:49:42 +00:00
|
|
|
if (write->file_length == 0) {
|
2014-06-04 07:23:41 +00:00
|
|
|
// whoops, no payload, don't store anything
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2014-06-04 07:23:41 +00:00
|
|
|
DEBUGF("Ignoring empty write");
|
|
|
|
goto failure;
|
2013-12-30 04:26:50 +00:00
|
|
|
}
|
2014-06-04 07:23:41 +00:00
|
|
|
|
2013-12-30 04:26:50 +00:00
|
|
|
rhizome_filehash_t hash_out;
|
|
|
|
SHA512_Final(hash_out.binary, &write->sha512_context);
|
|
|
|
SHA512_End(&write->sha512_context, NULL);
|
|
|
|
|
2014-06-04 07:23:41 +00:00
|
|
|
if (write->id_known) {
|
|
|
|
if (cmp_rhizome_filehash_t(&write->id, &hash_out) != 0) {
|
|
|
|
WARNF("expected filehash=%s, got %s", alloca_tohex_rhizome_filehash_t(write->id), alloca_tohex_rhizome_filehash_t(hash_out));
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_WRONG_HASH;
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
write->id = hash_out;
|
|
|
|
|
2013-12-30 04:26:50 +00:00
|
|
|
char blob_path[1024];
|
2014-03-26 05:05:43 +00:00
|
|
|
if (!FORMF_RHIZOME_STORE_PATH(blob_path, "%s/%"PRIu64, RHIZOME_BLOB_SUBDIR, write->temp_id)) {
|
2013-12-30 04:26:50 +00:00
|
|
|
WHYF("Failed to generate external blob path");
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-12-27 18:56:22 +00:00
|
|
|
goto failure;
|
2013-07-18 07:34:12 +00:00
|
|
|
}
|
2013-12-30 04:26:50 +00:00
|
|
|
// If the payload was written into an external blob (file) but is small enough to fit into a
|
|
|
|
// SQLite blob, then copy it into a proper blob (this occurs if rhizome_open_write() was called
|
2014-06-04 07:23:41 +00:00
|
|
|
// with file_length == RHIZOME_SIZE_UNSET) and max_blob_size > RHIZOME_BUFFER_MAXIMUM_SIZE.
|
2013-12-27 18:56:22 +00:00
|
|
|
int external = 0;
|
2013-12-30 04:26:50 +00:00
|
|
|
if (write->blob_fd != -1) {
|
2013-12-27 18:56:22 +00:00
|
|
|
external = 1;
|
2013-12-30 04:26:50 +00:00
|
|
|
if (write->file_length <= config.rhizome.max_blob_size) {
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-30 04:26:50 +00:00
|
|
|
DEBUGF("Copying %zu bytes from external file %s into blob, id=%"PRIu64, (size_t)write->file_offset, blob_path, write->temp_id);
|
|
|
|
int ret = 0;
|
|
|
|
if (lseek(write->blob_fd, 0, SEEK_SET) == (off_t) -1)
|
|
|
|
ret = WHYF_perror("lseek(%d,0,SEEK_SET)", write->blob_fd);
|
|
|
|
else if ((write->blob_rowid = rhizome_copy_file_to_blob(write->blob_fd, write->temp_id, (size_t)write->file_length)) == 0)
|
|
|
|
ret = -1;
|
|
|
|
if (ret == -1) {
|
|
|
|
WHY("Failed to copy external file into blob; keeping external file");
|
|
|
|
} else {
|
|
|
|
external = 0;
|
|
|
|
if (unlink(blob_path) == -1)
|
|
|
|
WARNF_perror("unlink(%s)", alloca_str_toprint(blob_path));
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-27 18:56:22 +00:00
|
|
|
DEBUGF("Closing fd=%d", write->blob_fd);
|
|
|
|
close(write->blob_fd);
|
|
|
|
write->blob_fd = -1;
|
2013-06-18 06:58:26 +00:00
|
|
|
}
|
2013-12-27 18:56:22 +00:00
|
|
|
if (write_release_lock(write)) {
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-08-02 04:32:56 +00:00
|
|
|
goto failure;
|
2013-12-27 18:56:22 +00:00
|
|
|
}
|
2013-09-30 07:02:08 +00:00
|
|
|
|
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
2014-06-18 07:55:15 +00:00
|
|
|
|
2013-10-10 07:53:06 +00:00
|
|
|
if (rhizome_exists(&write->id)) {
|
2013-09-30 07:02:08 +00:00
|
|
|
// we've already got that payload, delete the new copy
|
2014-06-04 07:23:41 +00:00
|
|
|
if (write->blob_rowid){
|
|
|
|
sqlite_exec_void_retry_loglevel(LOG_LEVEL_WARN, &retry, "DELETE FROM FILEBLOBS WHERE rowid = ?;",
|
|
|
|
INT64, write->blob_rowid, END);
|
|
|
|
}
|
|
|
|
if (external){
|
|
|
|
if (unlink(blob_path) == -1)
|
|
|
|
WARNF_perror("unlink(%s)", alloca_str_toprint(blob_path));
|
|
|
|
}
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-30 04:26:50 +00:00
|
|
|
DEBUGF("Payload id=%s already present, removed id='%"PRIu64"'", alloca_tohex_rhizome_filehash_t(write->id), write->temp_id);
|
2015-03-27 18:38:07 +00:00
|
|
|
status = RHIZOME_PAYLOAD_STATUS_STORED;
|
2014-06-04 07:23:41 +00:00
|
|
|
}else{
|
2013-10-03 05:45:30 +00:00
|
|
|
if (sqlite_exec_void_retry(&retry, "BEGIN TRANSACTION;", END) == -1)
|
2013-08-27 06:45:51 +00:00
|
|
|
goto dbfailure;
|
2013-12-30 04:26:50 +00:00
|
|
|
|
2014-07-04 04:55:28 +00:00
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
|
2013-10-03 05:45:30 +00:00
|
|
|
if (sqlite_exec_void_retry(
|
|
|
|
&retry,
|
2014-07-04 04:55:28 +00:00
|
|
|
"INSERT OR REPLACE INTO FILES(id,length,datavalid,inserttime,last_verified) VALUES(?,?,1,?,?);",
|
2013-10-10 07:53:06 +00:00
|
|
|
RHIZOME_FILEHASH_T, &write->id,
|
2013-12-30 04:26:50 +00:00
|
|
|
INT64, write->file_length,
|
2014-07-04 04:55:28 +00:00
|
|
|
INT64, now,
|
|
|
|
INT64, now,
|
2013-10-03 05:45:30 +00:00
|
|
|
END
|
|
|
|
) == -1
|
|
|
|
)
|
2013-08-27 06:45:51 +00:00
|
|
|
goto dbfailure;
|
2013-12-30 04:26:50 +00:00
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
if (external) {
|
2013-08-27 06:45:51 +00:00
|
|
|
char dest_path[1024];
|
2014-03-26 05:05:43 +00:00
|
|
|
if (!FORMF_RHIZOME_STORE_PATH(dest_path, "%s/%s", RHIZOME_BLOB_SUBDIR, alloca_tohex_rhizome_filehash_t(write->id)))
|
2013-08-27 06:45:51 +00:00
|
|
|
goto dbfailure;
|
2013-12-18 07:10:24 +00:00
|
|
|
if (rename(blob_path, dest_path) == -1) {
|
2013-09-25 06:21:54 +00:00
|
|
|
WHYF_perror("rename(%s, %s)", blob_path, dest_path);
|
2013-08-27 06:45:51 +00:00
|
|
|
goto dbfailure;
|
|
|
|
}
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-18 07:10:24 +00:00
|
|
|
DEBUGF("Renamed %s to %s", blob_path, dest_path);
|
2013-08-27 06:45:51 +00:00
|
|
|
}else{
|
2013-10-03 05:45:30 +00:00
|
|
|
if (sqlite_exec_void_retry(
|
|
|
|
&retry,
|
|
|
|
"UPDATE FILEBLOBS SET id = ? WHERE rowid = ?",
|
2013-10-10 07:53:06 +00:00
|
|
|
RHIZOME_FILEHASH_T, &write->id,
|
2013-10-03 05:45:30 +00:00
|
|
|
INT64, write->blob_rowid,
|
|
|
|
END
|
|
|
|
) == -1
|
|
|
|
)
|
|
|
|
goto dbfailure;
|
2012-12-17 05:11:27 +00:00
|
|
|
}
|
2013-10-03 05:45:30 +00:00
|
|
|
if (sqlite_exec_void_retry(&retry, "COMMIT;", END) == -1)
|
2013-08-27 06:45:51 +00:00
|
|
|
goto dbfailure;
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-10-10 07:53:06 +00:00
|
|
|
DEBUGF("Stored file %s", alloca_tohex_rhizome_filehash_t(write->id));
|
2012-12-17 05:11:27 +00:00
|
|
|
}
|
2013-12-11 00:41:34 +00:00
|
|
|
write->blob_rowid = 0;
|
2013-12-27 18:56:22 +00:00
|
|
|
return status;
|
2013-12-30 04:26:50 +00:00
|
|
|
|
2013-08-20 23:51:40 +00:00
|
|
|
dbfailure:
|
2013-10-03 05:45:30 +00:00
|
|
|
sqlite_exec_void_retry(&retry, "ROLLBACK;", END);
|
2013-12-27 18:56:22 +00:00
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-08-20 23:51:40 +00:00
|
|
|
failure:
|
2012-12-17 05:11:27 +00:00
|
|
|
rhizome_fail_write(write);
|
2013-12-27 18:56:22 +00:00
|
|
|
return status;
|
2012-12-17 05:11:27 +00:00
|
|
|
}
|
2012-12-19 05:46:49 +00:00
|
|
|
|
2013-12-19 08:43:39 +00:00
|
|
|
/* Import the payload for an existing manifest with a known file size and hash. Compute the hash of
|
|
|
|
* the payload as it is imported, and when finished, check if the size and hash match the manifest.
|
|
|
|
* If the import is successful and the size and hash match, return 0. If the size or hash do not
|
|
|
|
* match, return 1. If there is an error reading the payload file or writing to the database,
|
|
|
|
* return -1.
|
|
|
|
*/
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_import_payload_from_file(rhizome_manifest *m, const char *filepath)
|
2012-12-19 05:46:49 +00:00
|
|
|
{
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
|
|
|
if (m->filesize == 0)
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_EMPTY;
|
2012-12-19 05:46:49 +00:00
|
|
|
|
|
|
|
/* Import the file first, checking the hash as we go */
|
|
|
|
struct rhizome_write write;
|
|
|
|
bzero(&write, sizeof(write));
|
|
|
|
|
2014-06-18 07:55:15 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_write(&write, &m->filehash, m->filesize);
|
2013-12-27 18:56:22 +00:00
|
|
|
if (status != RHIZOME_PAYLOAD_STATUS_NEW)
|
|
|
|
return status;
|
2012-12-19 05:46:49 +00:00
|
|
|
|
|
|
|
// file payload is not in the store yet
|
|
|
|
if (rhizome_write_file(&write, filepath)){
|
|
|
|
rhizome_fail_write(&write);
|
2014-04-03 00:06:38 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
2012-12-19 05:46:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 08:43:39 +00:00
|
|
|
return rhizome_finish_write(&write);
|
2012-12-19 05:46:49 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 23:51:40 +00:00
|
|
|
// store a whole payload from a single buffer
|
2014-06-28 08:06:06 +00:00
|
|
|
enum rhizome_payload_status rhizome_import_buffer(rhizome_manifest *m, uint8_t *buffer, size_t length)
|
2013-08-20 23:51:40 +00:00
|
|
|
{
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
|
|
|
if (m->filesize == 0)
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_EMPTY;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
if (length != m->filesize) {
|
|
|
|
WHYF("Expected %"PRIu64" bytes, got %zu", m->filesize, length);
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_WRONG_SIZE;
|
|
|
|
}
|
2013-08-20 23:51:40 +00:00
|
|
|
|
|
|
|
/* Import the file first, checking the hash as we go */
|
|
|
|
struct rhizome_write write;
|
|
|
|
bzero(&write, sizeof(write));
|
|
|
|
|
2014-06-18 07:55:15 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_write(&write, &m->filehash, m->filesize);
|
2013-12-27 18:56:22 +00:00
|
|
|
if (status != RHIZOME_PAYLOAD_STATUS_NEW)
|
|
|
|
return status;
|
2013-08-20 23:51:40 +00:00
|
|
|
|
|
|
|
// file payload is not in the store yet
|
|
|
|
if (rhizome_write_buffer(&write, buffer, length)){
|
|
|
|
rhizome_fail_write(&write);
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-08-20 23:51:40 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 08:43:39 +00:00
|
|
|
return rhizome_finish_write(&write);
|
2013-08-20 23:51:40 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 08:43:39 +00:00
|
|
|
/* Checks the size of the file with the given path as a candidate payload for an existing manifest.
|
|
|
|
* An empty path (zero length) is taken to mean empty payload (size = 0). If the manifest's
|
|
|
|
* 'filesize' is not yet set, then sets the manifest's 'filesize' to the size of the file and
|
|
|
|
* returns 0. Otherwise, if the file's size equals the 'filesize' in the manifest, return 0. If
|
|
|
|
* the file size does not match the manifest's 'filesize', returns 1. If there is an error calling
|
|
|
|
* stat(2) on the payload file (eg, file does not exist), returns -1.
|
|
|
|
*/
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_stat_payload_file(rhizome_manifest *m, const char *filepath)
|
2012-12-19 05:46:49 +00:00
|
|
|
{
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
uint64_t size = 0;
|
2012-12-19 05:46:49 +00:00
|
|
|
if (filepath[0]) {
|
|
|
|
struct stat stat;
|
2013-12-27 18:56:22 +00:00
|
|
|
if (lstat(filepath, &stat)) {
|
|
|
|
WHYF_perror("lstat(%s)", alloca_str_toprint(filepath));
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
size = stat.st_size;
|
2012-12-19 05:46:49 +00:00
|
|
|
}
|
2013-12-19 08:43:39 +00:00
|
|
|
if (m->filesize == RHIZOME_SIZE_UNSET)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
rhizome_manifest_set_filesize(m, size);
|
2013-12-19 08:43:39 +00:00
|
|
|
else if (size != m->filesize) {
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-19 08:43:39 +00:00
|
|
|
DEBUGF("payload file %s (size=%"PRIu64") does not match manifest[%d].filesize=%"PRIu64,
|
|
|
|
alloca_str_toprint(filepath), size, m->manifest_record_number, m->filesize);
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_WRONG_SIZE;
|
2013-12-19 08:43:39 +00:00
|
|
|
}
|
2013-12-27 18:56:22 +00:00
|
|
|
return size ? RHIZOME_PAYLOAD_STATUS_NEW : RHIZOME_PAYLOAD_STATUS_EMPTY;
|
2012-12-20 04:48:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 00:06:38 +00:00
|
|
|
static enum rhizome_payload_status rhizome_write_derive_key(rhizome_manifest *m, struct rhizome_write *write)
|
2013-07-05 06:54:50 +00:00
|
|
|
{
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->payloadEncryption != PAYLOAD_ENCRYPTED)
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_NEW;
|
|
|
|
|
|
|
|
// if the manifest specifies encryption, make sure we can generate the payload key and encrypt the
|
|
|
|
// contents as we go
|
|
|
|
if (!rhizome_derive_payload_key(m))
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_CRYPTO_FAIL;
|
2013-07-05 06:54:50 +00:00
|
|
|
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-30 07:39:56 +00:00
|
|
|
DEBUGF("Encrypting payload contents for bid=%s, version=%"PRIu64,
|
|
|
|
alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), m->version);
|
2013-07-19 06:45:05 +00:00
|
|
|
|
|
|
|
write->crypt=1;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->is_journal && m->tail > 0)
|
|
|
|
write->tail = m->tail;
|
2013-07-22 05:34:02 +00:00
|
|
|
|
2013-07-05 06:54:50 +00:00
|
|
|
bcopy(m->payloadKey, write->key, sizeof(write->key));
|
|
|
|
bcopy(m->payloadNonce, write->nonce, sizeof(write->nonce));
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_NEW;
|
2013-07-05 06:54:50 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_write_open_manifest(struct rhizome_write *write, rhizome_manifest *m)
|
2013-07-25 05:12:30 +00:00
|
|
|
{
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_write(
|
2013-12-19 08:43:39 +00:00
|
|
|
write,
|
|
|
|
m->has_filehash ? &m->filehash : NULL,
|
2014-06-18 07:55:15 +00:00
|
|
|
m->filesize
|
2013-12-27 18:56:22 +00:00
|
|
|
);
|
|
|
|
if (status == RHIZOME_PAYLOAD_STATUS_NEW)
|
|
|
|
status = rhizome_write_derive_key(m, write);
|
|
|
|
return status;
|
2013-07-25 05:12:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-20 04:48:59 +00:00
|
|
|
// import a file for a new bundle with an unknown file hash
|
|
|
|
// update the manifest with the details of the file
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_store_payload_file(rhizome_manifest *m, const char *filepath)
|
2012-12-20 04:48:59 +00:00
|
|
|
{
|
2012-12-19 05:46:49 +00:00
|
|
|
// Stream the file directly into the database, encrypting & hashing as we go.
|
|
|
|
struct rhizome_write write;
|
|
|
|
bzero(&write, sizeof(write));
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_write_open_manifest(&write, m);
|
2014-07-02 07:46:19 +00:00
|
|
|
int status_ok = 0;
|
2013-12-27 18:56:22 +00:00
|
|
|
switch (status) {
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EMPTY:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_NEW:
|
2014-07-02 07:46:19 +00:00
|
|
|
status_ok = 1;
|
2013-12-27 18:56:22 +00:00
|
|
|
break;
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_STORED:
|
2014-06-18 07:55:15 +00:00
|
|
|
case RHIZOME_PAYLOAD_STATUS_TOO_BIG:
|
2014-07-02 07:46:19 +00:00
|
|
|
case RHIZOME_PAYLOAD_STATUS_EVICTED:
|
2013-12-27 18:56:22 +00:00
|
|
|
case RHIZOME_PAYLOAD_STATUS_ERROR:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_SIZE:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_HASH:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_CRYPTO_FAIL:
|
|
|
|
return status;
|
|
|
|
}
|
2014-07-02 07:46:19 +00:00
|
|
|
if (!status_ok)
|
|
|
|
FATALF("rhizome_write_open_manifest() returned status = %d", status);
|
2015-03-27 18:38:07 +00:00
|
|
|
if (rhizome_write_file(&write, filepath) == -1)
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
else
|
|
|
|
status = rhizome_finish_write(&write);
|
|
|
|
return rhizome_finish_store(&write, m, status);
|
2012-12-19 05:46:49 +00:00
|
|
|
}
|
2012-12-20 04:48:59 +00:00
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
/* Return RHIZOME_PAYLOAD_STATUS_STORED if file blob found, RHIZOME_PAYLOAD_STATUS_NEW if not found.
|
2013-02-20 04:14:29 +00:00
|
|
|
*/
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_open_read(struct rhizome_read *read, const rhizome_filehash_t *hashp)
|
2013-02-20 04:14:29 +00:00
|
|
|
{
|
2013-10-10 07:53:06 +00:00
|
|
|
read->id = *hashp;
|
2013-12-11 00:41:34 +00:00
|
|
|
read->blob_rowid = 0;
|
2013-02-22 03:55:48 +00:00
|
|
|
read->blob_fd = -1;
|
2014-07-04 04:55:28 +00:00
|
|
|
read->verified = 0;
|
|
|
|
read->offset = 0;
|
|
|
|
read->hash_offset = 0;
|
2014-06-28 08:06:06 +00:00
|
|
|
|
|
|
|
if (sqlite_exec_uint64(&read->length,"SELECT length FROM FILES WHERE id = ?",
|
|
|
|
RHIZOME_FILEHASH_T, &read->id, END) == -1)
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
|
2013-12-11 00:41:34 +00:00
|
|
|
if (sqlite_exec_uint64(&read->blob_rowid,
|
2014-06-28 08:06:06 +00:00
|
|
|
"SELECT rowid "
|
|
|
|
"FROM FILEBLOBS "
|
|
|
|
"WHERE id = ?", RHIZOME_FILEHASH_T, &read->id, END) == -1)
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
2014-06-28 08:06:06 +00:00
|
|
|
|
|
|
|
if (read->blob_rowid == 0) {
|
2013-02-22 03:55:48 +00:00
|
|
|
// No row in FILEBLOBS, look for an external blob file.
|
2013-02-20 03:59:08 +00:00
|
|
|
char blob_path[1024];
|
2014-03-26 05:05:43 +00:00
|
|
|
if (!FORMF_RHIZOME_STORE_PATH(blob_path, "%s/%s", RHIZOME_BLOB_SUBDIR, alloca_tohex_rhizome_filehash_t(read->id)))
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-02-22 03:55:48 +00:00
|
|
|
read->blob_fd = open(blob_path, O_RDONLY);
|
|
|
|
if (read->blob_fd == -1) {
|
2013-12-18 07:10:24 +00:00
|
|
|
if (errno == ENOENT) {
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-18 07:10:24 +00:00
|
|
|
DEBUGF("Stored file does not exist: %s", blob_path);
|
2014-02-28 19:15:32 +00:00
|
|
|
// make sure we remove an orphan file row
|
|
|
|
rhizome_delete_file(&read->id);
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_NEW;
|
2013-12-18 07:10:24 +00:00
|
|
|
}
|
2013-12-27 18:56:22 +00:00
|
|
|
WHYF_perror("open(%s)", alloca_str_toprint(blob_path));
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
2013-02-22 03:55:48 +00:00
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
off64_t pos = lseek64(read->blob_fd, 0, SEEK_END);
|
2013-12-27 18:56:22 +00:00
|
|
|
if (pos == -1) {
|
|
|
|
WHYF_perror("lseek64(%s,0,SEEK_END)", alloca_str_toprint(blob_path));
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
}
|
2014-06-28 08:06:06 +00:00
|
|
|
if (read->length != (uint64_t)pos){
|
|
|
|
WHYF("Length mismatch");
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
}
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-18 07:10:24 +00:00
|
|
|
DEBUGF("Opened stored file %s as fd %d, len %"PRIx64, blob_path, read->blob_fd, read->length);
|
2012-12-28 01:04:22 +00:00
|
|
|
}
|
2013-08-21 06:15:18 +00:00
|
|
|
SHA512_Init(&read->sha512_context);
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_STORED;
|
2012-12-20 04:48:59 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
static ssize_t rhizome_read_retry(sqlite_retry_state *retry, struct rhizome_read *read_state, unsigned char *buffer, size_t bufsz)
|
|
|
|
{
|
|
|
|
IN();
|
|
|
|
if (read_state->blob_fd != -1) {
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (lseek64(read_state->blob_fd, (off64_t) read_state->offset, SEEK_SET) == -1)
|
|
|
|
RETURN(WHYF_perror("lseek64(%d,%"PRIu64",SEEK_SET)", read_state->blob_fd, read_state->offset));
|
2013-10-16 07:55:58 +00:00
|
|
|
if (bufsz == 0)
|
|
|
|
RETURN(0);
|
|
|
|
ssize_t rd = read(read_state->blob_fd, buffer, bufsz);
|
|
|
|
if (rd == -1)
|
|
|
|
RETURN(WHYF_perror("read(%d,%p,%zu)", read_state->blob_fd, buffer, bufsz));
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-10-16 07:55:58 +00:00
|
|
|
DEBUGF("Read %zu bytes from fd=%d @%"PRIx64, (size_t) rd, read_state->blob_fd, read_state->offset);
|
|
|
|
RETURN(rd);
|
|
|
|
}
|
2013-12-11 00:41:34 +00:00
|
|
|
if (read_state->blob_rowid == 0)
|
2013-12-30 04:26:50 +00:00
|
|
|
RETURN(WHY("blob not created"));
|
2013-10-16 07:55:58 +00:00
|
|
|
sqlite3_blob *blob = NULL;
|
2013-12-30 04:26:50 +00:00
|
|
|
if (sqlite_blob_open_retry(retry, "main", "FILEBLOBS", "data", read_state->blob_rowid, 0 /* read only */, &blob) == -1)
|
|
|
|
RETURN(WHY("blob open failed"));
|
2013-10-16 07:55:58 +00:00
|
|
|
assert(blob != NULL);
|
2014-06-28 08:06:06 +00:00
|
|
|
assert(read_state->length == (uint64_t)sqlite3_blob_bytes(blob));
|
2013-10-16 07:55:58 +00:00
|
|
|
// A NULL buffer skips the actual sqlite3_blob_read() call, which is useful just to work out
|
|
|
|
// the length.
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
if (buffer && bufsz && read_state->offset < read_state->length) {
|
2013-12-10 06:51:35 +00:00
|
|
|
bytes_read = (size_t)(read_state->length - read_state->offset);
|
2013-10-16 07:55:58 +00:00
|
|
|
if (bytes_read > bufsz)
|
|
|
|
bytes_read = bufsz;
|
|
|
|
assert(bytes_read > 0);
|
2013-12-30 04:26:50 +00:00
|
|
|
int ret;
|
2013-10-16 07:55:58 +00:00
|
|
|
do {
|
|
|
|
ret = sqlite3_blob_read(blob, buffer, (int) bytes_read, read_state->offset);
|
|
|
|
} while (sqlite_code_busy(ret) && sqlite_retry(retry, "sqlite3_blob_read"));
|
|
|
|
if (ret != SQLITE_OK) {
|
|
|
|
WHYF("sqlite3_blob_read() failed: %s", sqlite3_errmsg(rhizome_db));
|
2013-12-30 04:26:50 +00:00
|
|
|
sqlite_blob_close(blob);
|
2013-10-16 07:55:58 +00:00
|
|
|
RETURN(-1);
|
|
|
|
}
|
|
|
|
}
|
2013-12-30 04:26:50 +00:00
|
|
|
sqlite_blob_close(blob);
|
2013-10-16 07:55:58 +00:00
|
|
|
RETURN(bytes_read);
|
|
|
|
OUT();
|
|
|
|
}
|
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
/* Read content from the store, hashing and decrypting as we go.
|
2013-08-21 06:15:18 +00:00
|
|
|
Random access is supported, but hashing requires all payload contents to be read sequentially. */
|
2012-12-28 01:04:22 +00:00
|
|
|
// returns the number of bytes read
|
2013-10-17 12:47:41 +00:00
|
|
|
ssize_t rhizome_read(struct rhizome_read *read_state, unsigned char *buffer, size_t buffer_length)
|
2013-02-22 03:55:48 +00:00
|
|
|
{
|
2013-02-16 09:55:26 +00:00
|
|
|
IN();
|
2013-08-21 06:15:18 +00:00
|
|
|
// hash check failed, just return an error
|
2014-07-04 04:55:28 +00:00
|
|
|
if (read_state->verified == -1)
|
2013-08-21 06:15:18 +00:00
|
|
|
RETURN(-1);
|
2013-10-16 07:55:58 +00:00
|
|
|
|
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
|
|
|
ssize_t n = rhizome_read_retry(&retry, read_state, buffer, buffer_length);
|
|
|
|
if (n == -1)
|
|
|
|
RETURN(-1);
|
|
|
|
size_t bytes_read = (size_t) n;
|
|
|
|
|
2013-08-21 06:15:18 +00:00
|
|
|
// hash the payload as we go, but only if we happen to read the payload data in order
|
|
|
|
if (read_state->hash_offset == read_state->offset && buffer && bytes_read>0){
|
|
|
|
SHA512_Update(&read_state->sha512_context, buffer, bytes_read);
|
|
|
|
read_state->hash_offset += bytes_read;
|
2014-07-04 04:55:28 +00:00
|
|
|
|
|
|
|
// if we hash everything and the hash doesn't match, we need to delete the payload
|
2013-12-10 06:51:35 +00:00
|
|
|
if (read_state->hash_offset >= read_state->length){
|
2013-10-10 07:53:06 +00:00
|
|
|
rhizome_filehash_t hash_out;
|
|
|
|
SHA512_Final(hash_out.binary, &read_state->sha512_context);
|
|
|
|
SHA512_End(&read_state->sha512_context, NULL);
|
|
|
|
if (cmp_rhizome_filehash_t(&read_state->id, &hash_out) != 0) {
|
2013-08-21 06:15:18 +00:00
|
|
|
// hash failure, mark the payload as invalid
|
2014-07-04 04:55:28 +00:00
|
|
|
read_state->verified = -1;
|
2013-10-10 07:53:06 +00:00
|
|
|
RETURN(WHYF("Expected hash=%s, got %s", alloca_tohex_rhizome_filehash_t(read_state->id), alloca_tohex_rhizome_filehash_t(hash_out)));
|
2014-07-04 04:55:28 +00:00
|
|
|
}else{
|
|
|
|
// we read it, and it's good. Lets remember that (not fatal if the database is locked)
|
|
|
|
read_state->verified = 1;
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
2012-12-28 01:04:22 +00:00
|
|
|
}
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
2013-08-21 06:15:18 +00:00
|
|
|
|
2013-02-20 03:59:08 +00:00
|
|
|
if (read_state->crypt && buffer && bytes_read>0){
|
2013-07-22 05:34:02 +00:00
|
|
|
if(rhizome_crypt_xor_block(
|
|
|
|
buffer, bytes_read,
|
|
|
|
read_state->offset + read_state->tail,
|
|
|
|
read_state->key, read_state->nonce)){
|
2013-02-16 09:55:26 +00:00
|
|
|
RETURN(-1);
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
read_state->offset += bytes_read;
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
DEBUGF("read %zu bytes, read_state->offset=%"PRIu64, bytes_read, read_state->offset);
|
2013-02-20 03:59:08 +00:00
|
|
|
RETURN(bytes_read);
|
2013-02-16 09:55:26 +00:00
|
|
|
OUT();
|
2012-12-20 04:48:59 +00:00
|
|
|
}
|
2013-01-02 02:23:00 +00:00
|
|
|
|
2013-07-25 05:12:30 +00:00
|
|
|
/* Read len bytes from read->offset into data, using *buffer to cache any reads */
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ssize_t rhizome_read_buffered(struct rhizome_read *read, struct rhizome_read_buffer *buffer, unsigned char *data, size_t len)
|
2013-07-25 05:12:30 +00:00
|
|
|
{
|
2013-10-17 12:47:41 +00:00
|
|
|
size_t bytes_copied=0;
|
2013-07-26 05:06:11 +00:00
|
|
|
|
2013-07-25 05:12:30 +00:00
|
|
|
while (len>0){
|
2013-11-21 07:02:58 +00:00
|
|
|
//DEBUGF("len=%zu read->length=%"PRIu64" read->offset=%"PRIu64" buffer->offset=%"PRIu64"", len, read->length, read->offset, buffer->offset);
|
2013-07-26 05:06:11 +00:00
|
|
|
// make sure we only attempt to read data that actually exists
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (read->length != RHIZOME_SIZE_UNSET && read->offset + len > read->length)
|
2013-07-26 05:06:11 +00:00
|
|
|
len = read->length - read->offset;
|
|
|
|
|
2013-07-25 05:12:30 +00:00
|
|
|
// if we can supply either the beginning or end of the data from cache, do that first.
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (read->offset >= buffer->offset) {
|
|
|
|
assert(read->offset - buffer->offset <= SIZE_MAX);
|
|
|
|
size_t ofs = read->offset - buffer->offset;
|
|
|
|
if (ofs <= buffer->len){
|
|
|
|
size_t size = len;
|
|
|
|
if (size > buffer->len - ofs)
|
|
|
|
size = buffer->len - ofs;
|
|
|
|
if (size > 0){
|
|
|
|
// copy into the start of the data buffer
|
|
|
|
bcopy(buffer->data + ofs, data, size);
|
|
|
|
data+=size;
|
|
|
|
len-=size;
|
|
|
|
read->offset+=size;
|
|
|
|
bytes_copied+=size;
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-25 05:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (read->offset + len > buffer->offset) {
|
|
|
|
assert(read->offset + len - buffer->offset <= SIZE_MAX);
|
|
|
|
size_t ofs = read->offset + len - buffer->offset;
|
|
|
|
if (ofs <= buffer->len){
|
|
|
|
size_t size = len;
|
|
|
|
if (size > ofs)
|
|
|
|
size = ofs;
|
|
|
|
if (size>0){
|
|
|
|
// copy into the end of the data buffer
|
|
|
|
bcopy(buffer->data + ofs - size, data + len - size, size);
|
|
|
|
len-=size;
|
|
|
|
bytes_copied+=size;
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-25 05:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, so we need to read a new buffer to fulfill the request.
|
|
|
|
// remember the requested read offset so we can put it back
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
uint64_t ofs = read->offset;
|
2013-07-25 05:12:30 +00:00
|
|
|
buffer->offset = read->offset = ofs & ~(RHIZOME_CRYPT_PAGE_SIZE -1);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
ssize_t r = rhizome_read(read, buffer->data, sizeof buffer->data);
|
2013-07-25 05:12:30 +00:00
|
|
|
read->offset = ofs;
|
2013-10-17 12:47:41 +00:00
|
|
|
buffer->len = 0;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (r == -1)
|
2013-10-17 12:47:41 +00:00
|
|
|
return -1;
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
buffer->len = (size_t) r;
|
2013-07-25 05:12:30 +00:00
|
|
|
}
|
2013-07-26 05:06:11 +00:00
|
|
|
return bytes_copied;
|
2013-07-25 05:12:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
void rhizome_read_close(struct rhizome_read *read)
|
2013-02-22 03:55:48 +00:00
|
|
|
{
|
2013-12-27 18:56:22 +00:00
|
|
|
if (read->blob_fd != -1) {
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-06-18 06:58:26 +00:00
|
|
|
DEBUGF("Closing store fd %d", read->blob_fd);
|
2013-02-20 03:59:08 +00:00
|
|
|
close(read->blob_fd);
|
2013-12-27 18:56:22 +00:00
|
|
|
read->blob_fd = -1;
|
2013-06-18 06:58:26 +00:00
|
|
|
}
|
2014-07-04 04:55:28 +00:00
|
|
|
|
|
|
|
if (read->verified==-1) {
|
2013-08-21 06:15:18 +00:00
|
|
|
// delete payload!
|
2013-10-10 07:53:06 +00:00
|
|
|
rhizome_delete_file(&read->id);
|
2014-07-04 04:55:28 +00:00
|
|
|
}else if(read->verified==1) {
|
|
|
|
// remember when we verified the file
|
|
|
|
time_ms_t now = gettime_ms();
|
|
|
|
sqlite_exec_void_loglevel(LOG_LEVEL_WARN,
|
|
|
|
"UPDATE FILES SET last_verified = ? WHERE id=?",
|
|
|
|
INT64, now,
|
|
|
|
RHIZOME_FILEHASH_T, &read->id,
|
|
|
|
END);
|
2013-08-21 06:15:18 +00:00
|
|
|
}
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 05:27:28 +00:00
|
|
|
struct cache_entry{
|
|
|
|
struct cache_entry *_left;
|
|
|
|
struct cache_entry *_right;
|
2013-10-03 13:46:45 +00:00
|
|
|
rhizome_bid_t bundle_id;
|
2013-08-16 05:27:28 +00:00
|
|
|
uint64_t version;
|
|
|
|
struct rhizome_read read_state;
|
|
|
|
time_ms_t expires;
|
|
|
|
};
|
|
|
|
struct cache_entry *root;
|
|
|
|
|
2013-10-03 13:46:45 +00:00
|
|
|
static struct cache_entry ** find_entry_location(struct cache_entry **ptr, const rhizome_bid_t *bundle_id, uint64_t version)
|
2013-08-16 05:27:28 +00:00
|
|
|
{
|
|
|
|
while(*ptr){
|
|
|
|
struct cache_entry *entry = *ptr;
|
2013-10-03 13:46:45 +00:00
|
|
|
int cmp = cmp_rhizome_bid_t(bundle_id, &entry->bundle_id);
|
2013-08-16 05:27:28 +00:00
|
|
|
if (cmp==0){
|
|
|
|
if (entry->version==version)
|
|
|
|
break;
|
|
|
|
if (version < entry->version)
|
|
|
|
ptr = &entry->_left;
|
|
|
|
else
|
|
|
|
ptr = &entry->_right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cmp<0)
|
|
|
|
ptr = &entry->_left;
|
|
|
|
else
|
|
|
|
ptr = &entry->_right;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static time_ms_t close_entries(struct cache_entry **entry, time_ms_t timeout)
|
|
|
|
{
|
|
|
|
if (!*entry)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
time_ms_t ret = close_entries(&(*entry)->_left, timeout);
|
|
|
|
time_ms_t t_right = close_entries(&(*entry)->_right, timeout);
|
|
|
|
if (t_right!=0 && (t_right < ret || ret==0))
|
|
|
|
ret=t_right;
|
|
|
|
|
|
|
|
if ((*entry)->expires < timeout || timeout==0){
|
|
|
|
rhizome_read_close(&(*entry)->read_state);
|
|
|
|
// remember the two children
|
|
|
|
struct cache_entry *left=(*entry)->_left;
|
|
|
|
struct cache_entry *right=(*entry)->_right;
|
|
|
|
// free this entry
|
|
|
|
free(*entry);
|
|
|
|
// re-add both children to the tree
|
|
|
|
*entry=left;
|
|
|
|
if (right){
|
2013-10-03 13:46:45 +00:00
|
|
|
entry = find_entry_location(entry, &right->bundle_id, right->version);
|
2013-08-16 05:27:28 +00:00
|
|
|
*entry=right;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if ((*entry)->expires < ret || ret==0)
|
|
|
|
ret=(*entry)->expires;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close any expired cache entries
|
|
|
|
static void rhizome_cache_alarm(struct sched_ent *alarm)
|
|
|
|
{
|
|
|
|
alarm->alarm = close_entries(&root, gettime_ms());
|
|
|
|
if (alarm->alarm){
|
|
|
|
alarm->deadline = alarm->alarm + 1000;
|
|
|
|
schedule(alarm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct profile_total cache_alarm_stats={
|
|
|
|
.name="rhizome_cache_alarm",
|
|
|
|
};
|
|
|
|
static struct sched_ent cache_alarm={
|
|
|
|
.function = rhizome_cache_alarm,
|
|
|
|
.stats = &cache_alarm_stats,
|
|
|
|
};
|
|
|
|
|
|
|
|
// close all cache entries
|
|
|
|
int rhizome_cache_close()
|
|
|
|
{
|
|
|
|
close_entries(&root, 0);
|
|
|
|
unschedule(&cache_alarm);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-24 04:53:40 +00:00
|
|
|
static int _rhizome_cache_count(struct cache_entry *entry)
|
|
|
|
{
|
|
|
|
if (!entry)
|
|
|
|
return 0;
|
|
|
|
return 1+_rhizome_cache_count(entry->_left)+_rhizome_cache_count(entry->_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rhizome_cache_count()
|
|
|
|
{
|
|
|
|
return _rhizome_cache_count(root);
|
|
|
|
}
|
|
|
|
|
2013-08-16 05:27:28 +00:00
|
|
|
// read a block of data, caching meta data for reuse
|
2013-12-27 18:56:22 +00:00
|
|
|
ssize_t rhizome_read_cached(const rhizome_bid_t *bidp, uint64_t version, time_ms_t timeout, uint64_t fileOffset, unsigned char *buffer, size_t length)
|
2013-08-16 05:27:28 +00:00
|
|
|
{
|
|
|
|
// look for a cached entry
|
2013-10-03 13:46:45 +00:00
|
|
|
struct cache_entry **ptr = find_entry_location(&root, bidp, version);
|
2013-08-16 05:27:28 +00:00
|
|
|
struct cache_entry *entry = *ptr;
|
|
|
|
|
|
|
|
// if we don't have one yet, create one and open it
|
|
|
|
if (!entry){
|
2013-10-10 07:53:06 +00:00
|
|
|
rhizome_filehash_t filehash;
|
2014-07-07 02:01:09 +00:00
|
|
|
if (rhizome_database_filehash_from_id(bidp, version, &filehash) != 0){
|
|
|
|
if (config.debug.rhizome_store)
|
|
|
|
DEBUGF("Payload not found for bundle bid=%s version=%"PRIu64,
|
|
|
|
alloca_tohex_rhizome_bid_t(*bidp), version);
|
2013-08-16 05:27:28 +00:00
|
|
|
return -1;
|
2014-07-07 02:01:09 +00:00
|
|
|
}
|
2013-08-16 05:27:28 +00:00
|
|
|
entry = emalloc_zero(sizeof(struct cache_entry));
|
2013-12-27 18:56:22 +00:00
|
|
|
if (entry == NULL)
|
|
|
|
return -1;
|
|
|
|
enum rhizome_payload_status status = rhizome_open_read(&entry->read_state, &filehash);
|
|
|
|
switch (status) {
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EMPTY:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_STORED:
|
|
|
|
break;
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_NEW:
|
|
|
|
free(entry);
|
|
|
|
return WHYF("Payload %s not found", alloca_tohex_rhizome_filehash_t(filehash));
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_ERROR:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_SIZE:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_HASH:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_CRYPTO_FAIL:
|
|
|
|
free(entry);
|
|
|
|
return WHYF("Error opening payload %s", alloca_tohex_rhizome_filehash_t(filehash));
|
|
|
|
default:
|
|
|
|
FATALF("status = %d", status);
|
2013-08-16 05:27:28 +00:00
|
|
|
}
|
2013-10-03 13:46:45 +00:00
|
|
|
entry->bundle_id = *bidp;
|
2013-08-16 05:27:28 +00:00
|
|
|
entry->version = version;
|
|
|
|
*ptr = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->read_state.offset = fileOffset;
|
2013-12-10 06:47:18 +00:00
|
|
|
if (entry->read_state.length != RHIZOME_SIZE_UNSET && fileOffset >= entry->read_state.length)
|
2013-08-16 05:27:28 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (entry->expires < timeout){
|
|
|
|
entry->expires = timeout;
|
|
|
|
|
|
|
|
if (!cache_alarm.alarm){
|
|
|
|
cache_alarm.alarm = timeout;
|
|
|
|
cache_alarm.deadline = timeout + 1000;
|
|
|
|
schedule(&cache_alarm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rhizome_read(&entry->read_state, buffer, length);
|
|
|
|
}
|
|
|
|
|
2013-02-20 04:14:29 +00:00
|
|
|
/* Returns -1 on error, 0 on success.
|
|
|
|
*/
|
2013-01-02 02:23:00 +00:00
|
|
|
static int write_file(struct rhizome_read *read, const char *filepath){
|
|
|
|
int fd=-1, ret=0;
|
|
|
|
|
|
|
|
if (filepath&&filepath[0]) {
|
|
|
|
fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0775);
|
|
|
|
if (fd == -1)
|
|
|
|
return WHY_perror("open");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char buffer[RHIZOME_CRYPT_PAGE_SIZE];
|
|
|
|
while((ret=rhizome_read(read, buffer, sizeof(buffer)))>0){
|
|
|
|
if (fd!=-1){
|
|
|
|
if (write(fd,buffer,ret)!=ret) {
|
|
|
|
ret = WHY("Failed to write data to file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd!=-1){
|
|
|
|
if (close(fd)==-1)
|
|
|
|
ret=WHY_perror("close");
|
|
|
|
if (ret<0){
|
|
|
|
// TODO delete partial file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
static enum rhizome_payload_status read_derive_key(rhizome_manifest *m, struct rhizome_read *read_state)
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
{
|
|
|
|
read_state->crypt = m->payloadEncryption == PAYLOAD_ENCRYPTED;
|
2013-07-19 06:45:05 +00:00
|
|
|
if (read_state->crypt){
|
|
|
|
// if the manifest specifies encryption, make sure we can generate the payload key and encrypt
|
|
|
|
// the contents as we go
|
2013-12-27 18:56:22 +00:00
|
|
|
if (!rhizome_derive_payload_key(m)) {
|
2013-07-19 06:45:05 +00:00
|
|
|
rhizome_read_close(read_state);
|
2013-12-27 18:56:22 +00:00
|
|
|
WHY("Unable to decrypt bundle, valid key not found");
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_CRYPTO_FAIL;
|
2013-07-19 06:45:05 +00:00
|
|
|
}
|
2014-03-12 03:18:08 +00:00
|
|
|
if (config.debug.rhizome_store)
|
2013-12-11 00:41:34 +00:00
|
|
|
DEBUGF("Decrypting payload contents for bid=%s version=%"PRIu64, alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), m->version);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (m->is_journal && m->tail > 0)
|
|
|
|
read_state->tail = m->tail;
|
2013-07-19 06:45:05 +00:00
|
|
|
bcopy(m->payloadKey, read_state->key, sizeof(read_state->key));
|
|
|
|
bcopy(m->payloadNonce, read_state->nonce, sizeof(read_state->nonce));
|
|
|
|
}
|
2013-12-27 18:56:22 +00:00
|
|
|
return RHIZOME_PAYLOAD_STATUS_STORED;
|
2013-07-19 06:45:05 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_open_decrypt_read(rhizome_manifest *m, struct rhizome_read *read_state)
|
2013-10-10 07:53:06 +00:00
|
|
|
{
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_read(read_state, &m->filehash);
|
|
|
|
if (status == RHIZOME_PAYLOAD_STATUS_STORED)
|
|
|
|
status = read_derive_key(m, read_state);
|
|
|
|
return status;
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 06:52:53 +00:00
|
|
|
/* Extract the file related to a manifest to the file system. The file will be de-crypted and
|
|
|
|
* verified while reading. If filepath is not supplied, the file will still be checked.
|
2013-02-20 03:59:08 +00:00
|
|
|
*/
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_extract_file(rhizome_manifest *m, const char *filepath)
|
2013-02-22 03:55:48 +00:00
|
|
|
{
|
2013-02-20 03:59:08 +00:00
|
|
|
struct rhizome_read read_state;
|
|
|
|
bzero(&read_state, sizeof read_state);
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_decrypt_read(m, &read_state);
|
|
|
|
if (status == RHIZOME_PAYLOAD_STATUS_STORED) {
|
|
|
|
if (write_file(&read_state, filepath) == -1)
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
}
|
2013-02-20 03:59:08 +00:00
|
|
|
rhizome_read_close(&read_state);
|
2013-12-27 18:56:22 +00:00
|
|
|
return status;
|
2013-01-02 02:23:00 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 04:14:29 +00:00
|
|
|
/* dump the raw contents of a file
|
|
|
|
*/
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_dump_file(const rhizome_filehash_t *hashp, const char *filepath, uint64_t *lengthp)
|
2013-02-20 04:14:29 +00:00
|
|
|
{
|
2013-01-02 02:23:00 +00:00
|
|
|
struct rhizome_read read_state;
|
|
|
|
bzero(&read_state, sizeof read_state);
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_read(&read_state, hashp);
|
|
|
|
if (status == RHIZOME_PAYLOAD_STATUS_STORED) {
|
|
|
|
if (write_file(&read_state, filepath) == -1)
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
else if (lengthp)
|
2013-12-10 06:22:53 +00:00
|
|
|
*lengthp = read_state.length;
|
2013-02-20 03:59:08 +00:00
|
|
|
}
|
|
|
|
rhizome_read_close(&read_state);
|
2013-12-27 18:56:22 +00:00
|
|
|
return status;
|
2013-01-02 02:23:00 +00:00
|
|
|
}
|
2013-07-05 06:54:50 +00:00
|
|
|
|
|
|
|
// pipe data from one payload to another
|
|
|
|
static int rhizome_pipe(struct rhizome_read *read, struct rhizome_write *write, uint64_t length)
|
|
|
|
{
|
2013-12-10 06:22:53 +00:00
|
|
|
assert(write->file_offset <= write->file_length);
|
|
|
|
if (length > (uint64_t)(write->file_length - write->file_offset))
|
2013-07-05 06:54:50 +00:00
|
|
|
return WHY("Unable to pipe that much data");
|
|
|
|
|
2013-07-17 06:17:35 +00:00
|
|
|
unsigned char buffer[RHIZOME_CRYPT_PAGE_SIZE];
|
2013-07-05 06:54:50 +00:00
|
|
|
while(length>0){
|
2013-10-17 12:47:41 +00:00
|
|
|
size_t size=sizeof(buffer);
|
2013-07-05 06:54:50 +00:00
|
|
|
if (size > length)
|
|
|
|
size=length;
|
|
|
|
|
2013-10-17 12:47:41 +00:00
|
|
|
ssize_t r = rhizome_read(read, buffer, size);
|
|
|
|
if (r == -1)
|
2013-07-05 06:54:50 +00:00
|
|
|
return r;
|
|
|
|
|
2013-10-17 12:47:41 +00:00
|
|
|
length -= (size_t) r;
|
2013-07-19 06:45:05 +00:00
|
|
|
|
2013-10-17 12:47:41 +00:00
|
|
|
if (rhizome_write_buffer(write, buffer, (size_t) r))
|
2013-07-05 06:54:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-30 04:26:50 +00:00
|
|
|
uint64_t rhizome_copy_file_to_blob(int fd, uint64_t id, size_t size)
|
|
|
|
{
|
|
|
|
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
|
|
|
uint64_t rowid = rhizome_create_fileblob(&retry, id, size);
|
|
|
|
if (rowid == 0)
|
|
|
|
return 0;
|
|
|
|
sqlite3_blob *blob = NULL;
|
|
|
|
if (sqlite_blob_open_retry(&retry, "main", "FILEBLOBS", "data", rowid, 1 /* read/write */, &blob) == -1)
|
|
|
|
goto fail;
|
|
|
|
char buf[16384];
|
|
|
|
size_t offset = 0;
|
|
|
|
while (offset < size) {
|
|
|
|
size_t toread = size - offset;
|
|
|
|
if (toread > sizeof buf)
|
|
|
|
toread = sizeof buf;
|
|
|
|
ssize_t nread = read(fd, buf, toread);
|
|
|
|
if (nread == -1) {
|
|
|
|
WHYF_perror("read(%d,%p,%zu)", fd, buf, toread);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if ((size_t)nread == 0) {
|
|
|
|
WHYF("read(%d,%p,%zu) returned 0", fd, buf, toread);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (sqlite_blob_write_retry(&retry, blob, buf, (int)nread, (int)offset) == -1)
|
|
|
|
goto fail;
|
|
|
|
assert((size_t)nread <= toread);
|
|
|
|
offset += (size_t)nread;
|
|
|
|
}
|
|
|
|
assert(offset == size);
|
|
|
|
sqlite_blob_close(blob);
|
|
|
|
return rowid;
|
|
|
|
fail:
|
|
|
|
if (blob)
|
|
|
|
sqlite_blob_close(blob);
|
|
|
|
sqlite_exec_void_retry(&retry, "DELETE FROM FILEBLOBS WHERE id = ?;", UINT64_TOSTR, id, END);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_journal_pipe(struct rhizome_write *write, const rhizome_filehash_t *hashp, uint64_t start_offset, uint64_t length)
|
2013-08-15 04:23:37 +00:00
|
|
|
{
|
|
|
|
struct rhizome_read read_state;
|
|
|
|
bzero(&read_state, sizeof read_state);
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_read(&read_state, hashp);
|
|
|
|
if (status == RHIZOME_PAYLOAD_STATUS_STORED) {
|
|
|
|
read_state.offset = start_offset;
|
|
|
|
if (rhizome_pipe(&read_state, write, length) == -1)
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
}
|
2013-08-15 04:23:37 +00:00
|
|
|
rhizome_read_close(&read_state);
|
2013-12-27 18:56:22 +00:00
|
|
|
return status;
|
2013-08-15 04:23:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 06:54:50 +00:00
|
|
|
// open an existing journal bundle, advance the head pointer, duplicate the existing content and get ready to add more.
|
2015-03-02 08:22:12 +00:00
|
|
|
enum rhizome_payload_status rhizome_write_open_journal(struct rhizome_write *write, rhizome_manifest *m, uint64_t advance_by, uint64_t append_size)
|
2013-07-05 06:54:50 +00:00
|
|
|
{
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
assert(m->is_journal);
|
2015-03-02 08:22:12 +00:00
|
|
|
assert(m->filesize != RHIZOME_SIZE_UNSET);
|
2013-12-27 18:56:22 +00:00
|
|
|
assert(advance_by <= m->filesize);
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
uint64_t copy_length = m->filesize - advance_by;
|
2015-03-02 08:22:12 +00:00
|
|
|
uint64_t new_filesize = RHIZOME_SIZE_UNSET;
|
|
|
|
if (append_size != RHIZOME_SIZE_UNSET) {
|
|
|
|
assert(m->filesize + append_size > m->filesize); // no wraparound
|
|
|
|
new_filesize = m->filesize + append_size - advance_by;
|
|
|
|
}
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
if (advance_by > 0)
|
|
|
|
rhizome_manifest_set_tail(m, m->tail + advance_by);
|
2015-03-02 08:22:12 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_open_write(write, NULL, new_filesize);
|
2015-03-16 12:11:52 +00:00
|
|
|
if (config.debug.rhizome)
|
2015-03-23 07:10:08 +00:00
|
|
|
DEBUGF("rhizome_open_write() returned %d %s", status, rhizome_payload_status_message(status));
|
2013-12-27 18:56:22 +00:00
|
|
|
if (status == RHIZOME_PAYLOAD_STATUS_NEW && copy_length > 0) {
|
2015-03-16 12:11:52 +00:00
|
|
|
// we don't need to bother decrypting the existing journal payload
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rstatus = rhizome_journal_pipe(write, &m->filehash, advance_by, copy_length);
|
2015-03-16 12:11:52 +00:00
|
|
|
if (config.debug.rhizome)
|
2015-03-23 07:10:08 +00:00
|
|
|
DEBUGF("rhizome_journal_pipe() returned %d %s", rstatus, rhizome_payload_status_message(rstatus));
|
|
|
|
int rstatus_valid = 0;
|
|
|
|
switch (rstatus) {
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EMPTY:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_NEW:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_STORED:
|
|
|
|
rstatus_valid = 1;
|
|
|
|
break;
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_ERROR:
|
2015-03-27 18:38:07 +00:00
|
|
|
case RHIZOME_PAYLOAD_STATUS_TOO_BIG:
|
|
|
|
rstatus_valid = 1;
|
|
|
|
status = rstatus;
|
|
|
|
break;
|
2015-03-23 07:10:08 +00:00
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_SIZE:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_HASH:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_CRYPTO_FAIL:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EVICTED:
|
2015-03-27 18:38:07 +00:00
|
|
|
// rhizome_journal_pipe() should not return any of these codes
|
|
|
|
FATALF("rhizome_journal_pipe() returned %d %s", rstatus, rhizome_payload_status_message(rstatus));
|
2015-03-23 07:10:08 +00:00
|
|
|
}
|
|
|
|
if (!rstatus_valid)
|
|
|
|
FATALF("rstatus = %d", rstatus);
|
2013-07-05 06:54:50 +00:00
|
|
|
}
|
2015-03-16 12:11:52 +00:00
|
|
|
if (status == RHIZOME_PAYLOAD_STATUS_NEW) {
|
2013-12-27 18:56:22 +00:00
|
|
|
status = rhizome_write_derive_key(m, write);
|
2015-03-16 12:11:52 +00:00
|
|
|
if (config.debug.rhizome)
|
2015-03-23 07:10:08 +00:00
|
|
|
DEBUGF("rhizome_write_derive_key() returned %d %s", status, rhizome_payload_status_message(status));
|
2015-03-16 12:11:52 +00:00
|
|
|
}
|
|
|
|
if (status != RHIZOME_PAYLOAD_STATUS_NEW) {
|
2013-07-05 06:54:50 +00:00
|
|
|
rhizome_fail_write(write);
|
2015-03-16 12:11:52 +00:00
|
|
|
}
|
2013-12-27 18:56:22 +00:00
|
|
|
return status;
|
2013-07-05 06:54:50 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 18:38:07 +00:00
|
|
|
// Call to finish any payload store operation
|
|
|
|
enum rhizome_payload_status rhizome_finish_store(struct rhizome_write *write, rhizome_manifest *m, enum rhizome_payload_status status)
|
2015-03-02 08:22:12 +00:00
|
|
|
{
|
2015-03-27 18:38:07 +00:00
|
|
|
if (config.debug.rhizome)
|
|
|
|
DEBUGF("write=%p m=manifest[%d], status=%d %s", write, m->manifest_record_number, status, rhizome_payload_status_message_nonnull(status));
|
|
|
|
switch (status) {
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_NEW:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int status_valid = 0;
|
|
|
|
switch (status) {
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EMPTY:
|
|
|
|
status_valid = 1;
|
|
|
|
assert(write->file_length == 0);
|
|
|
|
break;
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_NEW:
|
|
|
|
assert(write->file_length != RHIZOME_SIZE_UNSET);
|
|
|
|
status_valid = 1;
|
|
|
|
break;
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_STORED:
|
|
|
|
assert(write->file_length != RHIZOME_SIZE_UNSET);
|
|
|
|
status_valid = 1;
|
|
|
|
// TODO: check that stored hash matches received payload's hash
|
|
|
|
break;
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_SIZE:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_WRONG_HASH:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_TOO_BIG:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_CRYPTO_FAIL:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_EVICTED:
|
|
|
|
case RHIZOME_PAYLOAD_STATUS_ERROR:
|
|
|
|
status_valid = 1;
|
|
|
|
rhizome_fail_write(write);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
if (!status_valid)
|
|
|
|
FATALF("status = %d", status);
|
|
|
|
// Fill in missing manifest fields and check consistency with existing fields.
|
|
|
|
if (m->is_journal || m->filesize == RHIZOME_SIZE_UNSET)
|
|
|
|
rhizome_manifest_set_filesize(m, write->file_length);
|
|
|
|
else if (m->filesize != write->file_length) {
|
|
|
|
if (config.debug.rhizome)
|
|
|
|
DEBUGF("m->filesize=%"PRIu64", write->file_length=%"PRIu64, m->filesize, write->file_length);
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_WRONG_SIZE;
|
|
|
|
}
|
|
|
|
if (m->is_journal)
|
|
|
|
rhizome_manifest_set_version(m, m->filesize);
|
|
|
|
if (m->filesize) {
|
|
|
|
if (m->is_journal || !m->has_filehash)
|
|
|
|
rhizome_manifest_set_filehash(m, &write->id);
|
|
|
|
else if (cmp_rhizome_filehash_t(&write->id, &m->filehash) != 0) {
|
|
|
|
if (config.debug.rhizome)
|
|
|
|
DEBUGF("m->filehash=%s, write->id=%s", alloca_tohex_rhizome_filehash_t(m->filehash), alloca_tohex_rhizome_filehash_t(write->id));
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_WRONG_HASH;
|
|
|
|
}
|
|
|
|
} else if (m->is_journal)
|
|
|
|
rhizome_manifest_del_filehash(m);
|
|
|
|
else if (m->has_filehash)
|
|
|
|
return RHIZOME_PAYLOAD_STATUS_WRONG_HASH;
|
|
|
|
return status;
|
2015-03-02 08:22:12 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 08:06:06 +00:00
|
|
|
enum rhizome_payload_status rhizome_append_journal_buffer(rhizome_manifest *m, uint64_t advance_by, uint8_t *buffer, size_t len)
|
2013-07-05 06:54:50 +00:00
|
|
|
{
|
|
|
|
struct rhizome_write write;
|
|
|
|
bzero(&write, sizeof write);
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_write_open_journal(&write, m, advance_by, (uint64_t) len);
|
|
|
|
if (status != RHIZOME_PAYLOAD_STATUS_NEW)
|
|
|
|
return status;
|
2015-03-27 18:38:07 +00:00
|
|
|
if (buffer && len && rhizome_write_buffer(&write, buffer, len) == -1)
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
else
|
|
|
|
status = rhizome_finish_write(&write);
|
|
|
|
return rhizome_finish_store(&write, m, status);
|
2013-07-05 06:54:50 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status rhizome_append_journal_file(rhizome_manifest *m, uint64_t advance_by, const char *filename)
|
2013-07-05 06:54:50 +00:00
|
|
|
{
|
|
|
|
struct stat stat;
|
|
|
|
if (lstat(filename,&stat))
|
Refactor manifest: specific setter functions
Replace generic rhizome_manifest_set() and rhizome_manifest_set_ll()
with per-field setter functions, eg, rhizome_manifest_set_filesize().
Struct rhizome_manifest elements for all known fields, to replace the
use of rhizome_manifest_get() and rhizome_manifest_get_ll() everywhere:
sender, recipient, service, name, date, bundle_key.
Add boolean validity flags for binary blob types, to avoid having to compare
with many bytes of all-zero to detect presence, eg, has_sender, has_recipient,
has_author, has_bundle_key. These maintained by the setter functions.
Rename existing manifest struct elements to be the same as their field
names: fileLength -> filesize, journalTail -> tail.
More use of unsigned int, size_t and uint64_t for payload sizes, offsets, byte
counts, etc. especially in rhizome_store.c and meshms.c. More uniform use of
size_t to dimension memory buffers. Fix some printf(3) style format strings
for 64-bit correctness on 32-bit systems. Use new constant RHIZOME_SIZE_UNSET
instead of -1 to indicate unknown dimension, and explicitly assert its absence
before comparisons and arithmetic, for safety.
Replace some 'int' loop variables with 'unsigned' where appropriate.
Fix bugs discovered in MeshMS bundle private/public key generation and
bundle secret key handling for export/extract commands.
Instrument the first MeshMS test case to aid debugging.
New debug config flag: debug.manifest logs all modifications to all manifest
fields by setter functions.
Rename debug config flag: debug.rhizome_bind -> debug.rhizome_sql_bind.
2013-10-30 12:52:19 +00:00
|
|
|
return WHYF_perror("stat(%s)", alloca_str_toprint(filename));
|
2013-07-05 06:54:50 +00:00
|
|
|
struct rhizome_write write;
|
|
|
|
bzero(&write, sizeof write);
|
2013-12-27 18:56:22 +00:00
|
|
|
enum rhizome_payload_status status = rhizome_write_open_journal(&write, m, advance_by, stat.st_size);
|
|
|
|
if (status != RHIZOME_PAYLOAD_STATUS_NEW)
|
|
|
|
return status;
|
2015-03-27 18:38:07 +00:00
|
|
|
if (stat.st_size != 0 && rhizome_write_file(&write, filename) == -1)
|
|
|
|
status = RHIZOME_PAYLOAD_STATUS_ERROR;
|
|
|
|
else
|
|
|
|
status = rhizome_finish_write(&write);
|
|
|
|
return rhizome_finish_store(&write, m, status);
|
2013-07-05 06:54:50 +00:00
|
|
|
}
|