Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions common/cf-lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ cf_lock_init (void)
char *backend = cf_ext_config_string ("lock_backend");
/* Checkout is unfinished and opt-in. A legacy file-lock switch alone
* must not make normal uploads depend on Guard or the retired ledger. */
char *repos = cf_ext_config_string("checkout_repo_ids");
lock_on = cf_ext_config_bool("checkout_enabled") && repos && repos[0] &&
lock_on = cf_ext_config_bool("checkout_enabled") &&
(!backend || strcmp(backend, "cloudfile") == 0);
g_free(repos);
if (backend && strcmp (backend, "cloudfile") != 0)
seaf_warning ("CloudFile: unsupported lock backend '%s'.\n", backend);
g_free (backend);
Expand Down
145 changes: 51 additions & 94 deletions common/cf-v2-edit-fence.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ path_touches(const char *changed, const char *guarded)
return touches;
}

/* Two policies only: OFF (default) and OPTIONAL (listed repository). Parse the
* startup configuration once; never query SQL for an OFF repository. */
static gsize checkout_policy_state = 0; /* 0=unread, 1=valid, 2=invalid */
static GHashTable *checkout_repo_ids;

static gboolean
canonical_repo_id(const char *id)
{
Expand All @@ -70,94 +65,52 @@ canonical_repo_id(const char *id)
return TRUE;
}

/* Repository allowlist parsed once; OFF libraries never query Protection.
* Protection has no lease and is independent of cf_edit_guard / Checkout. */
static gsize protection_policy_state = 0;
static GHashTable *protection_repo_ids;
/* A final locking read serializes a feature change with publication. Never
* retain process-wide allowlists or interpret an unavailable policy as OFF. */
static int
repo_feature(struct _SeafileSession *session, SeafDBTrans *trans,
const char *repo_id, const char *feature)
{
gboolean error = FALSE;
if (!session || !session->db || !canonical_repo_id(repo_id))
return -1;
gboolean present = trans ? seaf_db_trans_check_for_existence(trans,
"SELECT 1 FROM cf_repo_feature WHERE repo_id=? AND feature=? LOCK IN SHARE MODE",
&error, 2, "string", repo_id, "string", feature) :
seaf_db_statement_exists(session->db,
"SELECT 1 FROM cf_repo_feature WHERE repo_id=? AND feature=?",
&error, 2, "string", repo_id, "string", feature);
return error ? -1 : present;
}

int
cf_v2_file_protection_policy(struct _SeafileSession *session, const char *repo_id)
{
if (g_once_init_enter(&protection_policy_state)) {
gboolean valid = session && session->config;
GError *error = NULL;
char *raw = valid ? g_key_file_get_string(session->config, "cloudfile",
"protection_repo_ids", &error) : NULL;
if (error) {
valid = error->domain == G_KEY_FILE_ERROR &&
(error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND ||
error->code == G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
g_clear_error(&error);
}
protection_repo_ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
if (raw && strlen(raw) > 8192)
valid = FALSE;
if (valid && raw && *g_strstrip(raw)) {
char **parts = g_strsplit(raw, ",", -1);
for (char **q = parts; *q && valid; ++q) {
char *id = g_strstrip(*q);
if (!canonical_repo_id(id) || g_hash_table_contains(protection_repo_ids, id))
valid = FALSE;
else
g_hash_table_add(protection_repo_ids, g_strdup(id));
}
g_strfreev(parts);
}
g_free(raw);
if (!valid)
seaf_warning("CloudFile: invalid file protection repository configuration; writes denied.\n");
g_once_init_leave(&protection_policy_state, valid ? 1 : 2);
return repo_feature(session, NULL, repo_id, "protection");
}

static int
checkout_policy(struct _SeafileSession *session, SeafDBTrans *trans, const char *repo_id)
{
/* C and Go read the same deployment switch from seafile.conf. Only the
* per-repo selection is dynamic; a malformed switch cannot disable a fence. */
if (!session || !session->config)
return -1;
if (!g_key_file_has_key(session->config, "cloudfile", "checkout_enabled", NULL))
return 0;
GError *error = NULL;
gboolean enabled = g_key_file_get_boolean(session->config, "cloudfile", "checkout_enabled", &error);
if (error) {
g_clear_error(&error);
return -1;
}
if (protection_policy_state == 2) return -1;
if (!repo_id) return 0;
return g_hash_table_contains(protection_repo_ids, repo_id) ? 1 : 0;
return enabled ? repo_feature(session, trans, repo_id, "checkout") : 0;
}

int
cf_v2_edit_repo_checkout(struct _SeafileSession *session, const char *repo_id)
{
if (g_once_init_enter(&checkout_policy_state)) {
gboolean valid = TRUE;
GError *error = NULL;
gboolean enabled = session && session->config &&
g_key_file_get_boolean(session->config, "cloudfile",
"checkout_enabled", &error);
if (error) {
/* Missing setting means OFF; an explicitly invalid setting fails closed. */
valid = error->domain == G_KEY_FILE_ERROR &&
(error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND ||
error->code == G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
g_clear_error(&error);
}
checkout_repo_ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
if (valid && enabled) {
char *raw = g_key_file_get_string(session->config, "cloudfile",
"checkout_repo_ids", NULL);
if (raw && strlen(raw) > 8192)
valid = FALSE;
if (raw && *g_strstrip(raw)) {
char **parts = g_strsplit(raw, ",", -1);
for (char **p = parts; *p && valid; ++p) {
char *id = g_strstrip(*p);
if (!canonical_repo_id(id) ||
g_hash_table_contains(checkout_repo_ids, id))
valid = FALSE;
else
g_hash_table_add(checkout_repo_ids, g_strdup(id));
}
g_strfreev(parts);
}
g_free(raw);
}
if (!valid)
seaf_warning("CloudFile: invalid per-repo Checkout configuration; writes denied.\n");
g_once_init_leave(&checkout_policy_state, valid ? 1 : 2);
}
if (checkout_policy_state == 2)
return -1;
if (!repo_id)
return 0;
return g_hash_table_contains(checkout_repo_ids, repo_id) ? 1 : 0;
return checkout_policy(session, NULL, repo_id);
}

/* Schema is validated on guarded paths only. Its absence is a failure for an
Expand Down Expand Up @@ -198,6 +151,7 @@ struct _CfV2EditPrepared {
char *old_id;
char *new_id;
GList *changes;
int checkout, protection, origin_protection;
char *origin;
char *prefix;
GHashTable *candidate_ids;
Expand Down Expand Up @@ -335,7 +289,9 @@ cf_v2_edit_prepare_unscoped_branch(struct _SeafileSession *session,
int protection = cf_v2_file_protection_policy(session, repo_id);
if (policy < 0 || protection < 0)
goto fail;
if (g_hash_table_size(protection_repo_ids)) {
prepared->checkout = policy;
prepared->protection = protection;
{
int n = seaf_db_statement_foreach_row(session->db,
"SELECT origin_repo,path FROM VirtualRepo WHERE repo_id=?",
virtual_mapping, prepared, 1, "string", repo_id);
Expand All @@ -344,15 +300,14 @@ cf_v2_edit_prepare_unscoped_branch(struct _SeafileSession *session,
if (!canonical_repo_id(prepared->origin) || !prepared->prefix ||
prepared->prefix[0] != '/' || !strcmp(prepared->origin, repo_id) ||
strlen(prepared->prefix) < 2 || g_str_has_suffix(prepared->prefix, "/")) goto fail;
if (cf_v2_file_protection_policy(session, prepared->origin) == 0) {
g_clear_pointer(&prepared->origin, g_free);
g_clear_pointer(&prepared->prefix, g_free);
} else {
prepared->origin_protection = cf_v2_file_protection_policy(session, prepared->origin);
if (prepared->origin_protection < 0) goto fail;
if (prepared->origin_protection) {
prepared->candidate_ids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
}
}
}
if ((policy == 0 && protection == 0 && !prepared->origin) || !strcmp(old_commit_id, new_commit_id))
if ((policy == 0 && protection == 0 && !prepared->origin_protection) || !strcmp(old_commit_id, new_commit_id))
return prepared;
if (policy == 1 && guard_available(session) <= 0)
goto fail;
Expand All @@ -368,7 +323,7 @@ cf_v2_edit_prepare_unscoped_branch(struct _SeafileSession *session,
old_commit->root_id, new_commit->root_id,
&prepared->changes, FALSE)
: -1;
if (result == 0 && prepared->origin) {
if (result == 0 && prepared->origin_protection) {
VirtualProof proof = {prepared, session, new_commit->root_id, repo->version, FALSE, FALSE, 0};
guint count = 0;
for (GList *entry = prepared->changes; entry; entry = entry->next) {
Expand Down Expand Up @@ -455,10 +410,12 @@ cf_v2_edit_check_unscoped_branch(SeafDBTrans *trans,
g_strcmp0(prepared->old_id, old_commit_id) ||
g_strcmp0(prepared->new_id, new_commit_id))
return -1;
int checkout = cf_v2_edit_repo_checkout(session, repo_id);
int protection = cf_v2_file_protection_policy(session, repo_id);
if (checkout < 0 || protection < 0) return -1;
if (prepared->origin) {
int checkout = checkout_policy(session, trans, repo_id);
int protection = repo_feature(session, trans, repo_id, "protection");
if (checkout != prepared->checkout || protection != prepared->protection ||
(prepared->origin && repo_feature(session, trans, prepared->origin, "protection") !=
prepared->origin_protection)) return -1;
if (prepared->origin_protection) {
VirtualProof proof = {(CfV2EditPrepared *)prepared, session, NULL, 0, TRUE, FALSE, 0};
for (GList *entry = prepared->changes; entry; entry = entry->next) {
DiffEntry *delta = entry->data;
Expand Down
2 changes: 1 addition & 1 deletion common/cf-v2-share.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ v2_share_schema_present (void)
seaf->db,
"SELECT 1 FROM information_schema.tables "
"WHERE table_schema=DATABASE() AND table_name IN "
"('cf_library_storage','cf_dir_acl','cf_sso_group_map') "
"('cf_repo_feature','cf_dir_acl','cf_sso_group_map') "
"GROUP BY table_schema HAVING COUNT(DISTINCT table_name)=3",
&error, 0);
if (error)
Expand Down
76 changes: 18 additions & 58 deletions common/cf-v2-storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,12 @@ key_valid (const char *key)
return TRUE;
}

/* RepoStorageId is the only physical route. The historical CloudFile registry
* is consulted solely if the native route is absent, to avoid interpreting a
* lost auto-local mapping as an old CE repo. The allocation marker is not a
* second routing source or a parallel Repo/Branch lifecycle. */
/* Explicit native and managed routes make a missing allocation fail closed.
* Cache virtual existence only for the short native lifecycle interval. */
typedef struct {
char *storage_id;
char *legacy_key;
char *legacy_state;
gboolean route_row_present;
gboolean native_repo_present;
gboolean virtual_repo_present;
} Route;


Expand All @@ -114,19 +110,15 @@ static void
route_clear (Route *route)
{
g_free (route->storage_id);
g_free (route->legacy_key);
g_free (route->legacy_state);
memset (route, 0, sizeof(*route));
}

static void
route_copy (Route *dst, const Route *src)
{
dst->storage_id = g_strdup (src->storage_id);
dst->legacy_key = g_strdup (src->legacy_key);
dst->legacy_state = g_strdup (src->legacy_state);
dst->route_row_present = src->route_row_present;
dst->native_repo_present = src->native_repo_present;
dst->virtual_repo_present = src->virtual_repo_present;
}

/* Caller holds route_cache_mutex. */
Expand Down Expand Up @@ -203,9 +195,7 @@ read_route (SeafDBRow *row, void *data)
Route *route = data;
route->storage_id = g_strdup (seaf_db_row_get_column_text (row, 0));
route->route_row_present = seaf_db_row_get_column_text (row, 1) != NULL;
route->legacy_key = g_strdup (seaf_db_row_get_column_text (row, 2));
route->legacy_state = g_strdup (seaf_db_row_get_column_text (row, 3));
route->native_repo_present = seaf_db_row_get_column_text (row, 4) != NULL;
route->virtual_repo_present = seaf_db_row_get_column_text (row, 2) != NULL;
return TRUE;
}

Expand All @@ -221,38 +211,19 @@ cf_v2_store_dir (const char *native_obj_dir, const char *repo_id)
if (!seaf->db || !repo_id || !is_uuid_valid (repo_id))
return NULL;

/* The route itself is the only object location selector. Native lookup
* and historical markers are needed only when RepoStorageId is absent.
* A legacy V2 row with a missing route must NEVER become a native write.
*
* VirtualRepo counts as an existing native resource: its row is inserted
* before its initial commit, while the Repo row is written afterwards.
* Without that fact a folder-share (sub-library) commit would be refused
* even though it belongs in the native tree, exactly like CE. */
gboolean cached = route_cache_get (repo_id, &route);
if (!cached && seaf_db_statement_foreach_row (seaf->db,
"SELECT s.storage_id,s.repo_id,c.repo_key,c.state,"
"COALESCE(r.repo_id,t.repo_id,g.repo_id,v.repo_id) "
"FROM (SELECT ? AS repo_id) i "
"SELECT s.storage_id,s.repo_id,v.repo_id FROM (SELECT ? AS repo_id) i "
"LEFT JOIN RepoStorageId s ON s.repo_id=i.repo_id "
"LEFT JOIN cf_library_storage c ON c.repo_id=i.repo_id AND s.repo_id IS NULL "
"LEFT JOIN Repo r ON r.repo_id=i.repo_id AND s.repo_id IS NULL "
"LEFT JOIN RepoTrash t ON t.repo_id=i.repo_id AND s.repo_id IS NULL "
"LEFT JOIN GarbageRepos g ON g.repo_id=i.repo_id AND s.repo_id IS NULL "
"LEFT JOIN VirtualRepo v ON v.repo_id=i.repo_id AND s.repo_id IS NULL",
read_route, &route, 1, "string", repo_id) != 1)
goto out;

if (!route.route_row_present) {
char *expected = g_strconcat ("native-", repo_id, NULL);
gboolean recognized = route.native_repo_present &&
(!route.legacy_key ||
(g_strcmp0 (route.legacy_key, expected) == 0 &&
(g_strcmp0 (route.legacy_state, "active") == 0 ||
g_strcmp0 (route.legacy_state, "retired") == 0)));
g_free (expected);
if (recognized)
result = g_build_filename (native_obj_dir, repo_id, NULL);
/* Explicit native routes distinguish migrated CE libraries from a lost
* managed route. A virtual repo owns native commits, never origin bytes. */
if (!route.route_row_present || g_strcmp0(route.storage_id, "cf-native") == 0) {
if (route.route_row_present || route.virtual_repo_present)
result = g_build_filename(native_obj_dir, repo_id, NULL);
goto out;
}

Expand Down Expand Up @@ -284,9 +255,8 @@ cf_v2_store_dir (const char *native_obj_dir, const char *repo_id)
return result;
}

/* New libraries have one native route and one immutable UUID safety marker.
* Seafile owns Repo/Branch/Owner/Trash/GC: no extra activate state machine.
* Existing V2 custom-directory rows remain readable without migration. */
/* mkdir and the durable native allocation jointly prevent adopting another
* store, including a historical custom key equal to this new repo UUID. */
int
cf_v2_storage_reserve (const char *repo_id)
{
Expand All @@ -311,17 +281,12 @@ cf_v2_storage_reserve (const char *repo_id)
if (!trans)
goto rollback;
if (seaf_db_trans_check_for_existence (trans,
"SELECT 1 FROM RepoStorageId WHERE repo_id=? UNION ALL "
"SELECT 1 FROM RepoStorageId WHERE repo_id=? OR storage_id=? UNION ALL "
"SELECT 1 FROM Repo WHERE repo_id=? UNION ALL "
"SELECT 1 FROM RepoTrash WHERE repo_id=? UNION ALL "
"SELECT 1 FROM GarbageRepos WHERE repo_id=?", &db_error,
4, "string", repo_id, "string", repo_id, "string", repo_id, "string", repo_id) ||
5, "string", repo_id, "string", storage_id, "string", repo_id, "string", repo_id, "string", repo_id) ||
db_error ||
/* Immutable safety marker, not a second route or lifecycle engine.
* Prevent an accidentally lost RepoStorageId from looking like CE. */
seaf_db_trans_query (trans,
"INSERT INTO cf_library_storage (repo_key,repo_id,state) VALUES (?,?,'active')",
2, "string", repo_id, "string", repo_id) < 0 ||
seaf_db_trans_query (trans,
"INSERT INTO RepoStorageId (repo_id,storage_id) VALUES (?,?)",
2, "string", repo_id, "string", storage_id) < 0)
Expand Down Expand Up @@ -357,14 +322,9 @@ cf_v2_storage_reserve (const char *repo_id)
int
cf_v2_storage_retire (const char *repo_id)
{
if (!cf_v2_storage_enabled ())
return 0;
int ret = seaf_db_statement_query (seaf->db,
"UPDATE cf_library_storage SET state='retired' WHERE repo_id=?",
1, "string", repo_id);
if (ret >= 0)
route_cache_forget (repo_id);
return ret;
/* RepoStorageId survives trash removal so cold GC uses the same key. */
route_cache_forget(repo_id);
return 0;
}

/* Preserve native hardlinks on one volume; stage on the destination volume
Expand Down
Loading
Loading