1 | /* $Id: RTFileMove-generic.cpp 8170 2008-04-18 17:52:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Incredibly Portable Runtime - RTFileMove, Generic.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
32 | #include <iprt/file.h>
|
---|
33 | #include <iprt/path.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/log.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
|
---|
40 | {
|
---|
41 | /*
|
---|
42 | * Validate input.
|
---|
43 | */
|
---|
44 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
45 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
46 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
47 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
48 | AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Try RTFileRename first.
|
---|
52 | */
|
---|
53 | Assert(RTPATHRENAME_FLAGS_REPLACE == RTFILEMOVE_FLAGS_REPLACE);
|
---|
54 | unsigned fRename = fMove;
|
---|
55 | int rc = RTFileRename(pszSrc, pszDst, fRename);
|
---|
56 | if (rc == VERR_NOT_SAME_DEVICE)
|
---|
57 | {
|
---|
58 | const char *pszDelete = NULL;
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * The source and target are not on the same device, darn.
|
---|
62 | * We'll try open both ends and perform a copy.
|
---|
63 | */
|
---|
64 | RTFILE FileSrc;
|
---|
65 | rc = RTFileOpen(&FileSrc, pszSrc, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
|
---|
66 | if (RT_SUCCESS(rc))
|
---|
67 | {
|
---|
68 | RTFILE FileDst;
|
---|
69 | rc = RTFileOpen(&FileDst, pszDst, RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE_REPLACE);
|
---|
70 | if (RT_SUCCESS(rc))
|
---|
71 | {
|
---|
72 | rc = RTFileCopyByHandles(FileSrc, FileDst);
|
---|
73 | if (RT_SUCCESS(rc))
|
---|
74 | pszDelete = pszSrc;
|
---|
75 | else
|
---|
76 | {
|
---|
77 | pszDelete = pszDst;
|
---|
78 | Log(("RTFileMove('%s', '%s', %#x): copy failed, rc=%Rrc\n",
|
---|
79 | pszSrc, pszDst, fMove, rc));
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* try delete without closing, and could perhaps avoid some trouble */
|
---|
83 | int rc2 = RTFileDelete(pszDelete);
|
---|
84 | if (RT_SUCCESS(rc2))
|
---|
85 | pszDelete = NULL;
|
---|
86 | RTFileClose(FileDst);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | Log(("RTFileMove('%s', '%s', %#x): failed to create destination, rc=%Rrc\n",
|
---|
90 | pszSrc, pszDst, fMove, rc));
|
---|
91 | RTFileClose(FileSrc);
|
---|
92 | }
|
---|
93 | else
|
---|
94 | Log(("RTFileMove('%s', '%s', %#x): failed to open source, rc=%Rrc\n",
|
---|
95 | pszSrc, pszDst, fMove, rc));
|
---|
96 |
|
---|
97 | /* if we failed to close it while open, close it now */
|
---|
98 | if (pszDelete)
|
---|
99 | {
|
---|
100 | int rc2 = RTFileDelete(pszDelete);
|
---|
101 | if (RT_FAILURE(rc2))
|
---|
102 | Log(("RTFileMove('%s', '%s', %#x): failed to delete '%s', rc2=%Rrc (rc=%Rrc)\n",
|
---|
103 | pszSrc, pszDst, fMove, pszDelete, rc2, rc));
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
108 | pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
|
---|
109 | return rc;
|
---|
110 | }
|
---|
111 |
|
---|