1 | /* $Id: RTFileMove-generic.cpp 21337 2009-07-07 14:58:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - 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 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include "internal/iprt.h"
|
---|
38 |
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/log.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | RTDECL(int) RTFileMove(const char *pszSrc, const char *pszDst, unsigned fMove)
|
---|
46 | {
|
---|
47 | /*
|
---|
48 | * Validate input.
|
---|
49 | */
|
---|
50 | AssertMsgReturn(VALID_PTR(pszSrc), ("%p\n", pszSrc), VERR_INVALID_POINTER);
|
---|
51 | AssertMsgReturn(VALID_PTR(pszDst), ("%p\n", pszDst), VERR_INVALID_POINTER);
|
---|
52 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
53 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
54 | AssertMsgReturn(!(fMove & ~RTFILEMOVE_FLAGS_REPLACE), ("%#x\n", fMove), VERR_INVALID_PARAMETER);
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Try RTFileRename first.
|
---|
58 | */
|
---|
59 | Assert(RTPATHRENAME_FLAGS_REPLACE == RTFILEMOVE_FLAGS_REPLACE);
|
---|
60 | unsigned fRename = fMove;
|
---|
61 | int rc = RTFileRename(pszSrc, pszDst, fRename);
|
---|
62 | if (rc == VERR_NOT_SAME_DEVICE)
|
---|
63 | {
|
---|
64 | const char *pszDelete = NULL;
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * The source and target are not on the same device, darn.
|
---|
68 | * We'll try open both ends and perform a copy.
|
---|
69 | */
|
---|
70 | RTFILE FileSrc;
|
---|
71 | rc = RTFileOpen(&FileSrc, pszSrc, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
|
---|
72 | if (RT_SUCCESS(rc))
|
---|
73 | {
|
---|
74 | RTFILE FileDst;
|
---|
75 | rc = RTFileOpen(&FileDst, pszDst, RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE_REPLACE);
|
---|
76 | if (RT_SUCCESS(rc))
|
---|
77 | {
|
---|
78 | rc = RTFileCopyByHandles(FileSrc, FileDst);
|
---|
79 | if (RT_SUCCESS(rc))
|
---|
80 | pszDelete = pszSrc;
|
---|
81 | else
|
---|
82 | {
|
---|
83 | pszDelete = pszDst;
|
---|
84 | Log(("RTFileMove('%s', '%s', %#x): copy failed, rc=%Rrc\n",
|
---|
85 | pszSrc, pszDst, fMove, rc));
|
---|
86 | }
|
---|
87 |
|
---|
88 | /* try delete without closing, and could perhaps avoid some trouble */
|
---|
89 | int rc2 = RTFileDelete(pszDelete);
|
---|
90 | if (RT_SUCCESS(rc2))
|
---|
91 | pszDelete = NULL;
|
---|
92 | RTFileClose(FileDst);
|
---|
93 | }
|
---|
94 | else
|
---|
95 | Log(("RTFileMove('%s', '%s', %#x): failed to create destination, rc=%Rrc\n",
|
---|
96 | pszSrc, pszDst, fMove, rc));
|
---|
97 | RTFileClose(FileSrc);
|
---|
98 | }
|
---|
99 | else
|
---|
100 | Log(("RTFileMove('%s', '%s', %#x): failed to open source, rc=%Rrc\n",
|
---|
101 | pszSrc, pszDst, fMove, rc));
|
---|
102 |
|
---|
103 | /* if we failed to close it while open, close it now */
|
---|
104 | if (pszDelete)
|
---|
105 | {
|
---|
106 | int rc2 = RTFileDelete(pszDelete);
|
---|
107 | if (RT_FAILURE(rc2))
|
---|
108 | Log(("RTFileMove('%s', '%s', %#x): failed to delete '%s', rc2=%Rrc (rc=%Rrc)\n",
|
---|
109 | pszSrc, pszDst, fMove, pszDelete, rc2, rc));
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
|
---|
114 | pszSrc, pszSrc, pszDst, pszDst, fMove, rc));
|
---|
115 | return rc;
|
---|
116 | }
|
---|
117 | RT_EXPORT_SYMBOL(RTFileMove);
|
---|
118 |
|
---|