1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_FileLocation_h
6 #define mozilla_FileLocation_h
10 #include "nsAutoPtr.h"
12 #include "FileUtils.h"
23 * FileLocation is an helper to handle different kind of file locations
27 * - in archives within archives
28 * As such, it stores a path within an archive, as well as the archive
29 * path itself, or the complete file path alone when on a filesystem.
30 * When the archive is in an archive, an nsZipArchive is stored instead
37 * Constructor for plain files
39 explicit FileLocation(nsIFile
* aFile
);
42 * Constructors for path within an archive. The archive can be given either
43 * as nsIFile or nsZipArchive.
45 FileLocation(nsIFile
* aZip
, const char* aPath
);
47 FileLocation(nsZipArchive
* aZip
, const char* aPath
);
50 * Creates a new file location relative to another one.
52 FileLocation(const FileLocation
& aFile
, const char* aPath
= nullptr);
55 * Initialization functions corresponding to constructors
57 void Init(nsIFile
* aFile
);
59 void Init(nsIFile
* aZip
, const char* aPath
);
61 void Init(nsZipArchive
* aZip
, const char* aPath
);
64 * Returns an URI string corresponding to the file location
66 void GetURIString(nsACString
& aResult
) const;
69 * Returns the base file of the location, where base file is defined as:
70 * - The file itself when the location is on a filesystem
71 * - The archive file when the location is in an archive
72 * - The outer archive file when the location is in an archive in an archive
74 already_AddRefed
<nsIFile
> GetBaseFile();
77 * Returns whether the "base file" (see GetBaseFile) is an archive
79 bool IsZip() const { return !mPath
.IsEmpty(); }
82 * Returns the path within the archive, when within an archive
84 void GetPath(nsACString
& aResult
) const { aResult
= mPath
; }
87 * Boolean value corresponding to whether the file location is initialized
90 operator bool() const { return mBaseFile
|| mBaseZip
; }
93 * Returns whether another FileLocation points to the same resource
95 bool Equals(const FileLocation
& aFile
) const;
98 * Data associated with a FileLocation.
104 * Returns the data size
106 nsresult
GetSize(uint32_t* aResult
);
109 * Copies the data in the given buffer
111 nsresult
Copy(char* aBuf
, uint32_t aLen
);
113 friend class FileLocation
;
115 nsRefPtr
<nsZipArchive
> mZip
;
116 mozilla::AutoFDClose mFd
;
120 * Returns the data associated with the resource pointed at by the file
123 nsresult
GetData(Data
& aData
);
125 nsCOMPtr
<nsIFile
> mBaseFile
;
126 nsRefPtr
<nsZipArchive
> mBaseZip
;
128 }; /* class FileLocation */
130 } /* namespace mozilla */
132 #endif /* mozilla_FileLocation_h */