• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "volume/volume_info.h"
17 
18 #include <sys/mount.h>
19 
20 #include "storage_service_errno.h"
21 #include "storage_service_log.h"
22 #include "utils/string_utils.h"
23 #include "parameter.h"
24 
25 using namespace std;
26 namespace OHOS {
27 namespace StorageDaemon {
28 const int32_t TRUE_LEN = 5;
Create(const std::string volId,const std::string diskId,dev_t device)29 int32_t VolumeInfo::Create(const std::string volId, const std::string diskId, dev_t device)
30 {
31     id_ = volId;
32     diskId_ = diskId;
33     type_ = EXTERNAL;
34     mountState_ = UNMOUNTED;
35     mountFlags_ = 0;
36     userIdOwner_ = 0;
37 
38     std::string key = "usb.readonly";
39     int handle = static_cast<int>(FindParameter(key.c_str()));
40     if (handle != -1) {
41         char rdOnlyEnable[255] = {"false"};
42         auto res = GetParameterValue(handle, rdOnlyEnable, 255);
43         if (res >= 0 && strncmp(rdOnlyEnable, "true", TRUE_LEN) == 0) {
44             mountFlags_ |= MS_RDONLY;
45         }
46     }
47 
48     int32_t err = DoCreate(device);
49     if (err) {
50         return err;
51     }
52     return E_OK;
53 }
54 
GetVolumeId()55 std::string VolumeInfo::GetVolumeId()
56 {
57     return id_;
58 }
59 
GetVolumeType()60 int32_t VolumeInfo::GetVolumeType()
61 {
62     return type_;
63 }
64 
GetDiskId()65 std::string VolumeInfo::GetDiskId()
66 {
67     return diskId_;
68 }
69 
GetState()70 int32_t VolumeInfo::GetState()
71 {
72     return mountState_;
73 }
74 
Destroy()75 int32_t VolumeInfo::Destroy()
76 {
77     VolumeState state = REMOVED;
78     if (mountState_ == REMOVED || mountState_ == BADREMOVABLE) {
79         return E_OK;
80     }
81     if (mountState_ != UNMOUNTED) {
82         // force umount
83         UMount(true);
84         state = BADREMOVABLE;
85     }
86 
87     int32_t err = DoDestroy();
88     if (err) {
89         return err;
90     }
91     mountState_ = state;
92     return E_OK;
93 }
94 
Mount(uint32_t flags)95 int32_t VolumeInfo::Mount(uint32_t flags)
96 {
97     int32_t err = 0;
98 
99     if (mountState_ == MOUNTED) {
100         return E_OK;
101     }
102     if (mountState_ != CHECKING) {
103         LOGE("please check volume %{public}s first", GetVolumeId().c_str());
104         return E_VOL_STATE;
105     }
106 
107     mountFlags_ |= flags;
108     err = DoMount(mountFlags_);
109     if (err) {
110         mountState_ = UNMOUNTED;
111         return err;
112     }
113 
114     mountState_ = MOUNTED;
115     return E_OK;
116 }
117 
UMount(bool force)118 int32_t VolumeInfo::UMount(bool force)
119 {
120     int32_t err = 0;
121 
122     if (mountState_ == REMOVED || mountState_ == BADREMOVABLE) {
123         LOGE("the volume %{public}s is in REMOVED state", GetVolumeId().c_str());
124         return E_VOL_STATE;
125     }
126 
127     if (mountState_ == UNMOUNTED) {
128         return E_OK;
129     }
130 
131     if (mountState_ == CHECKING) {
132         mountState_ = UNMOUNTED;
133         return E_OK;
134     }
135 
136     if (mountState_ == EJECTING && !force) {
137         return E_WAIT;
138     }
139 
140     mountState_ = EJECTING;
141 
142     err = DoUMount(force);
143     if (!force && err) {
144         mountState_ = MOUNTED;
145         return err;
146     }
147 
148     mountState_ = UNMOUNTED;
149     return E_OK;
150 }
151 
Check()152 int32_t VolumeInfo::Check()
153 {
154     if (mountState_ != UNMOUNTED) {
155         LOGE("the volume %{public}s is not in UNMOUNT state", GetVolumeId().c_str());
156         return E_VOL_STATE;
157     }
158 
159     if (mountState_ == CHECKING) {
160         mountState_ = UNMOUNTED;
161     }
162 
163     int32_t err = DoCheck();
164     if (err) {
165         return err;
166     }
167     mountState_ = CHECKING;
168     return E_OK;
169 }
170 
Format(std::string type)171 int32_t VolumeInfo::Format(std::string type)
172 {
173     if (mountState_ != UNMOUNTED) {
174         LOGE("Please unmount the volume %{public}s first", GetVolumeId().c_str());
175         return E_VOL_STATE;
176     }
177 
178     int32_t err = DoFormat(type);
179     return err;
180 }
181 
SetVolumeDescription(const std::string description)182 int32_t VolumeInfo::SetVolumeDescription(const std::string description)
183 {
184     if (mountState_ != UNMOUNTED) {
185         LOGE("Please unmount the volume %{public}s first", GetVolumeId().c_str());
186         return E_VOL_STATE;
187     }
188 
189     int32_t err = DoSetVolDesc(description);
190     return err;
191 }
192 } // StorageDaemon
193 } // OHOS
194