~ubuntu-branches/ubuntu/vivid/libzip/vivid

« back to all changes in this revision

Viewing changes to lib/zip_set_name.c

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2014-02-09 13:24:18 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140209132418-t7u2auujqnfb5rvm
Tags: 0.11.2-1
* New upstream release (Closes: #734388).
* Remove patches:
  - fix_open_nonarchive_test.patch - fixed upstream
  - fix_zipconf_path.patch - fixed upstream
  - fix_broken_decrypt.patch - fixed upstream
  - fix_autotools_tests.diff - stolen upstream
* Update debian/control:
  - add unzip to Build-Depends.
  - bump compat to 9
* Update *.install: multiarch paths.
* Update libzip2 symbols file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
  zip_set_name.c -- rename helper function
3
 
  Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
 
3
  Copyright (C) 1999-2012 Dieter Baron and Thomas Klausner
4
4
 
5
5
  This file is part of libzip, a library to manipulate ZIP archives.
6
6
  The authors can be contacted at <libzip@nih.at>
41
41
 
42
42
 
43
43
int
44
 
_zip_set_name(struct zip *za, zip_uint64_t idx, const char *name)
 
44
_zip_set_name(struct zip *za, zip_uint64_t idx, const char *name, zip_flags_t flags)
45
45
{
46
 
    char *s;
 
46
    struct zip_entry *e;
 
47
    struct zip_string *str;
 
48
    int changed;
47
49
    zip_int64_t i;
48
 
    
49
 
    if (idx >= za->nentry || name == NULL) {
 
50
 
 
51
    if (idx >= za->nentry) {
50
52
        _zip_error_set(&za->error, ZIP_ER_INVAL, 0);
51
53
        return -1;
52
54
    }
53
55
 
54
 
    if ((i=_zip_name_locate(za, name, 0, NULL)) != -1 && i != idx) {
 
56
    if (ZIP_IS_RDONLY(za)) {
 
57
        _zip_error_set(&za->error, ZIP_ER_RDONLY, 0);
 
58
        return -1;
 
59
    }
 
60
 
 
61
    if (name && strlen(name) > 0) {
 
62
        /* TODO: check for string too long */
 
63
        if ((str=_zip_string_new((const zip_uint8_t *)name, (zip_uint16_t)strlen(name), flags, &za->error)) == NULL)
 
64
            return -1;
 
65
        if ((flags & ZIP_FL_ENCODING_ALL) == ZIP_FL_ENC_GUESS && _zip_guess_encoding(str, ZIP_ENCODING_UNKNOWN) == ZIP_ENCODING_UTF8_GUESSED)
 
66
            str->encoding = ZIP_ENCODING_UTF8_KNOWN;
 
67
    }
 
68
    else
 
69
        str = NULL;
 
70
 
 
71
    /* TODO: encoding flags needed for CP437? */
 
72
    if ((i=_zip_name_locate(za, name, 0, NULL)) >= 0 && (zip_uint64_t)i != idx) {
 
73
        _zip_string_free(str);
55
74
        _zip_error_set(&za->error, ZIP_ER_EXISTS, 0);
56
75
        return -1;
57
76
    }
58
77
 
59
78
    /* no effective name change */
60
 
    if (i == idx)
 
79
    if (i>=0 && (zip_uint64_t)i == idx) {
 
80
        _zip_string_free(str);
61
81
        return 0;
62
 
    
63
 
    if ((s=strdup(name)) == NULL) {
64
 
        _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
65
 
        return -1;
66
 
    }
67
 
    
68
 
    if (za->entry[idx].state == ZIP_ST_UNCHANGED) 
69
 
        za->entry[idx].state = ZIP_ST_RENAMED;
70
 
 
71
 
    free(za->entry[idx].ch_filename);
72
 
    za->entry[idx].ch_filename = s;
 
82
    }
 
83
 
 
84
    e = za->entry+idx;
 
85
 
 
86
    if (e->changes) {
 
87
        _zip_string_free(e->changes->filename);
 
88
        e->changes->filename = NULL;
 
89
        e->changes->changed &= ~ZIP_DIRENT_FILENAME;
 
90
    }
 
91
 
 
92
    if (e->orig)
 
93
        changed = !_zip_string_equal(e->orig->filename, str);
 
94
    else
 
95
        changed = 1;
 
96
        
 
97
    if (changed) {
 
98
        if (e->changes == NULL) {
 
99
            if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) {
 
100
                _zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
 
101
                _zip_string_free(str);
 
102
                return -1;
 
103
            }
 
104
        }
 
105
        e->changes->filename = str;
 
106
        e->changes->changed |= ZIP_DIRENT_FILENAME;
 
107
    }
 
108
    else {
 
109
        _zip_string_free(str);
 
110
        if (e->changes && e->changes->changed == 0) {
 
111
            _zip_dirent_free(e->changes);
 
112
            e->changes = NULL;
 
113
        }
 
114
    }
73
115
 
74
116
    return 0;
75
117
}