2012-05-21 04:12:37 -07: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 02:03:36 -08:00
|
|
|
|
|
|
|
#ifndef mozilla_FileLocation_h
|
|
|
|
#define mozilla_FileLocation_h
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2011-12-08 02:03:36 -08:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
FileLocation() { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for plain files
|
|
|
|
*/
|
2012-06-05 19:08:30 -07:00
|
|
|
FileLocation(nsIFile *file)
|
2011-12-08 02:03:36 -08:00
|
|
|
{
|
|
|
|
Init(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructors for path within an archive. The archive can be given either
|
2012-06-05 19:08:30 -07:00
|
|
|
* as nsIFile or nsZipArchive.
|
2011-12-08 02:03:36 -08:00
|
|
|
*/
|
2012-06-05 19:08:30 -07:00
|
|
|
FileLocation(nsIFile *zip, const char *path)
|
2011-12-08 02:03:36 -08:00
|
|
|
{
|
|
|
|
Init(zip, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLocation(nsZipArchive *zip, const char *path)
|
|
|
|
{
|
|
|
|
Init(zip, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new file location relative to another one.
|
|
|
|
*/
|
|
|
|
FileLocation(const FileLocation &file, const char *path = NULL);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization functions corresponding to constructors
|
|
|
|
*/
|
2012-06-05 19:08:30 -07:00
|
|
|
void Init(nsIFile *file)
|
2011-12-08 02:03:36 -08:00
|
|
|
{
|
|
|
|
mBaseZip = NULL;
|
|
|
|
mBaseFile = file;
|
|
|
|
mPath.Truncate();
|
|
|
|
}
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
void Init(nsIFile *zip, const char *path)
|
2011-12-08 02:03:36 -08:00
|
|
|
{
|
|
|
|
mBaseZip = NULL;
|
|
|
|
mBaseFile = zip;
|
|
|
|
mPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(nsZipArchive *zip, const char *path)
|
|
|
|
{
|
|
|
|
mBaseZip = zip;
|
|
|
|
mBaseFile = NULL;
|
|
|
|
mPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-05 19:08:30 -07:00
|
|
|
already_AddRefed<nsIFile> GetBaseFile();
|
2011-12-08 02:03:36 -08: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 08:56:38 -07:00
|
|
|
nsresult GetSize(uint32_t *result);
|
2011-12-08 02:03:36 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the data in the given buffer
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult Copy(char *buf, uint32_t len);
|
2011-12-08 02:03:36 -08: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-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> mBaseFile;
|
2011-12-08 02:03:36 -08:00
|
|
|
nsRefPtr<nsZipArchive> mBaseZip;
|
|
|
|
nsCString mPath;
|
|
|
|
}; /* class FileLocation */
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* mozilla_FileLocation_h */
|