adb: don't divide by zero

If we stat a file and get a size of 0, and then successfully read bytes
from that file, we would previously divide by zero when calculating the
percentage completion of the file. This case happens either when we're
racing against something else writing to the file, or when we're pulling
magical files such as the ones in /dev/cpuctl/ that lie about their
size.

Bug: http://b/25925733
Change-Id: I980b9c14f44a1eb4a42bc8736c94fa6db06c08d1
This commit is contained in:
Josh Gao 2015-11-30 10:53:22 -08:00
parent 0380d49024
commit b0e039f4ca
1 changed files with 17 additions and 4 deletions

View File

@ -190,8 +190,15 @@ class SyncConnection {
bytes_copied += ret;
int percentage = static_cast<int>(bytes_copied * 100 / total_size);
Printf("%s: %d%%", rpath, percentage);
if (total_size == 0) {
// This case can happen if we're racing against something that wrote to the file
// between our stat and our read, or if we're reading a magic file that lies about
// its size.
Printf("%s: ?%%", rpath);
} else {
int percentage = static_cast<int>(bytes_copied * 100 / total_size);
Printf("%s: %d%%", rpath, percentage);
}
}
adb_close(lfd);
@ -458,8 +465,14 @@ static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath)
bytes_copied += msg.data.size;
int percentage = static_cast<int>(bytes_copied * 100 / size);
sc.Printf("%s: %d%%", rpath, percentage);
if (size == 0) {
// This case can happen if we're racing against something that wrote to the file between
// our stat and our read, or if we're reading a magic file that lies about its size.
sc.Printf("%s: ?%%", rpath);
} else {
int percentage = static_cast<int>(bytes_copied * 100 / size);
sc.Printf("%s: %d%%", rpath, percentage);
}
}
adb_close(lfd);