xref: /trunk/main/sal/systools/win32/uwinapi/MoveFileExA.cpp (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "macros.h"
29 
30 #define WININIT_FILENAME    "wininit.ini"
31 #define RENAME_SECTION      "rename"
32 
33 IMPLEMENT_THUNK( kernel32, WINDOWS, BOOL, WINAPI, MoveFileExA, ( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags ) )
34 {
35     BOOL    fSuccess = FALSE;   // assume failure
36 
37     // Windows 9x has a special mechanism to move files after reboot
38 
39     if ( dwFlags & MOVEFILE_DELAY_UNTIL_REBOOT )
40     {
41         CHAR    szExistingFileNameA[MAX_PATH];
42         CHAR    szNewFileNameA[MAX_PATH] = "NUL";
43 
44         // Path names in WININIT.INI must be in short path name form
45 
46         if (
47             GetShortPathNameA( lpExistingFileNameA, szExistingFileNameA, MAX_PATH ) &&
48             (!lpNewFileNameA || GetShortPathNameA( lpNewFileNameA, szNewFileNameA, MAX_PATH ))
49             )
50         {
51             CHAR    szBuffer[32767];    // The buffer size must not exceed 32K
52             DWORD   dwBufLen = GetPrivateProfileSectionA( RENAME_SECTION, szBuffer, elementsof(szBuffer), WININIT_FILENAME );
53 
54             CHAR    szRename[MAX_PATH]; // This is enough for at most to times 67 chracters
55             strcpy( szRename, szNewFileNameA );
56             strcat( szRename, "=" );
57             strcat( szRename, szExistingFileNameA );
58             size_t  lnRename = strlen(szRename);
59 
60             if ( dwBufLen + lnRename + 2 <= elementsof(szBuffer) )
61             {
62                 CopyMemory( &szBuffer[dwBufLen], szRename, lnRename );
63                 szBuffer[dwBufLen + lnRename ] = 0;
64                 szBuffer[dwBufLen + lnRename + 1 ] = 0;
65 
66                 fSuccess = WritePrivateProfileSectionA( RENAME_SECTION, szBuffer, WININIT_FILENAME );
67             }
68             else
69                 SetLastError( ERROR_BUFFER_OVERFLOW );
70         }
71     }
72     else
73     {
74 
75         fSuccess = MoveFileA( lpExistingFileNameA, lpNewFileNameA );
76 
77         if ( !fSuccess && 0 != (dwFlags & (MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) )
78         {
79             BOOL    bFailIfExist = 0 == (dwFlags & MOVEFILE_REPLACE_EXISTING);
80 
81             fSuccess = CopyFileA( lpExistingFileNameA, lpNewFileNameA, bFailIfExist );
82 
83             // In case of successfull copy do not return FALSE if delete fails.
84             // Error detection is done by GetLastError()
85 
86             if ( fSuccess )
87             {
88                 SetLastError( NO_ERROR );
89                 DeleteFileA( lpExistingFileNameA );
90             }
91         }
92 
93     }
94 
95     return fSuccess;
96 }
97 
98