VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/RTFileMove-generic.cpp@ 93115

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.3 KB
Line 
1/* $Id: RTFileMove-generic.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - RTFileMove, Generic.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_FILE
32#include <iprt/file.h>
33#include "internal/iprt.h"
34
35#include <iprt/path.h>
36#include <iprt/err.h>
37#include <iprt/assert.h>
38#include <iprt/log.h>
39
40
41RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
42{
43 /*
44 * Validate input.
45 */
46 AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
47 AssertPtrReturn(pszDst, VERR_INVALID_POINTER);
48 AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
49 AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
50 AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
51
52 /*
53 * Try RTFileRename first.
54 */
55 Assert(RTPATHRENAME_FLAGS_REPLACE == RTFILEMOVE_FLAGS_REPLACE);
56 unsigned fRename = fMove;
57 int rc = RTFileRename(pszSrc, pszDst, fRename);
58 if (rc == VERR_NOT_SAME_DEVICE)
59 {
60 const char *pszDelete = NULL;
61
62 /*
63 * The source and target are not on the same device, darn.
64 * We'll try open both ends and perform a copy.
65 */
66 RTFILE FileSrc;
67 rc = RTFileOpen(&FileSrc, pszSrc, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
68 if (RT_SUCCESS(rc))
69 {
70 RTFILE FileDst;
71 rc = RTFileOpen(&FileDst, pszDst, RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE_REPLACE);
72 if (RT_SUCCESS(rc))
73 {
74 rc = RTFileCopyByHandles(FileSrc, FileDst);
75 if (RT_SUCCESS(rc))
76 pszDelete = pszSrc;
77 else
78 {
79 pszDelete = pszDst;
80 Log(("RTFileMove('%s', '%s', %#x): copy failed, rc=%Rrc\n",
81 pszSrc, pszDst, fMove, rc));
82 }
83
84 /* try delete without closing, and could perhaps avoid some trouble */
85 int rc2 = RTFileDelete(pszDelete);
86 if (RT_SUCCESS(rc2))
87 pszDelete = NULL;
88 RTFileClose(FileDst);
89 }
90 else
91 Log(("RTFileMove('%s', '%s', %#x): failed to create destination, rc=%Rrc\n",
92 pszSrc, pszDst, fMove, rc));
93 RTFileClose(FileSrc);
94 }
95 else
96 Log(("RTFileMove('%s', '%s', %#x): failed to open source, rc=%Rrc\n",
97 pszSrc, pszDst, fMove, rc));
98
99 /* if we failed to close it while open, close it now */
100 if (pszDelete)
101 {
102 int rc2 = RTFileDelete(pszDelete);
103 if (RT_FAILURE(rc2))
104 Log(("RTFileMove('%s', '%s', %#x): failed to delete '%s', rc2=%Rrc (rc=%Rrc)\n",
105 pszSrc, pszDst, fMove, pszDelete, rc2, rc));
106 }
107 }
108
109 LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
110 pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
111 return rc;
112}
113RT_EXPORT_SYMBOL(RTFileMove);
114
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette