Skip to content

Commit ca952a1

Browse files
committed
Add fdscan for unix domain socket; scan by file-descriptor rather than passing filename
1 parent 5c5e33b commit ca952a1

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/clamd/__init__.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
3-
from __future__ import unicode_literals
4-
51
try:
6-
__version__ = __import__('pkg_resources').get_distribution('clamd').version
7-
except:
8-
__version__ = ''
9-
10-
# $Source$
2+
import importlib
113

4+
__version__ = importlib.metadata.distribution("clamd").version
5+
except ImportError:
6+
__version__ = ""
127

13-
import socket
14-
import sys
15-
import struct
8+
import base64
169
import contextlib
1710
import re
18-
import base64
11+
import socket
12+
import struct
13+
import sys
14+
from multiprocessing.reduction import sendfds
1915

20-
scan_response = re.compile(r"^(?P<path>.*): ((?P<virus>.+) )?(?P<status>(FOUND|OK|ERROR))$")
16+
scan_response = re.compile(
17+
r"^(?P<path>.*): ((?P<virus>.+) )?(?P<status>(FOUND|OK|ERROR))$"
18+
)
2119
EICAR = base64.b64decode(
22-
b'WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5E'
23-
b'QVJELUFOVElWSVJVUy1URVNU\nLUZJTEUhJEgrSCo=\n'
20+
b"WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5E"
21+
b"QVJELUFOVElWSVJVUy1URVNU\nLUZJTEUhJEgrSCo=\n"
2422
)
2523

2624

@@ -313,3 +311,16 @@ def _error_message(self, exception):
313311
path=self.unix_socket,
314312
msg=exception.args[1]
315313
)
314+
315+
def fdscan(self, file):
316+
"""Scan a file referenced by a file descriptor."""
317+
try:
318+
self._init_socket()
319+
with open(file, mode="rb") as fp:
320+
self._send_command("FILDES")
321+
sendfds(self.clamd_socket, [fp.fileno()])
322+
result = self._recv_response()
323+
_, reason, status = self._parse_response(result)
324+
return {file: (status, reason)}
325+
finally:
326+
self._close_socket()

0 commit comments

Comments
 (0)