@@ -78,41 +78,27 @@ public function __construct(
7878
7979 public function registerMounts (IUser $ user , array $ mounts , array $ mountProviderClasses = null ) {
8080 $ this ->eventLogger ->start ('fs:setup:user:register ' , 'Registering mounts for user ' );
81- // filter out non-proper storages coming from unit tests
82- $ mounts = array_filter ($ mounts , function (IMountPoint $ mount ) {
83- return $ mount instanceof SharedMount || ($ mount ->getStorage () && $ mount ->getStorage ()->getCache ());
84- });
85- /** @var ICachedMountInfo[] $newMounts */
86- $ newMounts = array_map (function (IMountPoint $ mount ) use ($ user ) {
81+ /** @var array<string, ICachedMountInfo> $newMounts */
82+ $ newMounts = [];
83+ foreach ($ mounts as $ mount ) {
8784 // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
88- if ($ mount ->getStorageRootId () === -1 ) {
89- return null ;
90- } else {
91- return new LazyStorageMountInfo ($ user , $ mount );
85+ if ($ mount ->getStorageRootId () !== -1 ) {
86+ $ mountInfo = new LazyStorageMountInfo ($ user , $ mount );
87+ $ newMounts [$ mountInfo ->getKey ()] = $ mountInfo ;
9288 }
93- }, $ mounts );
94- $ newMounts = array_values (array_filter ($ newMounts ));
95- $ newMountKeys = array_map (function (ICachedMountInfo $ mount ) {
96- return $ mount ->getRootId () . ':: ' . $ mount ->getMountPoint ();
97- }, $ newMounts );
98- $ newMounts = array_combine ($ newMountKeys , $ newMounts );
89+ }
9990
10091 $ cachedMounts = $ this ->getMountsForUser ($ user );
10192 if (is_array ($ mountProviderClasses )) {
10293 $ cachedMounts = array_filter ($ cachedMounts , function (ICachedMountInfo $ mountInfo ) use ($ mountProviderClasses , $ newMounts ) {
10394 // for existing mounts that didn't have a mount provider set
10495 // we still want the ones that map to new mounts
105- $ mountKey = $ mountInfo ->getRootId () . ':: ' . $ mountInfo ->getMountPoint ();
106- if ($ mountInfo ->getMountProvider () === '' && isset ($ newMounts [$ mountKey ])) {
96+ if ($ mountInfo ->getMountProvider () === '' && isset ($ newMounts [$ mountInfo ->getKey ()])) {
10797 return true ;
10898 }
10999 return in_array ($ mountInfo ->getMountProvider (), $ mountProviderClasses );
110100 });
111101 }
112- $ cachedRootKeys = array_map (function (ICachedMountInfo $ mount ) {
113- return $ mount ->getRootId () . ':: ' . $ mount ->getMountPoint ();
114- }, $ cachedMounts );
115- $ cachedMounts = array_combine ($ cachedRootKeys , $ cachedMounts );
116102
117103 $ addedMounts = [];
118104 $ removedMounts = [];
@@ -131,46 +117,42 @@ public function registerMounts(IUser $user, array $mounts, array $mountProviderC
131117
132118 $ changedMounts = $ this ->findChangedMounts ($ newMounts , $ cachedMounts );
133119
134- $ this ->connection ->beginTransaction ();
135- try {
136- foreach ($ addedMounts as $ mount ) {
137- $ this ->addToCache ($ mount );
138- /** @psalm-suppress InvalidArgument */
139- $ this ->mountsForUsers [$ user ->getUID ()][] = $ mount ;
140- }
141- foreach ($ removedMounts as $ mount ) {
142- $ this ->removeFromCache ($ mount );
143- $ index = array_search ($ mount , $ this ->mountsForUsers [$ user ->getUID ()]);
144- unset($ this ->mountsForUsers [$ user ->getUID ()][$ index ]);
145- }
146- foreach ($ changedMounts as $ mount ) {
147- $ this ->updateCachedMount ($ mount );
120+ if ($ addedMounts || $ removedMounts || $ changedMounts ) {
121+ $ this ->connection ->beginTransaction ();
122+ try {
123+ foreach ($ addedMounts as $ mount ) {
124+ $ this ->addToCache ($ mount );
125+ /** @psalm-suppress InvalidArgument */
126+ $ this ->mountsForUsers [$ user ->getUID ()][] = $ mount ;
127+ }
128+ foreach ($ removedMounts as $ mount ) {
129+ $ this ->removeFromCache ($ mount );
130+ $ index = array_search ($ mount , $ this ->mountsForUsers [$ user ->getUID ()]);
131+ unset($ this ->mountsForUsers [$ user ->getUID ()][$ index ]);
132+ }
133+ foreach ($ changedMounts as $ mount ) {
134+ $ this ->updateCachedMount ($ mount );
135+ }
136+ $ this ->connection ->commit ();
137+ } catch (\Throwable $ e ) {
138+ $ this ->connection ->rollBack ();
139+ throw $ e ;
148140 }
149- $ this ->connection ->commit ();
150- } catch (\Throwable $ e ) {
151- $ this ->connection ->rollBack ();
152- throw $ e ;
153141 }
154142 $ this ->eventLogger ->end ('fs:setup:user:register ' );
155143 }
156144
157145 /**
158- * @param ICachedMountInfo[] $newMounts
159- * @param ICachedMountInfo[] $cachedMounts
146+ * @param array<string, ICachedMountInfo> $newMounts
147+ * @param array<string, ICachedMountInfo> $cachedMounts
160148 * @return ICachedMountInfo[]
161149 */
162150 private function findChangedMounts (array $ newMounts , array $ cachedMounts ) {
163- $ new = [];
164- foreach ($ newMounts as $ mount ) {
165- $ new [$ mount ->getRootId () . ':: ' . $ mount ->getMountPoint ()] = $ mount ;
166- }
167151 $ changed = [];
168- foreach ($ cachedMounts as $ cachedMount ) {
169- $ key = $ cachedMount ->getRootId () . ':: ' . $ cachedMount ->getMountPoint ();
170- if (isset ($ new [$ key ])) {
171- $ newMount = $ new [$ key ];
152+ foreach ($ cachedMounts as $ key => $ cachedMount ) {
153+ if (isset ($ newMounts [$ key ])) {
154+ $ newMount = $ newMounts [$ key ];
172155 if (
173- $ newMount ->getMountPoint () !== $ cachedMount ->getMountPoint () ||
174156 $ newMount ->getStorageId () !== $ cachedMount ->getStorageId () ||
175157 $ newMount ->getMountId () !== $ cachedMount ->getMountId () ||
176158 $ newMount ->getMountProvider () !== $ cachedMount ->getMountProvider ()
@@ -258,7 +240,12 @@ public function getMountsForUser(IUser $user) {
258240 $ rows = $ result ->fetchAll ();
259241 $ result ->closeCursor ();
260242
261- $ this ->mountsForUsers [$ user ->getUID ()] = array_filter (array_map ([$ this , 'dbRowToMountInfo ' ], $ rows ));
243+ $ this ->mountsForUsers [$ user ->getUID ()] = [];
244+ /** @var array<string, ICachedMountInfo> $mounts */
245+ foreach ($ rows as $ row ) {
246+ $ mount = $ this ->dbRowToMountInfo ($ row );
247+ $ this ->mountsForUsers [$ user ->getUID ()][$ mount ->getKey ()] = $ mount ;
248+ }
262249 }
263250 return $ this ->mountsForUsers [$ user ->getUID ()];
264251 }
0 commit comments