~ubuntu-branches/ubuntu/precise/uget/precise-backports

« back to all changes in this revision

Viewing changes to src/ug_plugin.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-03-01 18:24:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100301182449-p57egd83f2i9g4l8
Tags: 1.5.0.2-1
* New upstream release, fixes LP: #512254.
* Bump Standards.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *
3
 
 *   Copyright (C) 2005-2009 by Raymond Huang
 
3
 *   Copyright (C) 2005-2010 by Raymond Huang
4
4
 *   plushuang at users.sourceforge.net
5
5
 *
6
6
 *  This library is free software; you can redistribute it and/or
253
253
// if function succeeded, return new path and it's folder length.
254
254
gchar*          ug_plugin_create_file (UgPlugin* plugin, const gchar* folder, const gchar* file, guint* folder_len)
255
255
{
256
 
        UgPathPart*             pathpart;
257
256
        GString*                gstr;
 
257
        const gchar*    tail_str;
 
258
        guint                   head_len;
258
259
        guint                   location_len;
259
 
        // for file creation
260
 
        const gchar*    tail_str;       // string of '.' + filename extension
261
 
        guint                   head_len;       // length of folder + primary filename
262
 
        guint                   retry_count;
 
260
        guint                   count;
263
261
        int                             fd;
264
262
 
 
263
        if (folder == NULL && file == NULL) {
 
264
                ug_plugin_post (plugin, ug_message_new_error (UG_MESSAGE_ERROR_FILE_CREATE_FAILED, NULL));
 
265
                return NULL;
 
266
        }
 
267
        // concatenate full path
265
268
        gstr = g_string_sized_new (80);
266
 
        // concatenate full path
267
269
        if (folder) {
268
270
                g_string_append (gstr, folder);
269
271
                if (gstr->len  &&  gstr->str [gstr->len -1] != G_DIR_SEPARATOR)
271
273
        }
272
274
        if (file)
273
275
                g_string_append (gstr, file);
274
 
        // use UgPathPart to get head_len, tail_str, location_len
275
 
        pathpart = ug_path_part_new (gstr->str, gstr->len);
276
 
        head_len = gstr->len - pathpart->file_ext_len;
277
 
        tail_str = file  + strlen (file) - pathpart->file_ext_len;
278
 
        if (pathpart->file_ext) {
279
 
                head_len--;
280
 
                tail_str--;
281
 
        }
282
 
        location_len = UG_PATH_PART_LOCATION_LEN (pathpart);
283
 
        ug_path_part_free (pathpart);
 
276
        // get location_len (length of path - filename).
 
277
        tail_str = strrchr (gstr->str, G_DIR_SEPARATOR);
 
278
        if (tail_str == NULL)
 
279
                location_len = 0;
 
280
        else
 
281
                location_len = tail_str - gstr->str + 1;        // + G_DIR_SEPARATOR
284
282
        // create folder
285
283
        if (location_len && ug_create_dir_all (gstr->str, location_len) == -1) {
286
284
                ug_plugin_post (plugin, ug_message_new_error (UG_MESSAGE_ERROR_FOLDER_CREATE_FAILED, NULL));
287
 
                g_string_free (gstr, TRUE);
288
 
                return NULL;
 
285
                return g_string_free (gstr, TRUE);
289
286
        }
290
 
 
 
287
        // get head_len (length of folder + primary filename)
 
288
        // and tail_str ('.' + filename extension)
 
289
        tail_str = strrchr (gstr->str + location_len, '.');
 
290
        if (tail_str == NULL)
 
291
                tail_str = gstr->str + gstr->len;
 
292
        head_len = tail_str - gstr->str;
 
293
        tail_str = g_strdup (tail_str);
291
294
        // create file
292
 
        for (retry_count=0;  retry_count < 100;  retry_count++) {
293
 
//              fd = open (gstr->str, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE | S_IRGRP | S_IROTH);
294
 
                fd = ug_fd_open (gstr->str, UG_FD_O_CREATE | UG_FD_O_EXCL | UG_FD_O_RDWR, UG_FD_S_IREAD | UG_FD_S_IWRITE | UG_FD_S_IRGRP | UG_FD_S_IROTH);
 
295
        for (count=0;  count < 100;  count++) {
 
296
//              fd = open (gstr->str, O_CREAT | O_EXCL | O_RDWR,
 
297
//                         S_IREAD | S_IWRITE | S_IRGRP | S_IROTH);
 
298
                fd = ug_fd_open (gstr->str, UG_FD_O_CREATE | UG_FD_O_EXCL | UG_FD_O_RDWR,
 
299
                                 UG_FD_S_IREAD | UG_FD_S_IWRITE | UG_FD_S_IRGRP | UG_FD_S_IROTH);
295
300
                if (fd != -1) {
296
301
//                      close (fd);
297
302
                        ug_fd_close (fd);
298
303
                        break;
299
304
                }
300
305
                g_string_truncate (gstr, head_len);
301
 
                g_string_append_printf (gstr, "(%d)", retry_count);
 
306
                g_string_append_printf (gstr, "(%d)", count);
302
307
                g_string_append (gstr, tail_str);
303
308
        }
304
 
        if (retry_count == 100) {
 
309
        // free tail_str
 
310
        g_free ((gpointer) tail_str);
 
311
        // result
 
312
        if (count == 100) {
305
313
                ug_plugin_post (plugin, ug_message_new_error (UG_MESSAGE_ERROR_FILE_CREATE_FAILED, NULL));
306
 
                g_string_free (gstr, TRUE);
307
 
                return NULL;
 
314
                return g_string_free (gstr, TRUE);
308
315
        }
309
 
        if (retry_count > 0)
 
316
        if (count > 0)
310
317
                ug_plugin_post (plugin, ug_message_new_data (UG_MESSAGE_DATA_FILE_CHANGED, gstr->str + location_len));
311
 
 
312
318
        if (folder_len)
313
319
                *folder_len = location_len;
 
320
 
314
321
        return g_string_free (gstr, FALSE);
315
322
}
316
323