VirtualBox

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

最後變更 在這個檔案從102315是 102315,由 vboxsync 提交於 12 月 前

libs/xpcom: Remove VBOX_MACOSX_FOLLOWS_UNIX_IO which is the default for a long time now and get rid of the OSX specific code which is not used, bugref:10545

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 7.3 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
64NS_IMETHODIMP
65nsLocalFile::InitWithFile(nsILocalFile *aFile)
66{
67 NS_ENSURE_ARG(aFile);
68
69 nsCAutoString path;
70 aFile->GetNativePath(path);
71 if (path.IsEmpty())
72 return NS_ERROR_INVALID_ARG;
73 return InitWithNativePath(path);
74}
75
76#define kMaxFilenameLength 255
77
78NS_IMETHODIMP
79nsLocalFile::CreateUnique(PRUint32 type, PRUint32 attributes)
80{
81 nsresult rv = Create(type, attributes);
82
83 if (NS_SUCCEEDED(rv)) return NS_OK;
84 if (rv != NS_ERROR_FILE_ALREADY_EXISTS) return rv;
85
86 nsCAutoString leafName;
87 rv = GetNativeLeafName(leafName);
88
89 if (NS_FAILED(rv)) return rv;
90
91 const char* lastDot = strrchr(leafName.get(), '.');
92 char suffix[kMaxFilenameLength + 1] = "";
93 if (lastDot)
94 {
95 strncpy(suffix, lastDot, kMaxFilenameLength); // include '.'
96 suffix[kMaxFilenameLength] = 0; // make sure it's null terminated
97 leafName.SetLength(lastDot - leafName.get()); // strip suffix and dot.
98 }
99
100 const int maxRootLength = (kMaxFilenameLength - 4) - strlen(suffix) - 1;
101
102 if ((int)leafName.Length() > (int)maxRootLength)
103 leafName.SetLength(maxRootLength);
104
105 for (short indx = 1; indx < 10000; indx++)
106 {
107 // start with "Picture-1.jpg" after "Picture.jpg" exists
108 SetNativeLeafName(leafName +
109 nsPrintfCString("-%d", indx) +
110 nsDependentCString(suffix));
111
112 rv = Create(type, attributes);
113
114 if (NS_SUCCEEDED(rv) || rv != NS_ERROR_FILE_ALREADY_EXISTS)
115 {
116 return rv;
117 }
118 }
119
120 // The disk is full, sort of
121 return NS_ERROR_FILE_TOO_BIG;
122}
123
124#if defined(XP_UNIX)
125static const PRUnichar kPathSeparatorChar = '/';
126#else
127#error Need to define file path separator for your platform
128#endif
129
130static PRInt32 SplitPath(PRUnichar *path, PRUnichar **nodeArray, PRInt32 arrayLen)
131{
132 if (*path == 0)
133 return 0;
134
135 PRUnichar **nodePtr = nodeArray;
136 if (*path == kPathSeparatorChar)
137 path++;
138 *nodePtr++ = path;
139
140 for (PRUnichar *cp = path; *cp != 0; cp++) {
141 if (*cp == kPathSeparatorChar) {
142 *cp++ = 0;
143 if (*cp == 0)
144 break;
145 if (nodePtr - nodeArray >= arrayLen)
146 return -1;
147 *nodePtr++ = cp;
148 }
149 }
150 return nodePtr - nodeArray;
151}
152
153
154NS_IMETHODIMP
155nsLocalFile::GetRelativeDescriptor(nsILocalFile *fromFile, nsACString& _retval)
156{
157 NS_ENSURE_ARG_POINTER(fromFile);
158 const PRInt32 kMaxNodesInPath = 32;
159
160 //
161 // _retval will be UTF-8 encoded
162 //
163
164 nsresult rv;
165 _retval.Truncate(0);
166
167 nsAutoString thisPath, fromPath;
168 PRUnichar *thisNodes[kMaxNodesInPath], *fromNodes[kMaxNodesInPath];
169 PRInt32 thisNodeCnt, fromNodeCnt, nodeIndex;
170
171 rv = GetPath(thisPath);
172 if (NS_FAILED(rv))
173 return rv;
174 rv = fromFile->GetPath(fromPath);
175 if (NS_FAILED(rv))
176 return rv;
177
178 // get raw pointer to mutable string buffer
179 PRUnichar *thisPathPtr; thisPath.BeginWriting(thisPathPtr);
180 PRUnichar *fromPathPtr; fromPath.BeginWriting(fromPathPtr);
181
182 thisNodeCnt = SplitPath(thisPathPtr, thisNodes, kMaxNodesInPath);
183 fromNodeCnt = SplitPath(fromPathPtr, fromNodes, kMaxNodesInPath);
184 if (thisNodeCnt < 0 || fromNodeCnt < 0)
185 return NS_ERROR_FAILURE;
186
187 for (nodeIndex = 0; nodeIndex < thisNodeCnt && nodeIndex < fromNodeCnt; ++nodeIndex) {
188 if (nsCRT::strcmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
189 break;
190 }
191
192 PRInt32 branchIndex = nodeIndex;
193 for (nodeIndex = branchIndex; nodeIndex < fromNodeCnt; nodeIndex++)
194 _retval.AppendLiteral("../");
195 for (nodeIndex = branchIndex; nodeIndex < thisNodeCnt; nodeIndex++) {
196 NS_ConvertUCS2toUTF8 nodeStr(thisNodes[nodeIndex]);
197 _retval.Append(nodeStr);
198 if (nodeIndex + 1 < thisNodeCnt)
199 _retval.Append('/');
200 }
201
202 return NS_OK;
203}
204
205NS_IMETHODIMP
206nsLocalFile::SetRelativeDescriptor(nsILocalFile *fromFile, const nsACString& relativeDesc)
207{
208 NS_NAMED_LITERAL_CSTRING(kParentDirStr, "../");
209
210 nsCOMPtr<nsIFile> targetFile;
211 nsresult rv = fromFile->Clone(getter_AddRefs(targetFile));
212 if (NS_FAILED(rv))
213 return rv;
214
215 //
216 // relativeDesc is UTF-8 encoded
217 //
218
219 nsCString::const_iterator strBegin, strEnd;
220 relativeDesc.BeginReading(strBegin);
221 relativeDesc.EndReading(strEnd);
222
223 nsCString::const_iterator nodeBegin(strBegin), nodeEnd(strEnd);
224 nsCString::const_iterator pos(strBegin);
225
226 nsCOMPtr<nsIFile> parentDir;
227 while (FindInReadable(kParentDirStr, nodeBegin, nodeEnd)) {
228 rv = targetFile->GetParent(getter_AddRefs(parentDir));
229 if (NS_FAILED(rv))
230 return rv;
231 targetFile = parentDir;
232
233 nodeBegin = nodeEnd;
234 pos = nodeEnd;
235 nodeEnd = strEnd;
236 }
237
238 nodeBegin = nodeEnd = pos;
239 while (nodeEnd != strEnd) {
240 FindCharInReadable('/', nodeEnd, strEnd);
241 targetFile->Append(NS_ConvertUTF8toUCS2(Substring(nodeBegin, nodeEnd)));
242 if (nodeEnd != strEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
243 ++nodeEnd;
244 nodeBegin = nodeEnd;
245 }
246
247 nsCOMPtr<nsILocalFile> targetLocalFile(do_QueryInterface(targetFile));
248 return InitWithFile(targetLocalFile);
249}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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