Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/FileLocation.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef mozilla_FileLocation_h
8
#define mozilla_FileLocation_h
9
10
#include "nsString.h"
11
#include "nsCOMPtr.h"
12
#include "nsAutoPtr.h"
13
#include "nsIFile.h"
14
#include "FileUtils.h"
15
16
class nsZipArchive;
17
class nsZipItem;
18
19
namespace mozilla {
20
21
class FileLocation
22
{
23
public:
24
  /**
25
   * FileLocation is an helper to handle different kind of file locations
26
   * within Gecko:
27
   * - on filesystems
28
   * - in archives
29
   * - in archives within archives
30
   * As such, it stores a path within an archive, as well as the archive
31
   * path itself, or the complete file path alone when on a filesystem.
32
   * When the archive is in an archive, an nsZipArchive is stored instead
33
   * of a file path.
34
   */
35
  FileLocation();
36
  ~FileLocation();
37
38
  FileLocation(const FileLocation& aOther);
39
  FileLocation(FileLocation&& aOther);
40
41
24
  FileLocation& operator=(const FileLocation&) = default;
42
43
  /**
44
   * Constructor for plain files
45
   */
46
  explicit FileLocation(nsIFile* aFile);
47
48
  /**
49
   * Constructors for path within an archive. The archive can be given either
50
   * as nsIFile or nsZipArchive.
51
   */
52
  FileLocation(nsIFile* aZip, const char* aPath);
53
54
  FileLocation(nsZipArchive* aZip, const char* aPath);
55
56
  /**
57
   * Creates a new file location relative to another one.
58
   */
59
  FileLocation(const FileLocation& aFile, const char* aPath);
60
61
  /**
62
   * Initialization functions corresponding to constructors
63
   */
64
  void Init(nsIFile* aFile);
65
66
  void Init(nsIFile* aZip, const char* aPath);
67
68
  void Init(nsZipArchive* aZip, const char* aPath);
69
70
  /**
71
   * Returns an URI string corresponding to the file location
72
   */
73
  void GetURIString(nsACString& aResult) const;
74
75
  /**
76
   * Returns the base file of the location, where base file is defined as:
77
   * - The file itself when the location is on a filesystem
78
   * - The archive file when the location is in an archive
79
   * - The outer archive file when the location is in an archive in an archive
80
   */
81
  already_AddRefed<nsIFile> GetBaseFile();
82
83
18
  nsZipArchive* GetBaseZip() { return mBaseZip; }
84
85
  /**
86
   * Returns whether the "base file" (see GetBaseFile) is an archive
87
   */
88
1.04k
  bool IsZip() const { return !mPath.IsEmpty(); }
89
90
  /**
91
   * Returns the path within the archive, when within an archive
92
   */
93
9
  void GetPath(nsACString& aResult) const { aResult = mPath; }
94
95
  /**
96
   * Boolean value corresponding to whether the file location is initialized
97
   * or not.
98
   */
99
3
  explicit operator bool() const { return mBaseFile || mBaseZip; }
100
101
  /**
102
   * Returns whether another FileLocation points to the same resource
103
   */
104
  bool Equals(const FileLocation& aFile) const;
105
106
  /**
107
   * Data associated with a FileLocation.
108
   */
109
  class Data
110
  {
111
  public:
112
    /**
113
     * Returns the data size
114
     */
115
    nsresult GetSize(uint32_t* aResult);
116
117
    /**
118
     * Copies the data in the given buffer
119
     */
120
    nsresult Copy(char* aBuf, uint32_t aLen);
121
  protected:
122
    friend class FileLocation;
123
    nsZipItem* mItem;
124
    RefPtr<nsZipArchive> mZip;
125
    mozilla::AutoFDClose mFd;
126
  };
127
128
  /**
129
   * Returns the data associated with the resource pointed at by the file
130
   * location.
131
   */
132
  nsresult GetData(Data& aData);
133
private:
134
  nsCOMPtr<nsIFile> mBaseFile;
135
  RefPtr<nsZipArchive> mBaseZip;
136
  nsCString mPath;
137
}; /* class FileLocation */
138
139
} /* namespace mozilla */
140
141
#endif /* mozilla_FileLocation_h */