--- ripoff-0.8.2/src/RipOffExtractor.c 2006-08-23 19:15:19.000000000 -0400 +++ ripoff-0.8.2.mine/src/RipOffExtractor.c 2007-03-11 15:41:30.000000000 -0400 @@ -672,131 +672,87 @@ } -gboolean create_path(gchar **pathp) +gchar* +get_free_filename(gchar* origfilename) { - gchar *path = *pathp; - gchar *path_copy = g_malloc(path_size); - gchar *buffer = g_malloc(path_size); /* stores the complete path we are completing */ - gchar *piece, *next; /* these buffers store the piece of the path we are working on and piece after it */ - gint j; - strncpy(path_copy, path, path_size); - chdir("/"); - - strcpy(buffer, "/"); - piece = strtok(path_copy, "/"); - next = strtok(NULL, "/"); - - /* as we verify the path exists, we cycle through it until we run out of "pieces" aka folders */ - while(piece != NULL) - { - /* add this piece to the buffer */ - strcat(buffer, piece); - - /* make sure we can change into the directory */ - if (chdir(buffer) == -1) - { - /* if we could not change into the directory due to it not existing - we create it and the rest of the path */ - if(errno == ENOENT) - { - /* we start worrying about the next piece of the path - as we want to create directories for all but the last - piece of the path, which is the name of the actual file - that will be created */ - - /* make sure we can create the directory. Failure most - likely due to permission issues */ - if(next != NULL && mkdir(buffer, 0777) == -1) - { - g_free(buffer); - g_free(path_copy); - return FALSE; - } + gchar* rv; + + if(g_file_test(origfilename, G_FILE_TEST_EXISTS)) + { + guint length = strlen(origfilename) + 10; + gchar* newfilename = g_malloc(length); + + guint i; + for(i = 1; i < 100; i++) + { + g_snprintf(newfilename, length, "%s (%i)", origfilename, i); + if(g_file_test(newfilename, G_FILE_TEST_EXISTS)) + continue; + else + { + rv = newfilename; + goto _exit_; + } + } + + rv = NULL; + } + else + { + rv = g_strdup(origfilename); + } - /* if we create the directory succesfully, we change into it */ - chdir(buffer); - - /* set piece to next */ - piece = next; - next = strtok(NULL, "/"); - /* create directories for all but the last piece of the path */ - while(next != NULL) - { - strcat(buffer, piece); - mkdir(buffer, 0755); - - #ifdef DEBUG - g_print("RipOffExtractor: Making %s\n", buffer); - #endif - - chdir(buffer); - piece = next; - next = strtok(NULL, "/"); - strcat(buffer, "/"); - piece = next; - g_free(buffer); - g_free(path_copy); - return TRUE; - } - } - /* case for which the file already exists */ - else if(errno == ENOTDIR && next == NULL) - { - /* we increase the buffer size so that we can just use it */ - gpointer tmp = g_realloc(buffer, path_size + 6); - - if(tmp == NULL) - { - g_free(path_copy); - g_free(buffer); - return FALSE; - } - - for(j = 1; j < 100; ++j) - { - /* we take the path and tack on an integer, giving us a file name like - Example.ogg (3) if it doesn't exist, we change the the path to this - new path */ - g_snprintf(buffer, path_size + 6, "%s \(%i)", path, j); - - #ifdef DEBUG - g_print("RipOffExtractor: Buffer %s\n", buffer); - #endif - - if(mkdir(buffer, 0777) != -1) - { - rmdir(buffer); - g_free(*pathp); - *pathp = buffer; - g_free(path_copy); - return TRUE; - } - } - - g_free(buffer); - g_free(path_copy); - return FALSE; - } - - /* case for which we could not verify the path for - reasons other than it not existing (most likely permission errors) */ - else - { - g_free(buffer); - g_free(path_copy); - return FALSE; - } - } - - piece = next; - next = strtok(NULL, "/"); - strcat(buffer, "/"); - } + _exit_: + return rv; +} - g_free(buffer); - g_free(path_copy); - return TRUE; - +gboolean +create_path(gchar** pathp) +{ + gboolean rv = TRUE; + gchar* dirname; + gchar* filename; + GIOChannel* ioc; + + if(pathp == NULL) + return FALSE; + + dirname = g_path_get_dirname(*pathp); + if(!g_file_test(dirname, G_FILE_TEST_EXISTS)) + { + if(g_mkdir_with_parents(dirname, 0700)) + { + rv = FALSE; + goto _exit_; + } + } + + filename = get_free_filename(*pathp); + if(filename != NULL) + { + if((ioc = g_io_channel_new_file(filename, "w", NULL)) != NULL) + { + g_io_channel_close(ioc); + g_free(*pathp); + *pathp = filename; + rv = TRUE; + goto _exit_; + } + else + { + rv = FALSE; + goto _exit_; + } + } + else + { + rv = FALSE; + goto _exit_; + } + + _exit_: + g_free(dirname); + return rv; } gchar *copy_string(const gchar *string_to_copy)