Show
Ignore:
Timestamp:
07/19/10 16:35:46 (3 years ago)
Author:
saturday06
Message:

auto delete

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/c/AutoUpdater/trunk/UpdaterProcess.c

    r37962 r37963  
    9393 
    9494                if (FILE_ATTRIBUTE_DIRECTORY & find_data.dwFileAttributes) { 
    95             AUI_Debug(_T("dir\n")); 
     95            AUI_Debug(_T("dir error\n")); 
    9696            result = result && CreateDirectory(to2, NULL) && CopyDirectory(from2, to2); 
    9797        } else if(!CopyFile(from2, to2, FALSE)) { 
    98             AUI_Debug(_T("file\n")); 
     98            AUI_Debug(_T("file error\n")); 
    9999            result = FALSE; 
    100100                } 
     
    108108        FindClose(hFind); 
    109109    HeapFree(GetProcessHeap(), 0, glob); 
    110         return TRUE; 
     110        return result; 
    111111} 
    112112 
     
    117117    DWORD bytes_got = 0; 
    118118    TCHAR* data = NULL; 
    119     TCHAR* lines[5] = {0}; 
     119    TCHAR* lines[5] = {NULL, NULL, NULL, NULL, NULL}; 
    120120    DWORD pid = 0; 
    121121    int complete = 0; 
     122    TCHAR* clean_command = NULL; 
     123    TCHAR* temp_path = NULL; 
     124    TCHAR* command = NULL; 
    122125 
    123126    AUI_Debug(_T("***** UpdaterProcess start: pid=0x%x *****\n"), GetCurrentProcessId()); 
     127    { 
     128        size_t temp_path_chars = GetTempPath(0, NULL); 
     129        temp_path = (TCHAR*)HeapAlloc(GetProcessHeap(),  
     130            HEAP_ZERO_MEMORY, (temp_path_chars * sizeof(TCHAR))); 
     131        if (!temp_path) { 
     132            goto clean_up; 
     133        } 
     134        GetTempPath(temp_path_chars, temp_path); 
     135    } 
    124136 
    125137    hFile = CreateFile(_T("updater.txt"), GENERIC_READ, 0, NULL,  
     
    131143 
    132144    bytes = GetFileSize(hFile, NULL); 
    133     data = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bytes + sizeof(TCHAR)); 
     145    data = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bytes + (/* null terminate */ sizeof(TCHAR))); 
    134146    if (!data) { 
    135147        goto clean_up; 
     
    177189    } 
    178190 
     191 
     192    {    
     193        TCHAR comspec[MAX_PATH]; 
     194        TCHAR rd_command[] = _T("\"%s\" /c rd /s /q \"%s\""); 
     195        size_t clean_command_len = 0; 
     196        if (GetEnvironmentVariable(_T("COMSPEC"), comspec, sizeof(comspec) / sizeof(comspec[0])) == 0) { 
     197            goto clean_up; 
     198        } 
     199 
     200        clean_command_len = lstrlen(comspec) + lstrlen(rd_command) + lstrlen(lines[4]); 
     201        clean_command = (TCHAR*)HeapAlloc(GetProcessHeap(),  
     202            HEAP_ZERO_MEMORY, clean_command_len * sizeof(TCHAR)); 
     203        if (!clean_command) { 
     204            goto clean_up; 
     205        } 
     206        wsprintf(clean_command, rd_command, comspec, lines[4]);  
     207    } 
     208 
    179209    AUI_Debug(L"waiting\n"); 
    180210    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, FALSE, pid); 
     
    190220 
    191221clean_up: 
    192     { 
    193         STARTUPINFO si; 
    194         PROCESS_INFORMATION pi; 
    195         DWORD e = 0; 
    196         TCHAR* command = NULL; 
     222    if (lines[3] && lines[4]) { 
    197223        TCHAR* arg = NULL; 
    198         TCHAR complete_arg[] = _T(" UPDATE_COMPLETE"); 
    199         TCHAR error_arg[] = _T(" UPDATE_ERROR"); 
     224        TCHAR command_format[] = _T("\"%s\" %s"); 
     225        TCHAR complete_arg[] = _T("UPDATE_COMPLETE"); 
     226        TCHAR error_arg[] = _T("UPDATE_ERROR"); 
    200227        int command_len = 0; 
    201228        SetLastError(0); 
     
    205232            arg = error_arg; 
    206233        } 
    207         command_len = lstrlen(lines[3]); 
    208         command_len += 2; /* double quotation */ 
     234        command_len = lstrlen(command_format); 
     235        command_len += lstrlen(lines[3]); 
    209236        command_len += lstrlen(arg); 
    210237 
    211238        command = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (unsigned)command_len * sizeof(TCHAR)); 
    212         lstrcat(command, _T("\"")); 
    213         lstrcat(command, lines[3]); 
    214         lstrcat(command, _T("\"")); 
    215         lstrcat(command, arg); 
    216  
     239        if (command) { 
     240            wsprintf(command, command_format, lines[3], arg); 
     241        } 
     242    } 
     243 
     244    if (command && lines[4]) { 
     245        STARTUPINFO si; 
     246        PROCESS_INFORMATION pi; 
     247        DWORD e; 
    217248        memset(&si, 0, sizeof(si)); 
    218249        si.cb = sizeof(si); 
     
    223254        AUI_Debug(_T("CreateProcess(): command=%s, cd=%s, error=%d\n"), command, lines[4], e); 
    224255 
     256        CloseHandle(pi.hThread); 
     257        CloseHandle(pi.hProcess); 
     258    } 
     259 
     260    CloseHandle(hFile); 
     261    CloseHandle(hProcess); 
     262 
     263    if (clean_command) { 
     264        STARTUPINFO si; 
     265        PROCESS_INFORMATION pi; 
     266        memset(&si, 0, sizeof(si)); 
     267        si.cb = sizeof(si); 
     268        si.dwFlags = STARTF_USESHOWWINDOW;  
     269        si.wShowWindow = SW_HIDE;  
     270        memset(&pi, 0, sizeof(pi)); 
     271        CreateProcess(NULL, clean_command, NULL, NULL, FALSE, 0, NULL, temp_path, &si, &pi); 
     272        /* 
    225273            CloseHandle(pi.hThread); 
    226274            CloseHandle(pi.hProcess); 
    227  
    228         /* HeapFree(GetProcessHeap(), 0, command); */ 
    229     } 
    230  
    231     CloseHandle(hFile); 
    232     CloseHandle(hProcess); 
    233  
     275        */ 
     276    } 
     277 
     278    /* 
    234279    if (data) { 
    235280        HeapFree(GetProcessHeap(), 0, data); 
    236281    } 
    237  
    238 /* 
    239     { 
    240         memset(&si, 0, sizeof(si)); 
    241         si.cb = sizeof(si); 
    242         memset(&pi, 0, sizeof(pi)); 
    243  
    244         CreateProcess(NULL, command, NULL, NULL, FALSE, 0, NULL, lines[4], &si, &pi); 
    245         e = GetLastError(); 
    246         AUI_Debug(_T("CreateProcess(): command=%s, cd=%s, error=%d\n"), command, lines[4], e); 
    247  
    248             CloseHandle(pi.hThread); 
    249             CloseHandle(pi.hProcess); 
    250  
    251     } 
    252 */ 
     282    if (command) { 
     283        HeapFree(GetProcessHeap(), 0, command); 
     284    } 
     285    if (clean_command) { 
     286        HeapFree(GetProcessHeap(), 0, clean_command); 
     287    } 
     288    if (temp_path) { 
     289        HeapFree(GetProcessHeap(), 0, clean_cd); 
     290    } 
     291    */     
    253292 
    254293    return 0;