Ticket #873: filename_escape.patch

File filename_escape.patch, 3.2 KB (added by savago, 4 years ago)

Escapes invalid characters from ID used as filename

  • src/filename_scape.h

     
     1#ifndef __FILENAME_SCAPER__ 
     2#define __FILENAME_SCAPER__ 
     3 
     4/** 
     5 * @file   filename_scape.h 
     6 * @author Adenilson Cavalcanti <savagobr@yahoo.com> 
     7 * @date   Tue Oct 21 15:31:10 2008 
     8 * 
     9 * @brief  An auxiliary module to scape invalid characters from filename. 
     10 * 
     11 * This code is free software; you can redistribute it and/or 
     12 * modify it under the terms of the GNU Lesser General Public 
     13 * License as published by the Free Software Foundation; either 
     14 * version 2.1 of the License, or (at your option) any later version. 
     15 * 
     16 * This library is distributed in the hope that it will be useful, 
     17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
     18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
     19 * Lesser General Public License for more details. 
     20 * 
     21 * You should have received a copy of the GNU Lesser General Public 
     22 * License along with this library; if not, write to the Free Software 
     23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA 
     24 
     25 */ 
     26 
     27static const char reserved_chars[] = { '/', '!', '?', ':', '*', '\\', '>', '<', '@' }; 
     28static const int reserved_count = 9; 
     29static const char scaper = '_'; 
     30 
     31static void filename_scape_characters(char *input) 
     32{ 
     33 
     34        int i; 
     35 
     36        while (*input) { 
     37                for (i = 0; i < reserved_count; ++i) 
     38                        if (*input ==  reserved_chars[i]) { 
     39                                *input++ = scaper; 
     40                                goto done; 
     41                        } 
     42                ++input; 
     43        done: 
     44                ; 
     45        } 
     46 
     47} 
     48 
     49#endif 
  • src/file_sync.c

     
    1919 */ 
    2020 
    2121#include "file_sync.h" 
     22#include "filename_scape.h" 
    2223#include <opensync/file.h> 
    2324#include <opensync/opensync-version.h> 
    2425#include <assert.h> 
     
    143144        char *buffer = NULL; 
    144145        unsigned int size = 0; 
    145146         
    146         char *filename = g_strdup_printf ("%s/%s", dir->path, osync_change_get_uid(change)); 
    147                          
     147        char *filename = NULL, *tmp = NULL; 
     148        if (!(tmp = strdup(osync_change_get_uid(change)))) { 
     149                osync_trace(TRACE_EXIT_ERROR, "%s", __func__); 
     150                return; 
     151        } 
     152        filename_scape_characters(tmp); 
     153 
     154        filename = g_strdup_printf ("%s/%s", dir->path, tmp); 
     155        free(tmp); 
     156 
    148157        switch (osync_change_get_changetype(change)) { 
    149158                case OSYNC_CHANGE_TYPE_DELETED: 
    150159                        if (!remove(filename) == 0) { 
     
    412421        OSyncObjTypeSink *sink = osync_plugin_info_get_sink(info); 
    413422        OSyncFileDir *dir = osync_objtype_sink_get_userdata(sink); 
    414423         
    415         char *filename = NULL; 
     424        char *filename = NULL, *tmp; 
    416425         
    417426        if (!osync_filesync_write(data, info, ctx, change)) { 
    418427                osync_trace(TRACE_EXIT_ERROR, "%s", __func__); 
    419428                return; 
    420429        } 
    421          
    422         filename = g_strdup_printf ("%s/%s", dir->path, osync_change_get_uid(change)); 
     430        if (!(tmp = strdup(osync_change_get_uid(change)))) { 
     431                osync_trace(TRACE_EXIT_ERROR, "%s", __func__); 
     432                return; 
     433        } 
     434        filename_scape_characters(tmp); 
     435 
     436        filename = g_strdup_printf ("%s/%s", dir->path, tmp); 
     437        free(tmp); 
    423438        char *hash = NULL; 
    424439         
    425440        if (osync_change_get_changetype(change) != OSYNC_CHANGE_TYPE_DELETED) {