-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathresource_utils.py
More file actions
71 lines (58 loc) · 2.03 KB
/
resource_utils.py
File metadata and controls
71 lines (58 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
System resource utilities for the Socket Security CLI.
"""
import logging
import sys
# The resource module is only available on Unix-like systems
resource_available = False
try:
import resource
resource_available = True
except ImportError:
# On Windows, the resource module is not available
pass
log = logging.getLogger("socketdev")
def get_file_descriptor_limit():
"""
Get the current file descriptor limit (equivalent to ulimit -n)
Returns:
tuple: (soft_limit, hard_limit) or (None, None) if error or on Windows
"""
if not resource_available:
# On Windows, resource module is not available
return None, None
try:
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
return soft_limit, hard_limit
except OSError as e:
log.error(f"Error getting file descriptor limit: {e}")
return None, None
def check_file_count_against_ulimit(file_count, buffer_size=100):
"""
Check if the number of files would exceed the file descriptor limit
Args:
file_count (int): Number of files to check
buffer_size (int): Safety buffer to leave for other file operations
Returns:
dict: Information about the check
"""
soft_limit, hard_limit = get_file_descriptor_limit()
if soft_limit is None:
return {
"can_check": False,
"error": "Could not determine file descriptor limit",
"safe_to_process": True # Assume safe if we can't check
}
available_fds = soft_limit - buffer_size
would_exceed = file_count > available_fds
return {
"can_check": True,
"file_count": file_count,
"soft_limit": soft_limit,
"hard_limit": hard_limit,
"available_fds": available_fds,
"would_exceed": would_exceed,
"safe_to_process": not would_exceed,
"buffer_size": buffer_size,
"recommendation": "Consider processing files in batches or increasing ulimit" if would_exceed else "Safe to process all files"
}