[1] | 1 | /* $Id: RTFileMove-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - RTFileMove, Generic.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[21337] | 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
| 42 | #include <iprt/file.h>
|
---|
[21337] | 43 | #include "internal/iprt.h"
|
---|
| 44 |
|
---|
[1] | 45 | #include <iprt/path.h>
|
---|
| 46 | #include <iprt/err.h>
|
---|
| 47 | #include <iprt/assert.h>
|
---|
| 48 | #include <iprt/log.h>
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
|
---|
| 52 | {
|
---|
| 53 | /*
|
---|
| 54 | * Validate input.
|
---|
| 55 | */
|
---|
[90781] | 56 | AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
|
---|
| 57 | AssertPtrReturn(pszDst, VERR_INVALID_POINTER);
|
---|
[1] | 58 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
| 59 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
[39644] | 60 | AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
|
---|
[1] | 61 |
|
---|
| 62 | /*
|
---|
| 63 | * Try RTFileRename first.
|
---|
| 64 | */
|
---|
| 65 | Assert(RTPATHRENAME_FLAGS_REPLACE == RTFILEMOVE_FLAGS_REPLACE);
|
---|
| 66 | unsigned fRename = fMove;
|
---|
| 67 | int rc = RTFileRename(pszSrc, pszDst, fRename);
|
---|
| 68 | if (rc == VERR_NOT_SAME_DEVICE)
|
---|
| 69 | {
|
---|
| 70 | const char *pszDelete = NULL;
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * The source and target are not on the same device, darn.
|
---|
| 74 | * We'll try open both ends and perform a copy.
|
---|
| 75 | */
|
---|
| 76 | RTFILE FileSrc;
|
---|
| 77 | rc = RTFileOpen(&FileSrc, pszSrc, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
|
---|
| 78 | if (RT_SUCCESS(rc))
|
---|
| 79 | {
|
---|
| 80 | RTFILE FileDst;
|
---|
| 81 | rc = RTFileOpen(&FileDst, pszDst, RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE_REPLACE);
|
---|
| 82 | if (RT_SUCCESS(rc))
|
---|
| 83 | {
|
---|
| 84 | rc = RTFileCopyByHandles(FileSrc, FileDst);
|
---|
| 85 | if (RT_SUCCESS(rc))
|
---|
| 86 | pszDelete = pszSrc;
|
---|
| 87 | else
|
---|
| 88 | {
|
---|
| 89 | pszDelete = pszDst;
|
---|
| 90 | Log(("RTFileMove('%s', '%s', %#x): copy failed, rc=%Rrc\n",
|
---|
| 91 | pszSrc, pszDst, fMove, rc));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /* try delete without closing, and could perhaps avoid some trouble */
|
---|
| 95 | int rc2 = RTFileDelete(pszDelete);
|
---|
| 96 | if (RT_SUCCESS(rc2))
|
---|
| 97 | pszDelete = NULL;
|
---|
| 98 | RTFileClose(FileDst);
|
---|
| 99 | }
|
---|
| 100 | else
|
---|
| 101 | Log(("RTFileMove('%s', '%s', %#x): failed to create destination, rc=%Rrc\n",
|
---|
| 102 | pszSrc, pszDst, fMove, rc));
|
---|
| 103 | RTFileClose(FileSrc);
|
---|
| 104 | }
|
---|
| 105 | else
|
---|
| 106 | Log(("RTFileMove('%s', '%s', %#x): failed to open source, rc=%Rrc\n",
|
---|
| 107 | pszSrc, pszDst, fMove, rc));
|
---|
| 108 |
|
---|
| 109 | /* if we failed to close it while open, close it now */
|
---|
| 110 | if (pszDelete)
|
---|
| 111 | {
|
---|
| 112 | int rc2 = RTFileDelete(pszDelete);
|
---|
| 113 | if (RT_FAILURE(rc2))
|
---|
| 114 | Log(("RTFileMove('%s', '%s', %#x): failed to delete '%s', rc2=%Rrc (rc=%Rrc)\n",
|
---|
| 115 | pszSrc, pszDst, fMove, pszDelete, rc2, rc));
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
| 120 | pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
|
---|
| 121 | return rc;
|
---|
| 122 | }
|
---|
[21337] | 123 | RT_EXPORT_SYMBOL(RTFileMove);
|
---|
[1] | 124 |
|
---|