MTP: Add support for reserve storage setting to avoid low storage situations.

Set resource config_mtpReserveSpaceMegabytes to number of megabytes to reserve.
If MTP has dedicated storage this value should be zero, but if MTP is
sharing storage with the rest of the system, set this to a positive value
to ensure that MTP activity does not result in the storage being
too close to full.

BUG: 3250924

Change-Id: I881c87240da268bad1ea1b99ad03673ab85ffdbf
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/media/mtp/MtpServer.cpp b/media/mtp/MtpServer.cpp
index d65845d..b371e41 100644
--- a/media/mtp/MtpServer.cpp
+++ b/media/mtp/MtpServer.cpp
@@ -104,10 +104,10 @@
 MtpServer::~MtpServer() {
 }
 
-void MtpServer::addStorage(const char* filePath) {
+void MtpServer::addStorage(const char* filePath, uint64_t reserveSpace) {
     int index = mStorages.size() + 1;
     index |= index << 16;   // set high and low part to our index
-    MtpStorage* storage = new MtpStorage(index, filePath, mDatabase);
+    MtpStorage* storage = new MtpStorage(index, filePath, reserveSpace);
     addStorage(storage);
 }
 
@@ -687,6 +687,10 @@
     if (access(path, R_OK) == 0)
         return MTP_RESPONSE_GENERAL_ERROR;
 
+    // check space first
+    if (mSendObjectFileSize > storage->getFreeSpace())
+        return MTP_RESPONSE_STORAGE_FULL;
+
     MtpObjectHandle handle = mDatabase->beginSendObject((const char*)path,
             format, parent, storageID, mSendObjectFileSize, modifiedTime);
     if (handle == kInvalidObjectHandle) {
diff --git a/media/mtp/MtpServer.h b/media/mtp/MtpServer.h
index 5aee4ea..605d5a2 100644
--- a/media/mtp/MtpServer.h
+++ b/media/mtp/MtpServer.h
@@ -67,7 +67,7 @@
                                     int fileGroup, int filePerm, int directoryPerm);
     virtual             ~MtpServer();
 
-    void                addStorage(const char* filePath);
+    void                addStorage(const char* filePath, uint64_t reserveSpace);
     inline void         addStorage(MtpStorage* storage) { mStorages.push(storage); }
     MtpStorage*         getStorage(MtpStorageID id);
     void                run();
diff --git a/media/mtp/MtpStorage.cpp b/media/mtp/MtpStorage.cpp
index eccf186..abc23de 100644
--- a/media/mtp/MtpStorage.cpp
+++ b/media/mtp/MtpStorage.cpp
@@ -32,11 +32,11 @@
 
 namespace android {
 
-MtpStorage::MtpStorage(MtpStorageID id, const char* filePath, MtpDatabase* db)
+MtpStorage::MtpStorage(MtpStorageID id, const char* filePath, uint64_t reserveSpace)
     :   mStorageID(id),
         mFilePath(filePath),
-        mDatabase(db),
-        mMaxCapacity(0)
+        mMaxCapacity(0),
+        mReserveSpace(reserveSpace)
 {
     LOGD("MtpStorage id: %d path: %s\n", id, filePath);
 }
@@ -70,7 +70,8 @@
     struct statfs   stat;
     if (statfs(mFilePath, &stat))
         return -1;
-    return (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
+    uint64_t freeSpace = (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
+    return (freeSpace > mReserveSpace ? freeSpace - mReserveSpace : 0);
 }
 
 const char* MtpStorage::getDescription() const {
diff --git a/media/mtp/MtpStorage.h b/media/mtp/MtpStorage.h
index b13b926..ace720b 100644
--- a/media/mtp/MtpStorage.h
+++ b/media/mtp/MtpStorage.h
@@ -28,11 +28,13 @@
 private:
     MtpStorageID            mStorageID;
     const char*             mFilePath;
-    MtpDatabase*            mDatabase;
     uint64_t                mMaxCapacity;
+    // amount of free space to leave unallocated
+    uint64_t                mReserveSpace;
 
 public:
-                            MtpStorage(MtpStorageID id, const char* filePath, MtpDatabase* db);
+                            MtpStorage(MtpStorageID id, const char* filePath,
+                                    uint64_t reserveSpace);
     virtual                 ~MtpStorage();
 
     inline MtpStorageID     getStorageID() const { return mStorageID; }