Use unique_fd

Also allow the state just before doMount() as a valid state for setting
fuse fd.
Test: manual
BUG:140173712

Change-Id: I012f8a83fef00e68f33010954fbc2ebc53cf8f1d
diff --git a/model/EmulatedVolume.cpp b/model/EmulatedVolume.cpp
index ca314ef..022ff42 100644
--- a/model/EmulatedVolume.cpp
+++ b/model/EmulatedVolume.cpp
@@ -88,13 +88,13 @@
 
     if (isFuse) {
         LOG(INFO) << "Mounting emulated fuse volume";
-        int fd = -1;
+        android::base::unique_fd fd;
         int result = MountUserFuse(getMountUserId(), label, &fd);
         if (result != 0) {
             PLOG(ERROR) << "Failed to mount emulated fuse volume";
             return -result;
         }
-        setDeviceFd(fd);
+        setFuseFd(std::move(fd));
         return OK;
     }
 
@@ -170,7 +170,7 @@
         }
 
         rmdir(path.c_str());
-        setDeviceFd(-1);
+        setFuseFd(android::base::unique_fd());
         return OK;
     }
 
diff --git a/model/PublicVolume.cpp b/model/PublicVolume.cpp
index 2c0b4cc..ebcb91a 100644
--- a/model/PublicVolume.cpp
+++ b/model/PublicVolume.cpp
@@ -173,13 +173,13 @@
 
     if (isFuse) {
         LOG(INFO) << "Mounting public fuse volume";
-        int fd = -1;
+        android::base::unique_fd fd;
         int result = MountUserFuse(getMountUserId(), stableName, &fd);
         if (result != 0) {
             LOG(ERROR) << "Failed to mount public fuse volume";
             return -result;
         }
-        setDeviceFd(fd);
+        setFuseFd(std::move(fd));
         return OK;
     }
 
@@ -265,7 +265,7 @@
         }
 
         rmdir(path.c_str());
-        setDeviceFd(-1);
+        setFuseFd(android::base::unique_fd());
         return OK;
     }
 
diff --git a/model/VolumeBase.cpp b/model/VolumeBase.cpp
index fa007ad..d6e4288 100644
--- a/model/VolumeBase.cpp
+++ b/model/VolumeBase.cpp
@@ -143,13 +143,13 @@
     return OK;
 }
 
-status_t VolumeBase::setDeviceFd(int deviceFd) {
-    if ((mState != State::kChecking)) {
-        LOG(WARNING) << getId() << " device fd change requires state checking";
+status_t VolumeBase::setFuseFd(android::base::unique_fd fuseFd) {
+    if ((mState != State::kChecking) && (mState != State::kEjecting)) {
+        LOG(WARNING) << getId() << " fuse fd change requires state checking or ejecting";
         return -EBUSY;
     }
 
-    mDeviceFd.reset(deviceFd);
+    mFuseFd.reset(std::move(fuseFd.get()));
     return OK;
 }
 
diff --git a/model/VolumeBase.h b/model/VolumeBase.h
index fa9eee4..6afb88e 100644
--- a/model/VolumeBase.h
+++ b/model/VolumeBase.h
@@ -87,7 +87,7 @@
     State getState() const { return mState; }
     const std::string& getPath() const { return mPath; }
     const std::string& getInternalPath() const { return mInternalPath; }
-    int getDeviceFd() const { return dup(mDeviceFd.get()); }
+    const android::base::unique_fd& getFuseFd() const { return mFuseFd; }
 
     status_t setDiskId(const std::string& diskId);
     status_t setPartGuid(const std::string& partGuid);
@@ -122,7 +122,7 @@
     status_t setId(const std::string& id);
     status_t setPath(const std::string& path);
     status_t setInternalPath(const std::string& internalPath);
-    status_t setDeviceFd(int deviceFd);
+    status_t setFuseFd(android::base::unique_fd fuseFd);
 
     android::sp<android::os::IVoldListener> getListener() const;
 
@@ -150,7 +150,7 @@
     /* Flag indicating that volume should emit no events */
     bool mSilent;
     /* The filedescriptor for the fuse device, if the volume uses fuse, or -1 otherwise */
-    android::base::unique_fd mDeviceFd;
+    android::base::unique_fd mFuseFd;
 
     /* Volumes stacked on top of this volume */
     std::list<std::shared_ptr<VolumeBase>> mVolumes;