2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_FileLocation_h
|
|
|
|
#define mozilla_FileLocation_h
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2011-12-08 14:03:36 +04:00
|
|
|
#include "FileUtils.h"
|
|
|
|
|
|
|
|
class nsZipArchive;
|
|
|
|
class nsZipItem;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class FileLocation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* FileLocation is an helper to handle different kind of file locations
|
|
|
|
* within Gecko:
|
|
|
|
* - on filesystems
|
|
|
|
* - in archives
|
|
|
|
* - in archives within archives
|
|
|
|
* As such, it stores a path within an archive, as well as the archive
|
|
|
|
* path itself, or the complete file path alone when on a filesystem.
|
|
|
|
* When the archive is in an archive, an nsZipArchive is stored instead
|
|
|
|
* of a file path.
|
|
|
|
*/
|
2013-09-23 21:26:47 +04:00
|
|
|
FileLocation();
|
|
|
|
~FileLocation();
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for plain files
|
|
|
|
*/
|
2013-09-23 21:26:47 +04:00
|
|
|
FileLocation(nsIFile *file);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructors for path within an archive. The archive can be given either
|
2012-06-06 06:08:30 +04:00
|
|
|
* as nsIFile or nsZipArchive.
|
2011-12-08 14:03:36 +04:00
|
|
|
*/
|
2013-09-23 21:26:47 +04:00
|
|
|
FileLocation(nsIFile *zip, const char *path);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
2013-09-23 21:26:47 +04:00
|
|
|
FileLocation(nsZipArchive *zip, const char *path);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new file location relative to another one.
|
|
|
|
*/
|
2013-10-11 00:41:00 +04:00
|
|
|
FileLocation(const FileLocation &file, const char *path = nullptr);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization functions corresponding to constructors
|
|
|
|
*/
|
2013-09-23 21:26:47 +04:00
|
|
|
void Init(nsIFile *file);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
2013-09-23 21:26:47 +04:00
|
|
|
void Init(nsIFile *zip, const char *path);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
2013-09-23 21:26:47 +04:00
|
|
|
void Init(nsZipArchive *zip, const char *path);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an URI string corresponding to the file location
|
|
|
|
*/
|
|
|
|
void GetURIString(nsACString &result) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the base file of the location, where base file is defined as:
|
|
|
|
* - The file itself when the location is on a filesystem
|
|
|
|
* - The archive file when the location is in an archive
|
|
|
|
* - The outer archive file when the location is in an archive in an archive
|
|
|
|
*/
|
2012-06-06 06:08:30 +04:00
|
|
|
already_AddRefed<nsIFile> GetBaseFile();
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the "base file" (see GetBaseFile) is an archive
|
|
|
|
*/
|
|
|
|
bool IsZip() const
|
|
|
|
{
|
|
|
|
return !mPath.IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the path within the archive, when within an archive
|
|
|
|
*/
|
|
|
|
void GetPath(nsACString &result) const
|
|
|
|
{
|
|
|
|
result = mPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Boolean value corresponding to whether the file location is initialized
|
|
|
|
* or not.
|
|
|
|
*/
|
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return mBaseFile || mBaseZip;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether another FileLocation points to the same resource
|
|
|
|
*/
|
|
|
|
bool Equals(const FileLocation &file) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data associated with a FileLocation.
|
|
|
|
*/
|
|
|
|
class Data
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns the data size
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult GetSize(uint32_t *result);
|
2011-12-08 14:03:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the data in the given buffer
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Copy(char *buf, uint32_t len);
|
2011-12-08 14:03:36 +04:00
|
|
|
protected:
|
|
|
|
friend class FileLocation;
|
|
|
|
nsZipItem *mItem;
|
|
|
|
nsRefPtr<nsZipArchive> mZip;
|
|
|
|
mozilla::AutoFDClose mFd;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data associated with the resource pointed at by the file
|
|
|
|
* location.
|
|
|
|
*/
|
|
|
|
nsresult GetData(Data &data);
|
|
|
|
private:
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> mBaseFile;
|
2011-12-08 14:03:36 +04:00
|
|
|
nsRefPtr<nsZipArchive> mBaseZip;
|
|
|
|
nsCString mPath;
|
|
|
|
}; /* class FileLocation */
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* mozilla_FileLocation_h */
|