~skss/live-usb-install/live-usb-install

« back to all changes in this revision

Viewing changes to tools/pylauncher/deletedir.c

  • Committer: Krasimir Stefanov
  • Date: 2010-12-08 13:56:49 UTC
  • Revision ID: lokiisyourmaster@gmail.com-20101208135649-qvtp2n4zw07oqu7h
2.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007, 2008 Agostino Russo
 
3
 *
 
4
 *  Written by Agostino Russo <agostino.russo@gmail.com>
 
5
 *  Following example at http://www.codeguru.com/forum/showthread.php?t=239271
 
6
 *
 
7
 *  pylauncher is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as
 
9
 *  published by the Free Software Foundation; either version 2 of
 
10
 *  the License, or (at your option) any later version.
 
11
 *
 
12
 *  pylauncher is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 * DESCRIPTION:
 
21
 * Recursively deletes a directory
 
22
 */
 
23
 
 
24
#include "deletedir.h"
 
25
#include "str.h"
 
26
#include "windows.h"
 
27
 
 
28
#define true 1
 
29
#define false 0
 
30
 
 
31
int _delete_directory(char* root_directory);
 
32
 
 
33
int delete_directory(char* target_directory)
 
34
{
 
35
    char abs_path[MAX_PATH];
 
36
    int result;
 
37
    if (GetFullPathName(target_directory, MAX_PATH, abs_path, NULL)!=0){
 
38
        chdir("\\");
 
39
        result = _delete_directory(abs_path);
 
40
        return result;
 
41
    }
 
42
    return 1;
 
43
}
 
44
 
 
45
int _delete_directory(char* root_directory)
 
46
{
 
47
    int result;
 
48
    HANDLE file_handle;
 
49
    char* file_path = 0;
 
50
    char* pattern;
 
51
    WIN32_FIND_DATA file_info;
 
52
 
 
53
    pattern = concat(root_directory, "\\*.*", false);
 
54
    if(!pattern) return ERROR_NOT_ENOUGH_MEMORY;
 
55
    file_handle = FindFirstFile(pattern, &file_info);
 
56
 
 
57
    if(file_handle != INVALID_HANDLE_VALUE){
 
58
        do{
 
59
            if(file_info.cFileName[0] != '.'){
 
60
                file_path = concat(root_directory, "\\", false);
 
61
                file_path = concat(file_path, file_info.cFileName, true);
 
62
                if(!file_path) {
 
63
                    FindClose(file_handle);
 
64
                    free(pattern);
 
65
                    return ERROR_NOT_ENOUGH_MEMORY;
 
66
                }
 
67
                result = 0;
 
68
                if(file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
 
69
                    // Delete subdirectory
 
70
                    result = _delete_directory(file_path);
 
71
                } else {
 
72
                    // Set file attributes
 
73
                    if(SetFileAttributes(file_path, FILE_ATTRIBUTE_NORMAL) == FALSE){
 
74
                        result = GetLastError();
 
75
                    }
 
76
                    // Delete file
 
77
                    if(DeleteFile(file_path) == FALSE){
 
78
                        result = GetLastError();
 
79
                    }
 
80
                }
 
81
                if(result){
 
82
                    freestr(&file_path);
 
83
                    FindClose(file_handle);
 
84
                    free(pattern);
 
85
                    return result;
 
86
                }
 
87
            }
 
88
        } while(FindNextFile(file_handle, &file_info) == TRUE);
 
89
        freestr(&file_path);
 
90
        FindClose(file_handle);
 
91
        free(pattern);
 
92
 
 
93
        DWORD dwError = GetLastError();
 
94
        if(dwError != ERROR_NO_MORE_FILES){
 
95
            return dwError;
 
96
        } else {
 
97
            // Set directory attributes
 
98
            if(SetFileAttributes(root_directory, FILE_ATTRIBUTE_NORMAL) == FALSE){
 
99
                return GetLastError();
 
100
            }
 
101
            // Delete directory
 
102
            if(RemoveDirectory(root_directory) == FALSE){
 
103
                return GetLastError();
 
104
            }
 
105
        }
 
106
    }
 
107
    return 0;
 
108
}