1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_FileLocation_h
8 #define mozilla_FileLocation_h
12 #include "nsAutoPtr.h"
14 #include "FileUtils.h"
24 * FileLocation is an helper to handle different kind of file locations
28 * - in archives within archives
29 * As such, it stores a path within an archive, as well as the archive
30 * path itself, or the complete file path alone when on a filesystem.
31 * When the archive is in an archive, an nsZipArchive is stored instead
37 FileLocation(const FileLocation
& aOther
);
38 FileLocation(FileLocation
&& aOther
);
40 FileLocation
& operator=(const FileLocation
&) = default;
43 * Constructor for plain files
45 explicit FileLocation(nsIFile
* aFile
);
48 * Constructors for path within an archive. The archive can be given either
49 * as nsIFile or nsZipArchive.
51 FileLocation(nsIFile
* aZip
, const char* aPath
);
53 FileLocation(nsZipArchive
* aZip
, const char* aPath
);
56 * Creates a new file location relative to another one.
58 FileLocation(const FileLocation
& aFile
, const char* aPath
);
61 * Initialization functions corresponding to constructors
63 void Init(nsIFile
* aFile
);
65 void Init(nsIFile
* aZip
, const char* aPath
);
67 void Init(nsZipArchive
* aZip
, const char* aPath
);
70 * Returns an URI string corresponding to the file location
72 void GetURIString(nsACString
& aResult
) const;
75 * Returns the base file of the location, where base file is defined as:
76 * - The file itself when the location is on a filesystem
77 * - The archive file when the location is in an archive
78 * - The outer archive file when the location is in an archive in an archive
80 already_AddRefed
<nsIFile
> GetBaseFile();
82 nsZipArchive
* GetBaseZip() { return mBaseZip
; }
85 * Returns whether the "base file" (see GetBaseFile) is an archive
87 bool IsZip() const { return !mPath
.IsEmpty(); }
90 * Returns the path within the archive, when within an archive
92 void GetPath(nsACString
& aResult
) const { aResult
= mPath
; }
95 * Boolean value corresponding to whether the file location is initialized
98 explicit operator bool() const { return mBaseFile
|| mBaseZip
; }
101 * Returns whether another FileLocation points to the same resource
103 bool Equals(const FileLocation
& aFile
) const;
106 * Data associated with a FileLocation.
111 * Returns the data size
113 nsresult
GetSize(uint32_t* aResult
);
116 * Copies the data in the given buffer
118 nsresult
Copy(char* aBuf
, uint32_t aLen
);
121 friend class FileLocation
;
123 RefPtr
<nsZipArchive
> mZip
;
124 mozilla::AutoFDClose mFd
;
128 * Returns the data associated with the resource pointed at by the file
131 nsresult
GetData(Data
& aData
);
134 nsCOMPtr
<nsIFile
> mBaseFile
;
135 RefPtr
<nsZipArchive
> mBaseZip
;
137 }; /* class FileLocation */
139 } /* namespace mozilla */
141 #endif /* mozilla_FileLocation_h */