File: gcab-file.c

package info (click to toggle)
gcab 0.7-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 2,512 kB
  • sloc: sh: 5,923; ansic: 3,122; makefile: 232
file content (333 lines) | stat: -rw-r--r-- 7,805 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "gcab-priv.h"
#include "gcab-file.h"

/**
 * SECTION:gcab-file
 * @title: GCabFile
 * @short_description: A file contained in the Cabinet
 * @see_also: #GCabFolder
 * @stability: Stable
 * @include: libgcab.h
 *
 * A GCabFile is a handle to a file inside a Cabinet archive.
 * It can either be a file that is already within an exisiting
 * archive, or a file that reference a file on disk that will be used
 * for a new archive creation. In the later case, gcab_file_get_file()
 * must return a valid handle.
 */

struct _GCabFileClass
{
    GObjectClass parent_class;
};

enum {
    PROP_0,

    PROP_NAME,
    PROP_FILE,
};

G_DEFINE_TYPE (GCabFile, gcab_file, G_TYPE_OBJECT);

static void
gcab_file_init (GCabFile *self)
{
}

static void
gcab_file_finalize (GObject *object)
{
    GCabFile *self = GCAB_FILE (object);

    g_object_unref (self->file);
    g_free (self->name);
    g_free (self->extract_name);

    G_OBJECT_CLASS (gcab_file_parent_class)->finalize (object);
}

static void
gcab_file_set_name (GCabFile *self, const gchar *name)
{
    gchar *fname = g_strdup (name);

    /* assuming that on win32 we don't get unix paths */
#ifndef G_OS_WIN32
    if (fname) {
        int i, len = strlen (fname);
        for (i = 0; i < len; i++)
            if (fname[i] == '/')
                fname[i] = '\\';
    }
#endif

    g_free (self->name);
    self->name = fname;
}

static void
gcab_file_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    g_return_if_fail (GCAB_IS_FILE (object));
    GCabFile *self = GCAB_FILE (object);

    switch (prop_id) {
    case PROP_NAME:
        gcab_file_set_name (self, g_value_get_string (value));
        break;
    case PROP_FILE:
        self->file = g_value_dup_object (value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gcab_file_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
    g_return_if_fail (GCAB_IS_FILE (object));
    GCabFile *self = GCAB_FILE(object);

    switch (prop_id) {
    case PROP_NAME:
        g_value_set_string (value, self->name);
        break;
    case PROP_FILE:
        g_value_set_object (value, self->file);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gcab_file_class_init (GCabFileClass *klass)
{
    GObjectClass* object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = gcab_file_finalize;
    object_class->set_property = gcab_file_set_property;
    object_class->get_property = gcab_file_get_property;

    g_object_class_install_property (object_class, PROP_NAME,
        g_param_spec_string ("name", "name", "name", NULL,
                             G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                             G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (object_class, PROP_FILE,
        g_param_spec_object ("file", "file", "file", G_TYPE_FILE,
                             G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
                             G_PARAM_STATIC_STRINGS));
}

G_GNUC_INTERNAL gboolean
gcab_file_update_info (GCabFile *self, GFileInfo *info)
{
    GTimeVal tv;
    time_t time;
    struct tm *m; // Use GDateTime when 2.26 in RHEL6

    g_return_val_if_fail (GCAB_IS_FILE (self), FALSE);
    g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);

    g_file_info_get_modification_time (info, &tv);
    time = tv.tv_sec;
    m = localtime (&time);

    self->cfile.name = self->name;
    self->cfile.usize = g_file_info_get_size (info);
    self->cfile.fattr = GCAB_FILE_ATTRIBUTE_ARCH;
    self->cfile.date = ((m->tm_year + 1900 - 1980 ) << 9 ) +
        ((m->tm_mon+1) << 5 ) + (m->tm_mday);
    self->cfile.time = (m->tm_hour << 11) + (m->tm_min << 5) + (m->tm_sec / 2);

    return TRUE;
}

G_GNUC_INTERNAL gboolean
gcab_file_set_uoffset (GCabFile *self, u4 uoffset)
{
    g_return_val_if_fail (GCAB_IS_FILE (self), FALSE);

    self->cfile.uoffset = uoffset;

    return TRUE;
}

/**
 * gcab_file_get_size:
 * @file: a #GCabFile
 *
 * Get the file size.
 *
 * Returns: the cabinet file size
 * Since: 0.6
 **/
guint32
gcab_file_get_size (GCabFile *self)
{
    g_return_val_if_fail (GCAB_IS_FILE (self), 0);

    return self->cfile.usize;
}

/**
 * gcab_file_get_date:
 * @file: a #GCabFile
 * @result: a #GTimeVal to return date
 *
 * Get the file date.
 *
 * Returns: the cabinet file date in @result
 * Since: 0.6
 **/
void
gcab_file_get_date (GCabFile *self, GTimeVal *tv)
{
    struct tm tm;
    guint16 date, time;

    g_return_if_fail (GCAB_IS_FILE (self));
    g_return_if_fail (tv != NULL);

    date = self->cfile.date;
    time = self->cfile.time;

    tm.tm_isdst = -1;
    tm.tm_year  = ((date >> 9) + 1980) - 1900;
    tm.tm_mon   = ((date >> 5) & 0xf) - 1;
    tm.tm_mday  = (date & 0x1f) - 1;

    tm.tm_hour  = (time >> 11);
    tm.tm_min   = ((time >> 5) & 0x3f);
    tm.tm_sec   = (time & 0x1f) * 2;

    tv->tv_sec  = mktime(&tm);
    tv->tv_usec = 0;
}

/**
 * gcab_file_get_attributes:
 * @file: a #GCabFile
 *
 * Get the file attributes.
 *
 * Returns: the cabinet file attributes
 * Since: 0.6
 **/
guint32
gcab_file_get_attributes (GCabFile *self)
{
    g_return_val_if_fail (GCAB_IS_FILE (self), 0);

    return self->cfile.fattr;
}

/**
 * gcab_file_get_name:
 * @file: a #GCabFile
 *
 * Get the file name within the cabinet.
 *
 * Returns: the cabinet file name
 **/
const gchar *
gcab_file_get_name (GCabFile *self)
{
    g_return_val_if_fail (GCAB_IS_FILE (self), NULL);

    return self->name;
}

/**
 * gcab_file_get_file:
 * @file: a #GCabFile
 *
 * If the cabinet is being created, get the #GFile associated with
 * @file. This must be an exisiting file that can be read, in order to
 * be added to the archive during cabinet creation.
 *
 * If @file is from an existing cabinet, the fuction will return
 * %NULL.
 *
 * Returns: (transfer full): the associated #GFile or %NULL
 **/
GFile *
gcab_file_get_file (GCabFile *self)
{
    g_return_val_if_fail (GCAB_IS_FILE (self), NULL);

    return self->file;
}

/**
 * gcab_file_new_with_file:
 * @name: name of the file within the cabinet
 * @file: a #GFile to be added to the cabinet
 *
 * Create a #GCabFile from a given #GFile, to be added to a
 * #GCabCabinet for archive creation.
 *
 * Returns: a new #GCabFile
 **/
GCabFile *
gcab_file_new_with_file (const gchar *name, GFile *file)
{
    g_return_val_if_fail (name != NULL, NULL);
    g_return_val_if_fail (G_IS_FILE (file), NULL);

    return g_object_new (GCAB_TYPE_FILE,
                         "name", name,
                         "file", file,
                         NULL);
}

G_GNUC_INTERNAL GCabFile *
gcab_file_new_with_cfile (const cfile_t *cfile)
{
    g_return_val_if_fail (cfile != NULL, NULL);

    GCabFile *file = g_object_new (GCAB_TYPE_FILE,
                                   "name", cfile->name,
                                   NULL);
    file->cfile = *cfile;

    return file;
}

/**
 * gcab_file_get_extract_name:
 * @file: a #GCabFile
 *
 * Get the file name to use for extraction, or %NULL.
 *
 * Returns: (allow-none): a file name
 **/
const gchar *
gcab_file_get_extract_name (GCabFile *self)
{
    g_return_val_if_fail (GCAB_IS_FILE (self), NULL);

    return self->extract_name ? self->extract_name : self->name;
}

/**
 * gcab_file_set_extract_name:
 * @file: a #GCabFile
 * @name: (allow-none): a file name or %NULL
 *
 * Sets the file name to use for extraction, instead of the name
 * provided by the Cabinet.
 **/
void
gcab_file_set_extract_name (GCabFile *self, const gchar *name)
{
    g_return_if_fail (GCAB_IS_FILE (self));

    g_free (self->extract_name);
    self->extract_name = g_strdup (name);
}