@@ -3601,8 +3601,49 @@ int cbm_yaml_upsert_mapping_entry(const char *file_path, const char *section_key
36013601 return result == 0 && release_result == 0 ? 0 : YAML_ERROR ;
36023602}
36033603
3604+ /* Removal is idempotent: a config that does not exist has nothing to remove.
3605+ * The JSON removers already answer OK here (cbm_json_like_read_document's
3606+ * "missing" result); the YAML removers instead took the lock first, and the
3607+ * lock directory is created beside the target — so with the parent directory
3608+ * absent (`~/.hermes/` never created because Hermes was detected by its
3609+ * binary on PATH, not by its home) every uninstall reported
3610+ * "does not exist or cannot be inspected" and refused to remove the
3611+ * executable. Only a true ENOENT short-circuits: a dangling symlink still
3612+ * reaches the reader and its byte-identical rejection. */
3613+ static bool yaml_remove_target_absent (const char * path ) {
3614+ if (!path ) {
3615+ return false;
3616+ }
3617+ #ifdef _WIN32
3618+ /* FILE_FLAG_OPEN_REPARSE_POINT, as in toml_read_file: opens the link
3619+ * itself rather than following it, so a dangling symlink does NOT count as
3620+ * absent and still reaches the reader — matching the POSIX branch, where
3621+ * lstat does not follow either. */
3622+ wchar_t * wide = cbm_utf8_to_wide (path );
3623+ if (!wide ) {
3624+ return false;
3625+ }
3626+ HANDLE handle = CreateFileW (
3627+ wide , GENERIC_READ , FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
3628+ OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OPEN_REPARSE_POINT , NULL );
3629+ DWORD error = handle == INVALID_HANDLE_VALUE ? GetLastError () : 0 ;
3630+ free (wide );
3631+ if (handle != INVALID_HANDLE_VALUE ) {
3632+ CloseHandle (handle );
3633+ return false;
3634+ }
3635+ return error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND ;
3636+ #else
3637+ struct stat path_state ;
3638+ return lstat (path , & path_state ) != 0 && errno == ENOENT ;
3639+ #endif
3640+ }
3641+
36043642int cbm_yaml_remove_mapping_entry (const char * file_path , const char * section_key ,
36053643 const char * entry_key ) {
3644+ if (yaml_remove_target_absent (file_path )) {
3645+ return 0 ;
3646+ }
36063647 yaml_config_lock_t lock ;
36073648 if (yaml_lock_acquire (file_path , & lock ) != 0 ) {
36083649 return YAML_ERROR ;
@@ -3626,6 +3667,9 @@ int cbm_yaml_upsert_owned_mapping_entry(const char *file_path, const char *secti
36263667
36273668int cbm_yaml_remove_owned_mapping_entry (const char * file_path , const char * section_key ,
36283669 const char * entry_key , const char * canonical_entry_block ) {
3670+ if (yaml_remove_target_absent (file_path )) {
3671+ return CBM_YAML_IDENTITY_EDIT_OK ;
3672+ }
36293673 yaml_config_lock_t lock ;
36303674 if (yaml_lock_acquire (file_path , & lock ) != 0 ) {
36313675 return CBM_YAML_IDENTITY_EDIT_ERROR ;
@@ -3653,6 +3697,9 @@ int cbm_yaml_upsert_mapping_sequence_item(const char *file_path, const char *con
36533697int cbm_yaml_remove_mapping_sequence_item (const char * file_path , const char * const * sequence_path ,
36543698 size_t sequence_path_len , const char * identity_key ,
36553699 const char * identity_scalar , const char * canonical_item ) {
3700+ if (yaml_remove_target_absent (file_path )) {
3701+ return CBM_YAML_IDENTITY_EDIT_OK ;
3702+ }
36563703 yaml_config_lock_t lock ;
36573704 if (yaml_lock_acquire (file_path , & lock ) != 0 ) {
36583705 return CBM_YAML_IDENTITY_EDIT_ERROR ;
@@ -3675,6 +3722,9 @@ int cbm_yaml_upsert_string_list_item(const char *file_path, const char *key, con
36753722}
36763723
36773724int cbm_yaml_remove_string_list_item (const char * file_path , const char * key , const char * item ) {
3725+ if (yaml_remove_target_absent (file_path )) {
3726+ return 0 ;
3727+ }
36783728 yaml_config_lock_t lock ;
36793729 if (yaml_lock_acquire (file_path , & lock ) != 0 ) {
36803730 return YAML_ERROR ;
0 commit comments