From 08c61f630356d2d7371fb9131f5a50f917c36778 Mon Sep 17 00:00:00 2001 From: madmurphy Date: Wed, 28 Apr 2021 10:04:23 +0200 Subject: [PATCH] Commit #3 --- ChangeLog | 9 +++ README | 18 ++++-- configure.ac | 9 +-- package.json | 2 +- po/it.po | 44 ++++++++------- src/nautilus-hide.c | 132 +++++++++++++++++++++++++++++++------------- 6 files changed, 148 insertions(+), 66 deletions(-) diff --git a/ChangeLog b/ChangeLog index c397706..d13e458 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,14 @@ Change Log ========== +## 0.2.0 (2021-04-28) + +Changes: + +* The extension now checks for the database of hidden files' read/write access +* Code review + + ## 0.1.1 (2021-04-26) Changes: @@ -13,3 +21,4 @@ Changes: ## 0.1.0 (2021-04-25) **Nautilus Hide** has been published. + diff --git a/README b/README index 75f6287..9867af8 100644 --- a/README +++ b/README @@ -34,18 +34,18 @@ ones are: 3. “Unhide file(s)” 4. “Unhide the selected file(s)” -Other translatable strings are only debug messages sent to the standard error -stream and can be left untraslated. +Other translatable strings are only debug messages sent to the error stream and +can be left untraslated. First thing to do is to make sure that `gettext` and `intltool` are installed on your system. Then clone this repository and launch the following four commands: -``` +``` sh cd nautilus-hide/po intltool-update -p msginit -l nl -rm nautilus-hide.pot +rm NautilusHide.pot ``` Now edit the `po/LINGUAS` file and add a new line containing the abbreviated @@ -54,6 +54,16 @@ name of the new language (`nl`). Finally, open the `po/nl.po` file and translate the strings above. All you have to do now is to commit your changes. +If you want to update an existing Dutch translation to the most recent changes +in the extension source code, launch + +``` sh +cd nautilus-hide/po +intltool-update -p +mv nl.po nl.po.bak && msgmerge nl.po.bak NautilusHide.pot > nl.po +rm NautilusHide.pot nl.po.bak +``` + For any issue, [drop me a message][1]. diff --git a/configure.ac b/configure.ac index 01193c5..e16af3a 100644 --- a/configure.ac +++ b/configure.ac @@ -9,7 +9,7 @@ dnl *************************************************************************** AC_PREREQ([2.69]) AC_INIT([Nautilus Hide], - [0.1.1], + [0.2.0], [madmurphy333@gmail.com], [nautilus-hide], [https://gitlab.gnome.org/madmurphy/nautilus-hide]) @@ -49,12 +49,13 @@ IT_PROG_INTLTOOL([0.35.0]) AM_CONDITIONAL([NLS_ENABLED], [test "x${USE_NLS}" != xno]) -AM_COND_IF([NLS_ENABLED], - [AC_SUBST([GETTEXT_PACKAGE], AC_PACKAGE_TARNAME) +AM_COND_IF([NLS_ENABLED], [ + AC_SUBST([GETTEXT_PACKAGE], AC_PACKAGE_TARNAME) AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["${GETTEXT_PACKAGE}"], [GETTEXT package name]) AM_GLIB_GNU_GETTEXT - AM_GLIB_DEFINE_LOCALEDIR([NAUTILUS_HIDE_LOCALEDIR])]) + AM_GLIB_DEFINE_LOCALEDIR([NAUTILUS_HIDE_LOCALEDIR]) +]) dnl *************************************************************************** diff --git a/package.json b/package.json index a52e32c..ff5efff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nautilus-hide", - "version": "0.1.1", + "version": "0.2.0", "description": "A simple Nautilus extension that adds \"Hide\" and \"Unhide\" to Nautilus right-click menu", "homepage": "https://gitlab.gnome.org/madmurphy/nautilus-hide", "author": "madmurphy333@gmail.com", diff --git a/po/it.po b/po/it.po index 3a45726..b8d7a5e 100644 --- a/po/it.po +++ b/po/it.po @@ -7,8 +7,8 @@ msgid "" msgstr "" "Project-Id-Version: 0.1.1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-04-26 18:05+0200\n" -"PO-Revision-Date: 2021-04-26 18:05+0200\n" +"POT-Creation-Date: 2021-04-28 10:03+0200\n" +"PO-Revision-Date: 2021-04-28 10:03+0200\n" "Last-Translator: FULL NAME \n" "Language-Team: Italian \n" "Language: it\n" @@ -17,33 +17,37 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -#: ../src/nautilus-hide.c:96 +#: ../src/nautilus-hide.c:99 msgid "Could not close the database of hidden files" msgstr "Impossibile chiudere il database dei file nascosti" -#: ../src/nautilus-hide.c:116 ../src/nautilus-hide.c:133 -#: ../src/nautilus-hide.c:147 ../src/nautilus-hide.c:205 -#: ../src/nautilus-hide.c:256 ../src/nautilus-hide.c:308 +#: ../src/nautilus-hide.c:119 ../src/nautilus-hide.c:136 +#: ../src/nautilus-hide.c:150 ../src/nautilus-hide.c:243 +#: ../src/nautilus-hide.c:296 ../src/nautilus-hide.c:348 msgid "Error allocating memory" msgstr "Errore nell'allocare memoria" -#: ../src/nautilus-hide.c:179 +#: ../src/nautilus-hide.c:201 +msgid "Error attempting to access the database of hidden files" +msgstr "Impossibile accedere al database dei file nascosti" + +#: ../src/nautilus-hide.c:215 msgid "Could not calculate the size of the database of hidden files" msgstr "Impossibile calcolare le dimensioni del database dei file nascosti" -#: ../src/nautilus-hide.c:191 +#: ../src/nautilus-hide.c:228 msgid "Database of hidden files is too big" msgstr "Il database dei file nascosti è troppo grande" -#: ../src/nautilus-hide.c:219 +#: ../src/nautilus-hide.c:257 msgid "I/O error while accessing database of hidden files" msgstr "Errore I/O nell'accedere al database dei file nascosti" -#: ../src/nautilus-hide.c:390 +#: ../src/nautilus-hide.c:430 msgid "No files were selected to be hidden" msgstr "Non ci sono file selezionati da nascondere" -#: ../src/nautilus-hide.c:404 +#: ../src/nautilus-hide.c:444 msgid "" "Could not hide the selected files, error attempting to group them according " "to their parent directories" @@ -51,18 +55,18 @@ msgstr "" "Impossibile nascondere i file selezionati, errore nel tentativo di " "raggrupparli in base alla loro directory di appartenenza" -#: ../src/nautilus-hide.c:432 +#: ../src/nautilus-hide.c:472 msgid "" "Could not hide the selected files, error attempting to edit the database" msgstr "" "Impossibile nascondere i file selezionati, errore nel tentativo di " "modificare il database" -#: ../src/nautilus-hide.c:549 +#: ../src/nautilus-hide.c:589 msgid "No files were selected to be unhidden" msgstr "Non ci sono file selezionati da rendere visibili" -#: ../src/nautilus-hide.c:563 +#: ../src/nautilus-hide.c:603 msgid "" "Could not unhide the selected files, error attempting to group them " "according to their parent directories" @@ -70,33 +74,33 @@ msgstr "" "Impossibile rendere visibili i file selezionati, errore nel tentativo di " "raggrupparli in base alla loro directory di appartenenza" -#: ../src/nautilus-hide.c:604 +#: ../src/nautilus-hide.c:643 msgid "" "Could not unhide the selected files, error attempting to edit the database" msgstr "" "Impossibile rendere visibili i file selezionati, errore nel tentativo di " "modificare il database" -#: ../src/nautilus-hide.c:835 +#: ../src/nautilus-hide.c:893 msgid "_Hide file" msgid_plural "_Hide files" msgstr[0] "_Nascondi" msgstr[1] "_Nascondi i file" -#: ../src/nautilus-hide.c:836 +#: ../src/nautilus-hide.c:894 msgid "Hide the selected file" msgid_plural "Hide the selected files" msgstr[0] "Nascondi il file selezionato" msgstr[1] "Nascondi i file selezionati" -#: ../src/nautilus-hide.c:863 +#: ../src/nautilus-hide.c:921 msgid "_Unhide file" msgid_plural "_Unhide files" msgstr[0] "Ripristina _visibilità" msgstr[1] "Ripristina _visibilità dei file" -#: ../src/nautilus-hide.c:864 -msgid "Unhide the selected files" +#: ../src/nautilus-hide.c:922 +msgid "Unhide the selected file" msgid_plural "Unhide the selected files" msgstr[0] "Rendi visibile il file selezionato" msgstr[1] "Rendi visibili i file selezionati" diff --git a/src/nautilus-hide.c b/src/nautilus-hide.c index 4fe341e..29c1675 100644 --- a/src/nautilus-hide.c +++ b/src/nautilus-hide.c @@ -32,13 +32,12 @@ #include #include #include +#include #include #ifdef ENABLE_NLS #include #include -#define __(STRING_S, STRING_PL, NUM) ((char *) \ - dngettext(GETTEXT_PACKAGE, STRING_S, STRING_PL, NUM)) #else #define _(STRING) ((char *) (STRING)) #define g_dngettext(DOMAIN, STRING1, STRING2, NUM) \ @@ -54,6 +53,9 @@ */ +#define NH_R_OK 1 +#define NH_W_OK 2 + typedef struct { GObject parent_slot; } NautilusHide; @@ -67,6 +69,7 @@ typedef struct ssofwcp_T { char * hdb_path; char ** hdb_entries; GList * selection; + guint8 db_access; } ssofwcp; static GType provider_types[1]; @@ -119,7 +122,7 @@ static ssofwcp * ssofwcp_new_with_dir ( } - const size_t dp_len = strlen(dpath); + const gsize dp_len = strlen(dpath); new_ssofwcp->hdb_entries = NULL; new_ssofwcp->selection = NULL; @@ -161,10 +164,43 @@ static ssofwcp * ssofwcp_new_with_dir ( ); new_ssofwcp->hdb_path[dp_len] = '/'; + new_ssofwcp->db_access = ( + !g_access(new_ssofwcp->hdb_path, R_OK) || ( + g_access(new_ssofwcp->hdb_path, F_OK) && + !g_access(new_ssofwcp->directory, X_OK) + ) ? + NH_R_OK + : + 0 + ) | ( + ( + g_access(new_ssofwcp->hdb_path, F_OK) ? + !g_access(new_ssofwcp->directory, W_OK) + : + !g_access(new_ssofwcp->hdb_path, W_OK) + ) ? + NH_W_OK + : + 0 + ); + + + if (g_access(new_ssofwcp->hdb_path, R_OK)) { + + return new_ssofwcp; + + } + FILE * const hdb_file = fopen(new_ssofwcp->hdb_path, "rb"); if (!hdb_file) { + fprintf( + stderr, + "Nautilus Hide: %s (database: %s)\n", + _("Error attempting to access the database of hidden files"), + new_ssofwcp->hdb_path + ); return new_ssofwcp; } @@ -175,8 +211,9 @@ static ssofwcp * ssofwcp_new_with_dir ( fprintf( stderr, - "Nautilus Hide: %s\n", - _("Could not calculate the size of the database of hidden files") + "Nautilus Hide: %s (database: %s)\n", + _("Could not calculate the size of the database of hidden files"), + new_ssofwcp->hdb_path ); close_hdb_file(hdb_file, new_ssofwcp->hdb_path); return new_ssofwcp; @@ -187,8 +224,9 @@ static ssofwcp * ssofwcp_new_with_dir ( fprintf( stderr, - "Nautilus Hide: %s\n", - _("Database of hidden files is too big") + "Nautilus Hide: %s (database: %s)\n", + _("Database of hidden files is too big"), + new_ssofwcp->hdb_path ); close_hdb_file(hdb_file, new_ssofwcp->hdb_path); return new_ssofwcp; @@ -215,8 +253,9 @@ static ssofwcp * ssofwcp_new_with_dir ( fprintf( stderr, - "Nautilus Hide: %s\n", - _("I/O error while accessing database of hidden files") + "Nautilus Hide: %s (database: %s)\n", + _("I/O error while accessing database of hidden files"), + new_ssofwcp->hdb_path ); free(cache); close_hdb_file(hdb_file, new_ssofwcp->hdb_path); @@ -225,8 +264,9 @@ static ssofwcp * ssofwcp_new_with_dir ( } close_hdb_file(hdb_file, new_ssofwcp->hdb_path); + cache[file_size] = '\0'; - size_t idx = 0, line_start = 0, fileno = 1; + gsize idx = 0, line_start = 0, fileno = 1; for (; idx < file_size; idx++) { @@ -246,7 +286,7 @@ static ssofwcp * ssofwcp_new_with_dir ( } - new_ssofwcp->hdb_entries = malloc(sizeof(char *) * fileno + file_size); + new_ssofwcp->hdb_entries = malloc(sizeof(char *) * fileno + file_size + 1); if (!new_ssofwcp->hdb_entries) { @@ -261,7 +301,7 @@ static ssofwcp * ssofwcp_new_with_dir ( } char * list_buffer = ((char *) new_ssofwcp->hdb_entries) + sizeof(char *) * fileno; - memcpy(list_buffer, cache, file_size); + memcpy(list_buffer, cache, file_size + 1); free(cache); new_ssofwcp->hdb_entries[fileno - 1] = NULL; @@ -292,7 +332,7 @@ static GList * ordered_file_selection_new ( GList * ordered_selection = NULL; ssofwcp * new_ssofwcp; - gchar * dpath; + char * dpath; for (GList * d_iter, * s_iter = file_selection; s_iter; s_iter = s_iter->next) { @@ -317,7 +357,7 @@ static GList * ordered_file_selection_new ( if (!strcmp(subselection->directory, dpath)) { - /* The parent directory of this file had already been indicized */ + /* The parent directory of this file has already been indicized */ subselection->selection = g_list_prepend( subselection->selection, @@ -410,7 +450,7 @@ static void nautilus_hide_push_files ( } char * fname; - size_t idx; + gsize idx; FILE * hdb_file_w; for ( @@ -568,8 +608,8 @@ static void nautilus_hide_pop_files ( } - bool b_erase_hdb_file; - size_t idx; + bool b_keep_hdb_file; + gsize idx; char * fname; FILE * hdb_file_w; @@ -585,8 +625,7 @@ static void nautilus_hide_pop_files ( /* Database file is empty or missing */ - remove(subselection->hdb_path); - continue; + goto remove_db; } @@ -611,11 +650,11 @@ static void nautilus_hide_pop_files ( } idx = 0; - b_erase_hdb_file = true; + b_keep_hdb_file = false; /* \ /\ - \ */ check_entry: /* \ + \ */ check_member: /* \ \/ ______________________ \ */ @@ -626,7 +665,7 @@ static void nautilus_hide_pop_files ( /* This is already a dotfile */ idx++; - goto check_entry; + goto check_member; } @@ -636,11 +675,11 @@ static void nautilus_hide_pop_files ( if (!strcmp(fname, subselection->hdb_entries[idx])) { - /* This database entry has been marked for deletion */ + /* This database entry is marked for deletion */ idx++; g_free(fname); - goto check_entry; + goto check_member; } @@ -649,7 +688,7 @@ static void nautilus_hide_pop_files ( } - /* This database entry is not eligible for deletion */ + /* This database entry is not marked for deletion */ fwrite( subselection->hdb_entries[idx], @@ -657,26 +696,37 @@ static void nautilus_hide_pop_files ( strlen(subselection->hdb_entries[idx]), hdb_file_w ); fputc('\n', hdb_file_w); - b_erase_hdb_file = false; + b_keep_hdb_file = true; idx++; - goto check_entry; + goto check_member; } close_hdb_file(hdb_file_w, subselection->hdb_path); - if (b_erase_hdb_file) { + if (b_keep_hdb_file) { + + continue; + + } + + + /* \ /\ + \ */ remove_db: /* \ + \/ ______________________ \ */ + + + /* Database is empty, let's remove the file */ - /* Database is empty, let's remove the file */ + if (!g_access(subselection->directory, W_OK)) { - remove(subselection->hdb_path); + g_remove(subselection->hdb_path); } #undef subselection - } g_list_free_full(ordered_selection, ssofwcp_destroy); @@ -721,10 +771,10 @@ static GList * nautilus_hide_get_file_items ( } - const unsigned int sel_len = g_list_length(file_selection); + const unsigned int sellen = g_list_length(file_selection); bool b_show_hide = false; bool b_show_unhide = false; - size_t idx; + gsize idx; char * fname; for ( @@ -735,6 +785,14 @@ static GList * nautilus_hide_get_file_items ( #define subselection ((ssofwcp *) o_iter->data) + if ((subselection->db_access & (NH_R_OK | NH_W_OK)) != (NH_R_OK | NH_W_OK)) { + + /* At least one database has not enough permissions */ + + return NULL; + + } + if (subselection->hdb_entries && *subselection->hdb_entries) { /* Database file is not empty */ @@ -832,8 +890,8 @@ static GList * nautilus_hide_get_file_items ( NautilusMenuItem * const menu_item_hide = nautilus_menu_item_new( "NautilusHide::hide", - g_dngettext(GETTEXT_PACKAGE, "_Hide file", "_Hide files", sel_len), - g_dngettext(GETTEXT_PACKAGE, "Hide the selected file", "Hide the selected files", sel_len), + g_dngettext(GETTEXT_PACKAGE, "_Hide file", "_Hide files", sellen), + g_dngettext(GETTEXT_PACKAGE, "Hide the selected file", "Hide the selected files", sellen), NULL /* icon name or `NULL` */ ); @@ -860,8 +918,8 @@ static GList * nautilus_hide_get_file_items ( NautilusMenuItem * const menu_item_unhide = nautilus_menu_item_new( "NautilusHide::unhide", - g_dngettext(GETTEXT_PACKAGE, "_Unhide file", "_Unhide files", sel_len), - g_dngettext(GETTEXT_PACKAGE, "Unhide the selected files", "Unhide the selected files", sel_len), + g_dngettext(GETTEXT_PACKAGE, "_Unhide file", "_Unhide files", sellen), + g_dngettext(GETTEXT_PACKAGE, "Unhide the selected file", "Unhide the selected files", sellen), NULL /* icon name or `NULL` */ );