sbFileUtils.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #include "sbFileUtils.h"
28 
29 #include <nsIFileStreams.h>
30 #include <nsIFileURL.h>
31 #include <nsILocalFile.h>
32 #include <nsIURI.h>
33 
34 #include <nsCOMPtr.h>
35 #include <nsComponentManagerUtils.h>
36 #include <nsMemory.h>
37 #include <nsNetCID.h>
38 #include <nsNetUtil.h>
39 
40 #include <sbMemoryUtils.h>
42 #include <sbURIUtils.h>
43 
47 nsresult sbOpenInputStream(nsAString const & aPath, nsIInputStream ** aStream)
48 {
49  NS_ENSURE_ARG_POINTER(aStream);
50 
51  nsresult rv;
52  nsCOMPtr<nsILocalFile> file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
53  NS_ENSURE_SUCCESS(rv, rv);
54 
55  rv = file->InitWithPath(aPath);
56  NS_ENSURE_SUCCESS(rv, rv);
57 
58  rv = sbOpenInputStream(file, aStream);
59  NS_ENSURE_SUCCESS(rv, rv);
60 
61  return NS_OK;
62 }
63 
64 nsresult sbOpenInputStream(nsIURI * aURI, nsIInputStream ** aStream)
65 {
66  NS_ENSURE_ARG_POINTER(aStream);
67  NS_ENSURE_ARG_POINTER(aURI);
68 
69  nsresult rv;
70  nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
71  NS_ENSURE_SUCCESS(rv, rv);
72 
73  nsCOMPtr<nsIFile> file;
74  rv = fileURL->GetFile(getter_AddRefs(file));
75  NS_ENSURE_SUCCESS(rv, rv);
76 
77  rv = sbOpenInputStream(file, aStream);
78  NS_ENSURE_SUCCESS(rv, rv);
79 
80  return NS_OK;
81 }
82 
83 nsresult sbOpenInputStream(nsIFile * aFile, nsIInputStream ** aStream)
84 {
85  NS_ENSURE_ARG_POINTER(aStream);
86  NS_ENSURE_ARG_POINTER(aFile);
87 
88  nsresult rv;
89  nsCOMPtr<nsIFileInputStream> fileStream =
90  do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv);
91  NS_ENSURE_SUCCESS(rv, rv);
92 
93  rv = fileStream->Init(aFile, -1, -1, 0);
94  NS_ENSURE_SUCCESS(rv, rv);
95 
96  nsCOMPtr<nsIInputStream> stream = do_QueryInterface(fileStream, &rv);
97  NS_ENSURE_SUCCESS(rv, rv);
98 
99  stream.forget(aStream);
100 
101  return NS_OK;
102 }
103 
104 nsresult sbOpenOutputStream(nsIFile * aFile, nsIOutputStream ** aStream)
105 {
106  NS_ENSURE_ARG_POINTER(aStream);
107  NS_ENSURE_ARG_POINTER(aFile);
108 
109  nsresult rv;
110  nsCOMPtr<nsIFileOutputStream> fileStream =
111  do_CreateInstance(NS_LOCALFILEOUTPUTSTREAM_CONTRACTID, &rv);
112  NS_ENSURE_SUCCESS(rv, rv);
113 
114  rv = fileStream->Init(aFile, -1, -1, 0);
115  NS_ENSURE_SUCCESS(rv, rv);
116 
117  nsCOMPtr<nsIOutputStream> stream = do_QueryInterface(fileStream, &rv);
118  NS_ENSURE_SUCCESS(rv, rv);
119 
120  stream.forget(aStream);
121 
122  return NS_OK;
123 }
124 
125 nsresult sbOpenOutputStream(nsAString const & aPath, nsIOutputStream ** aStream)
126 {
127  NS_ENSURE_ARG_POINTER(aStream);
128 
129  nsresult rv;
130  nsCOMPtr<nsILocalFile> file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv);
131  NS_ENSURE_SUCCESS(rv, rv);
132 
133  rv = file->InitWithPath(aPath);
134  NS_ENSURE_SUCCESS(rv, rv);
135 
136  rv = sbOpenOutputStream(file, aStream);
137  NS_ENSURE_SUCCESS(rv, rv);
138 
139  return NS_OK;
140 }
141 
142 
143 nsresult sbReadFile(nsIFile * aFile, nsACString &aBuffer)
144 {
145  NS_ENSURE_ARG_POINTER(aFile);
146 
147  nsresult rv;
148  PRInt64 fileSize;
149  rv = aFile->GetFileSize(&fileSize);
150  NS_ENSURE_SUCCESS(rv, rv);
151 
152  nsCOMPtr<nsIInputStream> stream;
153  rv = sbOpenInputStream(aFile, getter_AddRefs(stream));
154  NS_ENSURE_SUCCESS(rv, rv);
155 
156  rv = sbConsumeStream(stream, static_cast<PRUint32>(fileSize), aBuffer);
157  NS_ENSURE_SUCCESS(rv, rv);
158 
159  return NS_OK;
160 }
161 
162 /* from xpcom/io/nsStreamUtils.cpp
163  * (it's hidden in libxul, so we need a copy :( )
164  */
165 nsresult
166 sbConsumeStream(nsIInputStream *stream, PRUint32 maxCount, nsACString &result)
167 {
168  nsresult rv = NS_OK;
169  result.Truncate();
170 
171  while (maxCount) {
172  PRUint32 avail;
173  rv = stream->Available(&avail);
174  if (NS_FAILED(rv)) {
175  if (rv == NS_BASE_STREAM_CLOSED)
176  rv = NS_OK;
177  break;
178  }
179  if (avail == 0)
180  break;
181  if (avail > maxCount)
182  avail = maxCount;
183 
184  // resize result buffer
185  PRUint32 length = result.Length();
186  result.SetLength(length + avail);
187  if (result.Length() != (length + avail))
188  return NS_ERROR_OUT_OF_MEMORY;
189  char *buf = result.BeginWriting() + length;
190 
191  PRUint32 n;
192  rv = stream->Read(buf, avail, &n);
193  if (NS_FAILED(rv))
194  break;
195  if (n != avail)
196  result.SetLength(length + n);
197  if (n == 0)
198  break;
199  maxCount -= n;
200  }
201 
202  return rv;
203 }
204 
205 inline
206 nsCOMPtr<nsIIOService> GetIOService(nsresult & rv)
207 {
208  // Get the IO service.
209  if (NS_IsMainThread()) {
210  return do_GetIOService(&rv);
211  }
212  return do_ProxiedGetService(NS_IOSERVICE_CONTRACTID, &rv);
213 }
214 
215 nsresult
216 sbNewFileURI(nsIFile* aFile,
217  nsIURI** aURI)
218 {
219  NS_ENSURE_ARG_POINTER(aFile);
220  NS_ENSURE_ARG_POINTER(aURI);
221 
222  nsresult rv;
223 
224  // Get the IO service.
225  nsCOMPtr<nsIIOService> ioService = GetIOService(rv);
226  NS_ENSURE_SUCCESS(rv, rv);
227 
228  // Note that NewFileURI is broken on Linux when dealing with
229  // file names not in the filesystem charset; see bug 6227
230 #if XP_UNIX && !XP_MACOSX
231  nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(aFile, &rv);
232  if (NS_SUCCEEDED(rv)) {
233  // Use the local file persistent descriptor to form a URI spec.
234  nsCAutoString descriptor;
235  rv = localFile->GetPersistentDescriptor(descriptor);
236  if (NS_SUCCEEDED(rv)) {
237  // Escape the descriptor into a spec.
238  nsCOMPtr<nsINetUtil> netUtil =
239  do_CreateInstance("@mozilla.org/network/util;1", &rv);
240  NS_ENSURE_SUCCESS(rv, rv);
241  nsCAutoString spec;
242  rv = netUtil->EscapeString(descriptor,
243  nsINetUtil::ESCAPE_URL_PATH,
244  spec);
245  NS_ENSURE_SUCCESS(rv, rv);
246 
247  // Add the "file:" scheme.
248  spec.Insert("file://", 0);
249 
250  // Create the URI.
251  rv = SB_NewURI(aURI, spec);
252  NS_ENSURE_SUCCESS(rv, rv);
253 
254  return NS_OK;
255  }
256  }
257 #endif
258 
259  // Get a URI directly from the file.
260  nsCOMPtr<nsIURI> uri;
261  rv = ioService->NewFileURI(aFile, getter_AddRefs(uri));
262  NS_ENSURE_SUCCESS(rv, rv);
263 
264  // Get a main thread URI.
265  nsCOMPtr<nsIURI> mainThreadURI = do_MainThreadQueryInterface(uri, &rv);
266  NS_ENSURE_SUCCESS(rv, rv);
267 
268  // Return results.
269  mainThreadURI.forget(aURI);
270 
271  return NS_OK;
272 }
273 
274 void
275 RemoveBadFileNameCharacters(nsAString& aFileName,
276  PRBool aAllPlatforms)
277 {
278  // Get the list of bad characters.
279  const char* badCharacters;
280  if (aAllPlatforms)
281  badCharacters = SB_FILE_BAD_CHARACTERS_ALL_PLATFORMS;
282  else
283  badCharacters = SB_FILE_BAD_CHARACTERS;
284 
285  // Remove all bad characters from the file name.
286  aFileName.StripChars(badCharacters);
287 
288  // Windows does not like spaces at the begining or end of a file/folder name.
289  // Windows also does not like dots at the begining or end of the file/folder
290  // name and dots are bad as well on some other operating systems as they
291  // represent a hidden file.
292  aFileName.Trim(" .", PR_TRUE, PR_TRUE);
293 }
294 
#define SB_FILE_BAD_CHARACTERS_ALL_PLATFORMS
Definition: sbFileUtils.h:139
return NS_OK
_updateCookies aPath
nsresult sbOpenInputStream(nsAString const &aPath, nsIInputStream **aStream)
Definition: sbFileUtils.cpp:47
nsCOMPtr< nsIIOService > GetIOService(nsresult &rv)
nsresult sbReadFile(nsIFile *aFile, nsACString &aBuffer)
nsresult sbOpenOutputStream(nsIFile *aFile, nsIOutputStream **aStream)
#define SB_FILE_BAD_CHARACTERS
Definition: sbFileUtils.h:138
var ioService
static nsresult SB_NewURI(nsIURI **aURI, const nsACString &aSpec, const char *aCharSet=nsnull, nsIURI *aBaseURI=nsnull)
Definition: sbURIUtils.h:139
nsresult sbConsumeStream(nsIInputStream *stream, PRUint32 maxCount, nsACString &result)
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
sbMainThreadQueryInterface do_MainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult=nsnull)
var uri
Definition: FeedWriter.js:1135
nsresult sbNewFileURI(nsIFile *aFile, nsIURI **aURI)
void RemoveBadFileNameCharacters(nsAString &aFileName, PRBool aAllPlatforms)
var file