VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/io/nsLocalFileCommon.cpp@ 65902

最後變更 在這個檔案從65902是 16402,由 vboxsync 提交於 16 年 前

XPCOM-darwin/amd64: Replaced ugly expression with VBOX_MACOSX_FOLLOWS_UNIX_IO.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.1 KB
 
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Doug Turner <[email protected]>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38#include "nsIServiceManager.h"
39
40#include "nsLocalFile.h" // includes platform-specific headers
41#include "nsLocalFileUnicode.h"
42
43#include "nsString.h"
44#include "nsCOMPtr.h"
45#include "nsReadableUtils.h"
46#include "nsPrintfCString.h"
47#include "nsCRT.h"
48
49#ifdef XP_WIN
50#include <string.h>
51#endif
52
53
54void NS_StartupLocalFile()
55{
56 nsLocalFile::GlobalInit();
57}
58
59void NS_ShutdownLocalFile()
60{
61 nsLocalFile::GlobalShutdown();
62}
63
64#if (!defined(XP_MAC) && !defined(XP_MACOSX)) || defined(VBOX_MACOSX_FOLLOWS_UNIX_IO)
65NS_IMETHODIMP
66nsLocalFile::InitWithFile(nsILocalFile *aFile)
67{
68 NS_ENSURE_ARG(aFile);
69
70 nsCAutoString path;
71 aFile->GetNativePath(path);
72 if (path.IsEmpty())
73 return NS_ERROR_INVALID_ARG;
74 return InitWithNativePath(path);
75}
76#endif
77
78#if defined(XP_MAC)
79#define kMaxFilenameLength 31
80#else
81#define kMaxFilenameLength 255
82#endif
83
84NS_IMETHODIMP
85nsLocalFile::CreateUnique(PRUint32 type, PRUint32 attributes)
86{
87 nsresult rv = Create(type, attributes);
88
89 if (NS_SUCCEEDED(rv)) return NS_OK;
90 if (rv != NS_ERROR_FILE_ALREADY_EXISTS) return rv;
91
92 nsCAutoString leafName;
93 rv = GetNativeLeafName(leafName);
94
95 if (NS_FAILED(rv)) return rv;
96
97 const char* lastDot = strrchr(leafName.get(), '.');
98 char suffix[kMaxFilenameLength + 1] = "";
99 if (lastDot)
100 {
101 strncpy(suffix, lastDot, kMaxFilenameLength); // include '.'
102 suffix[kMaxFilenameLength] = 0; // make sure it's null terminated
103 leafName.SetLength(lastDot - leafName.get()); // strip suffix and dot.
104 }
105
106 const int maxRootLength = (kMaxFilenameLength - 4) - strlen(suffix) - 1;
107
108 if ((int)leafName.Length() > (int)maxRootLength)
109 leafName.SetLength(maxRootLength);
110
111 for (short indx = 1; indx < 10000; indx++)
112 {
113 // start with "Picture-1.jpg" after "Picture.jpg" exists
114 SetNativeLeafName(leafName +
115 nsPrintfCString("-%d", indx) +
116 nsDependentCString(suffix));
117
118 rv = Create(type, attributes);
119
120 if (NS_SUCCEEDED(rv) || rv != NS_ERROR_FILE_ALREADY_EXISTS)
121 {
122 return rv;
123 }
124 }
125
126 // The disk is full, sort of
127 return NS_ERROR_FILE_TOO_BIG;
128}
129
130#if defined(XP_MAC)
131static const PRUnichar kPathSeparatorChar = ':';
132#elif defined(XP_WIN) || defined(XP_OS2)
133static const PRUnichar kPathSeparatorChar = '\\';
134#elif defined(XP_UNIX) || defined(XP_BEOS)
135static const PRUnichar kPathSeparatorChar = '/';
136#else
137#error Need to define file path separator for your platform
138#endif
139
140#if defined(XP_MAC)
141static const char kSlashStr[] = "/";
142static const char kESCSlashStr[] = "%2F";
143#endif
144
145static PRInt32 SplitPath(PRUnichar *path, PRUnichar **nodeArray, PRInt32 arrayLen)
146{
147 if (*path == 0)
148 return 0;
149
150 PRUnichar **nodePtr = nodeArray;
151 if (*path == kPathSeparatorChar)
152 path++;
153 *nodePtr++ = path;
154
155 for (PRUnichar *cp = path; *cp != 0; cp++) {
156 if (*cp == kPathSeparatorChar) {
157 *cp++ = 0;
158 if (*cp == 0)
159 break;
160 if (nodePtr - nodeArray >= arrayLen)
161 return -1;
162 *nodePtr++ = cp;
163 }
164 }
165 return nodePtr - nodeArray;
166}
167
168
169NS_IMETHODIMP
170nsLocalFile::GetRelativeDescriptor(nsILocalFile *fromFile, nsACString& _retval)
171{
172 NS_ENSURE_ARG_POINTER(fromFile);
173 const PRInt32 kMaxNodesInPath = 32;
174
175 //
176 // _retval will be UTF-8 encoded
177 //
178
179 nsresult rv;
180 _retval.Truncate(0);
181
182 nsAutoString thisPath, fromPath;
183 PRUnichar *thisNodes[kMaxNodesInPath], *fromNodes[kMaxNodesInPath];
184 PRInt32 thisNodeCnt, fromNodeCnt, nodeIndex;
185
186 rv = GetPath(thisPath);
187 if (NS_FAILED(rv))
188 return rv;
189 rv = fromFile->GetPath(fromPath);
190 if (NS_FAILED(rv))
191 return rv;
192
193 // get raw pointer to mutable string buffer
194 PRUnichar *thisPathPtr; thisPath.BeginWriting(thisPathPtr);
195 PRUnichar *fromPathPtr; fromPath.BeginWriting(fromPathPtr);
196
197 thisNodeCnt = SplitPath(thisPathPtr, thisNodes, kMaxNodesInPath);
198 fromNodeCnt = SplitPath(fromPathPtr, fromNodes, kMaxNodesInPath);
199 if (thisNodeCnt < 0 || fromNodeCnt < 0)
200 return NS_ERROR_FAILURE;
201
202 for (nodeIndex = 0; nodeIndex < thisNodeCnt && nodeIndex < fromNodeCnt; ++nodeIndex) {
203#ifdef XP_WIN
204 if (_wcsicmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
205 break;
206#else
207 if (nsCRT::strcmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
208 break;
209#endif
210 }
211
212 PRInt32 branchIndex = nodeIndex;
213 for (nodeIndex = branchIndex; nodeIndex < fromNodeCnt; nodeIndex++)
214 _retval.AppendLiteral("../");
215 for (nodeIndex = branchIndex; nodeIndex < thisNodeCnt; nodeIndex++) {
216 NS_ConvertUCS2toUTF8 nodeStr(thisNodes[nodeIndex]);
217#ifdef XP_MAC
218 nodeStr.ReplaceSubstring(kSlashStr, kESCSlashStr);
219#endif
220 _retval.Append(nodeStr);
221 if (nodeIndex + 1 < thisNodeCnt)
222 _retval.Append('/');
223 }
224
225 return NS_OK;
226}
227
228NS_IMETHODIMP
229nsLocalFile::SetRelativeDescriptor(nsILocalFile *fromFile, const nsACString& relativeDesc)
230{
231 NS_NAMED_LITERAL_CSTRING(kParentDirStr, "../");
232
233 nsCOMPtr<nsIFile> targetFile;
234 nsresult rv = fromFile->Clone(getter_AddRefs(targetFile));
235 if (NS_FAILED(rv))
236 return rv;
237
238 //
239 // relativeDesc is UTF-8 encoded
240 //
241
242 nsCString::const_iterator strBegin, strEnd;
243 relativeDesc.BeginReading(strBegin);
244 relativeDesc.EndReading(strEnd);
245
246 nsCString::const_iterator nodeBegin(strBegin), nodeEnd(strEnd);
247 nsCString::const_iterator pos(strBegin);
248
249 nsCOMPtr<nsIFile> parentDir;
250 while (FindInReadable(kParentDirStr, nodeBegin, nodeEnd)) {
251 rv = targetFile->GetParent(getter_AddRefs(parentDir));
252 if (NS_FAILED(rv))
253 return rv;
254 targetFile = parentDir;
255
256 nodeBegin = nodeEnd;
257 pos = nodeEnd;
258 nodeEnd = strEnd;
259 }
260
261 nodeBegin = nodeEnd = pos;
262 while (nodeEnd != strEnd) {
263 FindCharInReadable('/', nodeEnd, strEnd);
264#ifdef XP_MAC
265 nsCAutoString nodeString(Substring(nodeBegin, nodeEnd));
266 nodeString.ReplaceSubstring(kESCSlashStr, kSlashStr);
267 targetFile->Append(NS_ConvertUTF8toUCS2(nodeString));
268#else
269 targetFile->Append(NS_ConvertUTF8toUCS2(Substring(nodeBegin, nodeEnd)));
270#endif
271 if (nodeEnd != strEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
272 ++nodeEnd;
273 nodeBegin = nodeEnd;
274 }
275
276 nsCOMPtr<nsILocalFile> targetLocalFile(do_QueryInterface(targetFile));
277 return InitWithFile(targetLocalFile);
278}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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