VirtualBox

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

Last change on this file since 7348 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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