11"""
22System resource utilities for the Socket Security CLI.
33"""
4- import resource
54import logging
5+ import sys
6+
7+ # The resource module is only available on Unix-like systems
8+ resource_available = False
9+ try :
10+ import resource
11+ resource_available = True
12+ except ImportError :
13+ # On Windows, the resource module is not available
14+ pass
615
716log = logging .getLogger ("socketdev" )
817
918
1019def get_file_descriptor_limit ():
1120 """
1221 Get the current file descriptor limit (equivalent to ulimit -n)
13-
22+
1423 Returns:
15- tuple: (soft_limit, hard_limit) or (None, None) if error
24+ tuple: (soft_limit, hard_limit) or (None, None) if error or on Windows
1625 """
26+ if not resource_available :
27+ # On Windows, resource module is not available
28+ return None , None
29+
1730 try :
1831 soft_limit , hard_limit = resource .getrlimit (resource .RLIMIT_NOFILE )
1932 return soft_limit , hard_limit
@@ -25,26 +38,26 @@ def get_file_descriptor_limit():
2538def check_file_count_against_ulimit (file_count , buffer_size = 100 ):
2639 """
2740 Check if the number of files would exceed the file descriptor limit
28-
41+
2942 Args:
3043 file_count (int): Number of files to check
3144 buffer_size (int): Safety buffer to leave for other file operations
32-
45+
3346 Returns:
3447 dict: Information about the check
3548 """
3649 soft_limit , hard_limit = get_file_descriptor_limit ()
37-
50+
3851 if soft_limit is None :
3952 return {
4053 "can_check" : False ,
4154 "error" : "Could not determine file descriptor limit" ,
4255 "safe_to_process" : True # Assume safe if we can't check
4356 }
44-
57+
4558 available_fds = soft_limit - buffer_size
4659 would_exceed = file_count > available_fds
47-
60+
4861 return {
4962 "can_check" : True ,
5063 "file_count" : file_count ,
0 commit comments