@@ -103,6 +103,48 @@ FileInfoRef FileCache::info(const FileExtension& extension,
103103 return FileInfoRef (i->second .get ());
104104}
105105
106+ void FileCache::purge ()
107+ {
108+ // Convenience.
109+ auto & fsAccess = client ().fsAccess ();
110+
111+ auto dirAccess = fsAccess.newdiraccess ();
112+ auto path = mCachePath ;
113+
114+ // Try and open the cache directory for iteration.
115+ if (!dirAccess->dopen (&path, nullptr , false ))
116+ return ;
117+
118+ LocalPath name;
119+ nodetype_t type;
120+
121+ // Iterate over each file the cache.
122+ while (dirAccess->dnext (path, name, false , &type))
123+ {
124+ // Entry isn't a file.
125+ if (type != FILENODE)
126+ continue ;
127+
128+ // Convert file name to inode ID.
129+ auto id = InodeID::fromFileName (name.toPath (false ));
130+
131+ // Invalid ID.
132+ if (!id)
133+ continue ;
134+
135+ // Inode's still present in the database.
136+ if (mContext .mInodeDB .exists (id))
137+ continue ;
138+
139+ LocalPath newPath{path};
140+ newPath.appendWithSeparator (name, true );
141+
142+ // Try and remove the file.
143+ if (!fsAccess.unlinklocal (newPath))
144+ FUSEWarningF (" Couldn't remove stale cache file: %s" , newPath.toPath (false ).c_str ());
145+ }
146+ }
147+
106148void FileCache::remove (const FileIOContext& context,
107149 FileCacheLock lock)
108150{
@@ -160,10 +202,6 @@ FileCache::FileCache(platform::ServiceContext& context)
160202 FUSEDebug1 (" File Cache constructed" );
161203
162204 ensureCachePathExists (client (), mCachePath );
163-
164- // Prevent others, especially file explorer, from opening files under the folder, generating
165- // thumbnail while we're running. We have seen we're blocked to open files forever due to this.
166- WINDOWS_ONLY (mFolderLocker = platform::FolderLocker{mCachePath .asPlatformEncoded (true )});
167205}
168206
169207FileCache::~FileCache ()
@@ -300,44 +338,15 @@ ErrorOr<FileInfoRef> FileCache::create(const FileExtension& extension,
300338
301339void FileCache::current ()
302340{
303- // Convenience.
304- auto & fsAccess = client (). fsAccess ( );
341+ // Preventive
342+ WINDOWS_ONLY ( mFolderLocker . release () );
305343
306- auto dirAccess = fsAccess.newdiraccess ();
307- auto path = mCachePath ;
308-
309- // Try and open the cache directory for iteration.
310- if (!dirAccess->dopen (&path, nullptr , false ))
311- return ;
344+ // Purge any unreferenced files in the cache.
345+ purge ();
312346
313- LocalPath name;
314- nodetype_t type;
315-
316- // Iterate over each file the cache.
317- while (dirAccess->dnext (path, name, false , &type))
318- {
319- // Entry isn't a file.
320- if (type != FILENODE)
321- continue ;
322-
323- // Convert file name to inode ID.
324- auto id = InodeID::fromFileName (name.toPath (false ));
325-
326- // Invalid ID.
327- if (!id)
328- continue ;
329-
330- // Inode's still present in the database.
331- if (mContext .mInodeDB .exists (id))
332- continue ;
333-
334- LocalPath newPath{path};
335- newPath.appendWithSeparator (name, true );
336-
337- // Try and remove the file.
338- if (!fsAccess.unlinklocal (newPath))
339- FUSEWarningF (" Couldn't remove stale cache file: %s" , newPath.toPath (false ).c_str ());
340- }
347+ // Prevent others, especially file explorer, from opening files under the folder, generating
348+ // thumbnail while we're running. We have seen we're blocked to open files forever due to this.
349+ WINDOWS_ONLY (mFolderLocker = platform::FolderLocker{mCachePath .asPlatformEncoded (true )});
341350}
342351
343352TaskExecutor& FileCache::executor () const
0 commit comments