DefaultPieceStorage may be referenced by one of DHT task (e.g.,
DHTPeerLookupTask), after RequestGroup was deleted, and even after
RequestGroupMan was deleted. DefaultPieceStorage has a reference to
MultiDiskAdaptor which calls RequestGroupMan object on destruction.
So when DHT task is destroyed, DefaultPieceStorage is destroyed, which
in turn destroys MultiDiskAdaptor. DHT task is destroyed after
RequestGroupMan was destroyed, MultiDiskAdaptor will use now freed
RequestGroupMan object, this is use-after-free.
When the directory is changed via aria2.changeOption RPC method, we
directly change first FileEntry's path using FileEntry::setPath(). If
there is no PREF_OUT option is given, basically file name is unknown,
so we just set empty string and let the next run determine the correct
file name and new directory is applied there. But previous code does
not reset length property of FileEntry, so the unexpected code path is
taken when unpaused and its path expects path is not empty string.
This commit fixes this issue by setting length to 0 using
FileEntry::setLength().
This is a slight optimization not to cause useless disk access. This
only applies to saving session automatically (see
--save-session-interval). aria2.saveSession and serialization at the
end of the session are always performed as before.
When serialization, we first check that whether there is any change
since the last serialization. To do this, we first calculate hash
value of serialized content without writing into file. Then compare
this value to the value of last serialization. If they do not match,
perform serialization.
This is a slight optimization not to cause useless disk access. This
only applies to saving session automatically (see
--save-session-interval). aria2.saveSession and serialization at the
end of the session are always performed as before.
In my quest to explore the code and understand it, why not clean it up
in the progress. Most formatting provided by clang-format. ;)
Aside from formatting, also extracted some method and unnested some
control structs.
Previously we disabled conditional-get if file part is missing in URI.
But we use constant string "index.html" in this case, so we can do the
same to determine the modification time. In this patch, if we have
file part in URI, we are not going to set absolute file path in
FileEntry, since it prevents content-disposition from working.