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
13 #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
36 FileLocation(const FileLocation
& aOther
);
37 FileLocation(FileLocation
&& aOther
);
39 FileLocation
& operator=(const FileLocation
&) = default;
42 * Constructor for plain files
44 explicit FileLocation(nsIFile
* aFile
);
47 * Constructors for path within an archive. The archive can be given either
48 * as nsIFile or nsZipArchive.
50 FileLocation(nsIFile
* aZip
, const char* aPath
);
52 FileLocation(nsZipArchive
* aZip
, const char* aPath
);
55 * Creates a new file location relative to another one.
57 FileLocation(const FileLocation
& aFile
, const char* aPath
);
60 * Initialization functions corresponding to constructors
62 void Init(nsIFile
* aFile
);
64 void Init(nsIFile
* aZip
, const char* aPath
);
66 void Init(nsZipArchive
* aZip
, const char* aPath
);
69 * Returns an URI string corresponding to the file location
71 void GetURIString(nsACString
& aResult
) const;
74 * Returns the base file of the location, where base file is defined as:
75 * - The file itself when the location is on a filesystem
76 * - The archive file when the location is in an archive
77 * - The outer archive file when the location is in an archive in an archive
79 already_AddRefed
<nsIFile
> GetBaseFile();
81 nsZipArchive
* GetBaseZip() { return mBaseZip
; }
84 * Returns whether the "base file" (see GetBaseFile) is an archive
86 bool IsZip() const { return !mPath
.IsEmpty(); }
89 * Returns the path within the archive, when within an archive
91 void GetPath(nsACString
& aResult
) const { aResult
= mPath
; }
94 * Boolean value corresponding to whether the file location is initialized
97 explicit operator bool() const { return mBaseFile
|| mBaseZip
; }
100 * Returns whether another FileLocation points to the same resource
102 bool Equals(const FileLocation
& aFile
) const;
105 * Data associated with a FileLocation.
110 * Returns the data size
112 nsresult
GetSize(uint32_t* aResult
);
115 * Copies the data in the given buffer
117 nsresult
Copy(char* aBuf
, uint32_t aLen
);
120 friend class FileLocation
;
122 RefPtr
<nsZipArchive
> mZip
;
123 mozilla::AutoFDClose mFd
;
127 * Returns the data associated with the resource pointed at by the file
130 nsresult
GetData(Data
& aData
);
133 nsCOMPtr
<nsIFile
> mBaseFile
;
134 RefPtr
<nsZipArchive
> mBaseZip
;
136 }; /* class FileLocation */
138 } /* namespace mozilla */
140 #endif /* mozilla_FileLocation_h */