Skip to content

Commit 0d01415

Browse files
committed
system/nxpkg: simplify lifecycle cleanup
Use shared cleanup paths and keep the command wrappers small. Assisted-by: OpenAI Codex:gpt-5.6-sol Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent 51b68e8 commit 0d01415

2 files changed

Lines changed: 15 additions & 72 deletions

File tree

system/nxpkg/pkg_install.c

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ static int pkg_install_acquire_lock(FAR const char *name, FAR char *path,
6969
* Name: pkg_install_acquire_installed_lock
7070
*
7171
* Description:
72-
* Acquire the global installed-database lock. This serializes the
73-
* read-modify-write sequence used by install, uninstall, and rollback.
72+
* Serialize installed database updates.
7473
*
7574
****************************************************************************/
7675

@@ -134,12 +133,7 @@ static int pkg_install_prune_oldest_version(
134133
size_t victim = entry->version_count;
135134
size_t i;
136135

137-
/* Versions are appended in install order, so the lowest index that
138-
* isn't the active ("current") or rollback ("previous") version is the
139-
* oldest one safe to drop. Without this, a package updated more than
140-
* PKG_INSTALLED_VERSIONS_MAX times becomes permanently un-installable
141-
* (pkg_install_add_version would just fail forever).
142-
*/
136+
/* Keep the current and rollback versions when pruning. */
143137

144138
for (i = 0; i < entry->version_count; i++)
145139
{
@@ -156,16 +150,7 @@ static int pkg_install_prune_oldest_version(
156150
return -E2BIG;
157151
}
158152

159-
/* Deleting the pruned version's on-disk directory here, before this
160-
* in-memory db update is even durably saved, left a real inconsistency
161-
* window: if pkg_metadata_save_installed() subsequently failed (full
162-
* SD card, I/O error), the payload was already gone but the last
163-
* successfully-saved instpkg.jsn could still list that version as
164-
* installed. Hand the victim's version string back to the caller
165-
* instead, so it can defer the actual directory removal until after
166-
* the save succeeds - mirroring how this file already treats the
167-
* installed db as authoritative everywhere else.
168-
*/
153+
/* Let the caller delete this version after committing the database. */
169154

170155
snprintf(pruned_version, pruned_version_size, "%s",
171156
entry->versions[victim]);
@@ -426,7 +411,7 @@ int pkg_install(FAR const char *name)
426411
goto errout;
427412
}
428413

429-
ret = pkg_acquire_source(source, tmp, lock);
414+
ret = pkg_acquire_source(source, tmp);
430415
if (ret < 0)
431416
{
432417
pkg_error("acquire source failed: %d", ret);
@@ -583,12 +568,7 @@ int pkg_install(FAR const char *name)
583568
goto errout;
584569
}
585570

586-
/* Only remove the pruned version's payload directory now that the db
587-
* update naming it gone is durably saved - see
588-
* pkg_install_prune_oldest_version()'s comment for why doing this
589-
* before the save could leave a saved db entry pointing at an
590-
* already-deleted version if the save had failed instead.
591-
*/
571+
/* Remove the pruned payload after committing the database. */
592572

593573
if (pruned_version[0] != '\0')
594574
{
@@ -598,10 +578,7 @@ int pkg_install(FAR const char *name)
598578
ret = pkg_install_write_pointers(installed, manifest->name);
599579
if (ret < 0)
600580
{
601-
/* The installed database is authoritative. The pointer files are
602-
* convenience mirrors and can be reconstructed from it, so failure
603-
* to refresh one must not roll back a durably committed install.
604-
*/
581+
/* Pointer files can be rebuilt from the installed database. */
605582

606583
pkg_error("unable to refresh current/previous pointers: %d", ret);
607584
}
@@ -612,11 +589,7 @@ int pkg_install(FAR const char *name)
612589
ret = pkg_txn_write_state(name, PKG_TXN_ACTIVATED);
613590
if (ret < 0)
614591
{
615-
/* The installed database has already been committed. Do not enter
616-
* the failure cleanup path here: it could remove a payload referenced
617-
* by that database. Transaction state is recovery bookkeeping and
618-
* can be cleared below.
619-
*/
592+
/* Do not remove payloads after the database commit. */
620593

621594
pkg_error("txn state activated failed: %d", ret);
622595
}
@@ -644,11 +617,7 @@ int pkg_install(FAR const char *name)
644617
pkg_store_remove_file(tmp);
645618
}
646619

647-
/* Reclaim whatever was staged into the version directory (payload,
648-
* manifest.jsn) before this failure - otherwise every failure past
649-
* this point leaves a permanently orphaned, never-activated version
650-
* directory with no way to reclaim it short of "remove".
651-
*/
620+
/* Remove a version directory created by this failed install. */
652621

653622
if (version_dir_created)
654623
{
@@ -682,9 +651,7 @@ int pkg_install(FAR const char *name)
682651
pkg_free(lock);
683652
pkg_free(installed_lock);
684653

685-
/* Callers such as nxstore use the errno to tell a download failure from
686-
* a digest mismatch or an incompatible package.
687-
*/
654+
/* Preserve negative errno values for library callers. */
688655

689656
return ret;
690657
}
@@ -733,13 +700,7 @@ int pkg_list(FAR FILE *stream)
733700
* Name: pkg_uninstall
734701
*
735702
* Description:
736-
* Remove every installed version of "name": their version directories
737-
* (payload + manifest.jsn), the current/previous pointer files, any
738-
* leftover txn.tx, the entry in the shared installed-packages database,
739-
* and finally the now-empty package root directory. Refuses to run
740-
* while an install/update for the same package is in flight (a live
741-
* lock.lk), since removing the store out from under it would corrupt
742-
* whatever it's mid-writing.
703+
* Remove a package and all of its installed versions.
743704
*
744705
****************************************************************************/
745706

@@ -804,10 +765,7 @@ int pkg_uninstall(FAR const char *name)
804765
goto errout_with_installed_lock;
805766
}
806767

807-
/* Drop this entry from the authoritative database before removing its
808-
* payloads. A power loss can then leave reclaimable orphan files, but
809-
* never a database entry that points at a payload already deleted.
810-
*/
768+
/* Commit removal before deleting payloads. */
811769

812770
removed = *entry;
813771
index = (size_t)(entry - db->entries);
@@ -872,11 +830,7 @@ int pkg_uninstall(FAR const char *name)
872830
* Name: pkg_rollback
873831
*
874832
* Description:
875-
* Swap "name"'s current and previous installed versions. The swap (as
876-
* opposed to just clearing "previous") lets a second rollback undo the
877-
* first. Verifies the rollback target's version directory still
878-
* exists on disk before committing any state change, and refuses to
879-
* run while an install/update for the same package is in flight.
833+
* Swap the current and previous installed versions.
880834
*
881835
****************************************************************************/
882836

@@ -978,10 +932,7 @@ int pkg_rollback(FAR const char *name)
978932
goto errout_with_installed_lock;
979933
}
980934

981-
/* The installed database is authoritative, so commit it first. Refresh
982-
* the current/previous pointer files afterwards as convenience mirrors;
983-
* they can be reconstructed from the database if either write fails.
984-
*/
935+
/* Commit the database before refreshing pointer files. */
985936

986937
ret = pkg_metadata_save_installed(db);
987938
if (ret < 0)

system/nxpkg/pkg_main.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,12 @@ int main(int argc, FAR char *argv[])
7878
return EXIT_FAILURE;
7979
}
8080

81-
/* pkg_install() returns a real negative errno on the meaningful
82-
* pipeline failures (nxstore uses that directly), not just
83-
* EXIT_SUCCESS/EXIT_FAILURE - normalize to a plain 0/1 shell exit
84-
* status here.
85-
*/
81+
/* Convert library errors to a shell exit status. */
8682

8783
return pkg_install(argv[2]) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
8884
}
8985

90-
/* "update" is a package name resolving to whatever version is latest
91-
* in the local index - pkg_install() already handles the "already
92-
* installed at a different version" transition transparently via
93-
* pkg_install_update_installed(), so no separate code path is needed.
94-
*/
86+
/* Installing the latest indexed version also performs an update. */
9587

9688
if (strcmp(cmd, "update") == 0)
9789
{

0 commit comments

Comments
 (0)