1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MoveFileExA.cpp,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 #define WININIT_FILENAME "wininit.ini"
34 #define RENAME_SECTION "rename"
36 IMPLEMENT_THUNK( kernel32
, WINDOWS
, BOOL
, WINAPI
, MoveFileExA
, ( LPCSTR lpExistingFileNameA
, LPCSTR lpNewFileNameA
, DWORD dwFlags
) )
38 BOOL fSuccess
= FALSE
; // assume failure
40 // Windows 9x has a special mechanism to move files after reboot
42 if ( dwFlags
& MOVEFILE_DELAY_UNTIL_REBOOT
)
44 CHAR szExistingFileNameA
[MAX_PATH
];
45 CHAR szNewFileNameA
[MAX_PATH
] = "NUL";
47 // Path names in WININIT.INI must be in short path name form
50 GetShortPathNameA( lpExistingFileNameA
, szExistingFileNameA
, MAX_PATH
) &&
51 (!lpNewFileNameA
|| GetShortPathNameA( lpNewFileNameA
, szNewFileNameA
, MAX_PATH
))
54 CHAR szBuffer
[32767]; // The buffer size must not exceed 32K
55 DWORD dwBufLen
= GetPrivateProfileSectionA( RENAME_SECTION
, szBuffer
, elementsof(szBuffer
), WININIT_FILENAME
);
57 CHAR szRename
[MAX_PATH
]; // This is enough for at most to times 67 chracters
58 strcpy( szRename
, szNewFileNameA
);
59 strcat( szRename
, "=" );
60 strcat( szRename
, szExistingFileNameA
);
61 size_t lnRename
= strlen(szRename
);
63 if ( dwBufLen
+ lnRename
+ 2 <= elementsof(szBuffer
) )
65 CopyMemory( &szBuffer
[dwBufLen
], szRename
, lnRename
);
66 szBuffer
[dwBufLen
+ lnRename
] = 0;
67 szBuffer
[dwBufLen
+ lnRename
+ 1 ] = 0;
69 fSuccess
= WritePrivateProfileSectionA( RENAME_SECTION
, szBuffer
, WININIT_FILENAME
);
72 SetLastError( ERROR_BUFFER_OVERFLOW
);
78 fSuccess
= MoveFileA( lpExistingFileNameA
, lpNewFileNameA
);
80 if ( !fSuccess
&& 0 != (dwFlags
& (MOVEFILE_COPY_ALLOWED
| MOVEFILE_REPLACE_EXISTING
)) )
82 BOOL bFailIfExist
= 0 == (dwFlags
& MOVEFILE_REPLACE_EXISTING
);
84 fSuccess
= CopyFileA( lpExistingFileNameA
, lpNewFileNameA
, bFailIfExist
);
86 // In case of successfull copy do not return FALSE if delete fails.
87 // Error detection is done by GetLastError()
91 SetLastError( NO_ERROR
);
92 DeleteFileA( lpExistingFileNameA
);