Skip to content

Commit 853cc83

Browse files
committed
[refactor] Avoid if-else def by suffixing different FUSE version implementations
1 parent 7f046fe commit 853cc83

File tree

1 file changed

+49
-69
lines changed

1 file changed

+49
-69
lines changed

mfusepy.py

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,9 +1187,21 @@ class as is to Operations, instead of just the fh field.
11871187

11881188
# Wrap functions into try-except statements.
11891189
if is_function:
1190-
method = getattr(self, name, None)
1190+
if fuse_version_major == 2:
1191+
method = getattr(self, name + '_fuse_2', None)
1192+
elif fuse_version_major == 3:
1193+
method = getattr(self, name + '_fuse_3', None)
1194+
1195+
if method is not None and hasattr(self, name):
1196+
raise RuntimeError(
1197+
"Internal Error: Only either suffixed or non-suffixed methods must exist!"
1198+
f"Found both for '{name}'."
1199+
)
1200+
11911201
if method is None:
1192-
raise RuntimeError(f"Internal Error: Method wrapper for FUSE callback '{name}' is missing!")
1202+
method = getattr(self, name, None)
1203+
if method is None:
1204+
raise RuntimeError(f"Internal Error: Method wrapper for FUSE callback '{name}' is missing!")
11931205

11941206
log.debug(f"Set libFUSE callback for '{name}' to wrapped {method} wrapping {value}")
11951207
value = prototype(functools.partial(self._wrapper, method))
@@ -1272,15 +1284,11 @@ def _wrapper(self, func, *args, **kwargs):
12721284
fuse_exit()
12731285
return -errno.EFAULT
12741286

1275-
if fuse_version_major == 2:
1276-
1277-
def getattr(self, path: bytes, buf):
1278-
return self.fgetattr(path, buf, None)
1287+
def getattr_fuse_2(self, path: bytes, buf):
1288+
return self.fgetattr(path, buf, None)
12791289

1280-
elif fuse_version_major == 3:
1281-
1282-
def getattr(self, path: bytes, buf, fip): # type: ignore
1283-
return self.fgetattr(path, buf, None)
1290+
def getattr_fuse_3(self, path: bytes, buf, fip):
1291+
return self.fgetattr(path, buf, None)
12841292

12851293
def readlink(self, path: bytes, buf, bufsize: int) -> int:
12861294
ret = self.operations.readlink(path.decode(self.encoding)).encode(self.encoding)
@@ -1308,30 +1316,22 @@ def symlink(self, source: bytes, target: bytes) -> int:
13081316

13091317
return self.operations.symlink(target.decode(self.encoding), source.decode(self.encoding))
13101318

1311-
def _rename(self, old: bytes, new: bytes) -> int:
1319+
def rename_fuse_2(self, old: bytes, new: bytes) -> int:
13121320
return self.operations.rename(old.decode(self.encoding), new.decode(self.encoding))
13131321

1314-
if fuse_version_major == 2:
1315-
rename = _rename
1316-
elif fuse_version_major == 3:
1317-
1318-
def rename(self, old: bytes, new: bytes, flags: int) -> int: # type: ignore
1319-
return self._rename(old, new)
1322+
def rename_fuse_3(self, old: bytes, new: bytes, flags: int) -> int:
1323+
return self.rename_fuse_2(old, new)
13201324

13211325
def link(self, source: bytes, target: bytes):
13221326
'creates a hard link `target -> source` (e.g. ln source target)'
13231327

13241328
return self.operations.link(target.decode(self.encoding), source.decode(self.encoding))
13251329

1326-
if fuse_version_major == 2:
1330+
def chmod_fuse_2(self, path: Optional[bytes], mode: int) -> int:
1331+
return self.operations.chmod(None if path is None else path.decode(self.encoding), mode)
13271332

1328-
def chmod(self, path: Optional[bytes], mode: int) -> int:
1329-
return self.operations.chmod(None if path is None else path.decode(self.encoding), mode)
1330-
1331-
elif fuse_version_major == 3:
1332-
1333-
def chmod(self, path: Optional[bytes], mode: int, fip) -> int: # type: ignore
1334-
return self.operations.chmod(None if path is None else path.decode(self.encoding), mode)
1333+
def chmod_fuse_3(self, path: Optional[bytes], mode: int, fip) -> int:
1334+
return self.operations.chmod(None if path is None else path.decode(self.encoding), mode)
13351335

13361336
def _chown(self, path: Optional[bytes], uid: int, gid: int) -> int:
13371337
# Check if any of the arguments is a -1 that has overflowed
@@ -1342,25 +1342,17 @@ def _chown(self, path: Optional[bytes], uid: int, gid: int) -> int:
13421342

13431343
return self.operations.chown(None if path is None else path.decode(self.encoding), uid, gid)
13441344

1345-
if fuse_version_major == 2:
1346-
1347-
def chown(self, path: Optional[bytes], uid: int, gid: int) -> int:
1348-
return self._chown(path, uid, gid)
1349-
1350-
elif fuse_version_major == 3:
1345+
def chown_fuse_2(self, path: Optional[bytes], uid: int, gid: int) -> int:
1346+
return self._chown(path, uid, gid)
13511347

1352-
def chown(self, path: Optional[bytes], uid: int, gid: int, fip) -> int: # type: ignore
1353-
return self._chown(path, uid, gid)
1348+
def chown_fuse_3(self, path: Optional[bytes], uid: int, gid: int, fip) -> int:
1349+
return self._chown(path, uid, gid)
13541350

1355-
if fuse_version_major == 2:
1351+
def truncate_fuse_2(self, path: Optional[bytes], length: int) -> int:
1352+
return self.operations.truncate(None if path is None else path.decode(self.encoding), length)
13561353

1357-
def truncate(self, path: Optional[bytes], length: int) -> int:
1358-
return self.operations.truncate(None if path is None else path.decode(self.encoding), length)
1359-
1360-
elif fuse_version_major == 3:
1361-
1362-
def truncate(self, path: Optional[bytes], length: int, fip) -> int: # type: ignore
1363-
return self.operations.truncate(None if path is None else path.decode(self.encoding), length)
1354+
def truncate_fuse_3(self, path: Optional[bytes], length: int, fip) -> int:
1355+
return self.operations.truncate(None if path is None else path.decode(self.encoding), length)
13641356

13651357
def open(self, path: bytes, fip) -> int:
13661358
fi = fip.contents
@@ -1548,18 +1540,14 @@ def _readdir(self, path: Optional[bytes], buf, filler, offset: int, fip) -> int:
15481540

15491541
return 0
15501542

1551-
if fuse_version_major == 2:
1552-
1553-
def readdir(self, path: Optional[bytes], buf, filler, offset, fip) -> int:
1554-
return self._readdir(path, buf, filler, offset, fip)
1543+
def readdir_fuse_2(self, path: Optional[bytes], buf, filler, offset, fip) -> int:
1544+
return self._readdir(path, buf, filler, offset, fip)
15551545

1556-
elif fuse_version_major == 3:
1557-
1558-
def readdir(self, path: Optional[bytes], buf, filler, offset, fip, flags) -> int: # type: ignore
1559-
# TODO if bit 0 (FUSE_READDIR_PLUS) is set in flags, then we might want to gather more metadata
1560-
# and return it in "filler" with bit 1 (FUSE_FILL_DIR_PLUS) being set.
1561-
# Ignore raw_fi
1562-
return self._readdir(path, buf, filler, offset, fip)
1546+
def readdir_fuse_3(self, path: Optional[bytes], buf, filler, offset, fip, flags) -> int:
1547+
# TODO if bit 0 (FUSE_READDIR_PLUS) is set in flags, then we might want to gather more metadata
1548+
# and return it in "filler" with bit 1 (FUSE_FILL_DIR_PLUS) being set.
1549+
# Ignore raw_fi
1550+
return self._readdir(path, buf, filler, offset, fip)
15631551

15641552
def releasedir(self, path: Optional[bytes], fip) -> int:
15651553
# Ignore raw_fi
@@ -1577,17 +1565,13 @@ def _init(self, conn, config) -> None:
15771565
elif hasattr(self.operations, "init") and not getattr(self.operations.init, "libfuse_ignore", False):
15781566
self.operations.init("/")
15791567

1580-
if fuse_version_major == 2:
1568+
def init_fuse_2(self, conn) -> None:
1569+
self._init(conn, fuse_config())
15811570

1582-
def init(self, conn) -> None:
1583-
self._init(conn, fuse_config())
1584-
1585-
else:
1586-
1587-
def init(self, conn, config) -> None: # type: ignore
1588-
if getattr(self.operations, 'flag_nopath', False) and getattr(self.operations, 'flag_nullpath_ok', False):
1589-
config.contents.nullpath_ok = True
1590-
self._init(conn, config)
1571+
def init_fuse_3(self, conn, config) -> None:
1572+
if getattr(self.operations, 'flag_nopath', False) and getattr(self.operations, 'flag_nullpath_ok', False):
1573+
config.contents.nullpath_ok = True
1574+
self._init(conn, config)
15911575

15921576
def destroy(self, private_data) -> None:
15931577
return self.operations.destroy('/')
@@ -1625,7 +1609,7 @@ def lock(self, path: Optional[bytes], fip, cmd: int, lock) -> int:
16251609
fh = fip.contents if self.raw_fi else fip.contents.fh
16261610
return self.operations.lock(None if path is None else path.decode(self.encoding), fh, cmd, lock)
16271611

1628-
def _utimens(self, path: Optional[bytes], buf) -> int:
1612+
def utimens_fuse_2(self, path: Optional[bytes], buf) -> int:
16291613
if buf:
16301614
atime = time_of_timespec(buf.contents.actime, use_ns=self.use_ns)
16311615
mtime = time_of_timespec(buf.contents.modtime, use_ns=self.use_ns)
@@ -1635,12 +1619,8 @@ def _utimens(self, path: Optional[bytes], buf) -> int:
16351619

16361620
return self.operations.utimens(None if path is None else path.decode(self.encoding), times)
16371621

1638-
if fuse_version_major == 2:
1639-
utimens = _utimens
1640-
elif fuse_version_major == 3:
1641-
1642-
def utimens(self, path: Optional[bytes], buf, fip) -> int: # type: ignore
1643-
return self._utimens(path, buf)
1622+
def utimens_fuse_3(self, path: Optional[bytes], buf, fip) -> int:
1623+
return self.utimens_fuse_2(path, buf)
16441624

16451625
def bmap(self, path: bytes, blocksize: int, idx) -> int:
16461626
return self.operations.bmap(path.decode(self.encoding), blocksize, idx)

0 commit comments

Comments
 (0)