-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmkfifo.py
More file actions
91 lines (77 loc) · 2.24 KB
/
mkfifo.py
File metadata and controls
91 lines (77 loc) · 2.24 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 24-11-2024
Last update: 24-11-2024
Version: 1.0
Description: The mkfifo command from GNU coreutils in Python3.
Example of use: python3 mkfifo.py mypipe
'''
import os
import sys
import stat
import argparse
import errno
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description='Create named pipes (FIFOs)',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''
Examples:
mkfifo pipe Create a FIFO named 'pipe'
mkfifo -m 644 pipe Create a FIFO with specific permissions
'''
)
parser.add_argument(
'names',
nargs='+',
help='Names of FIFOs to create'
)
parser.add_argument(
'-m', '--mode',
type=lambda x: int(x, 8), # Convert octal string to integer
default=0o666,
help='Set file permission bits (as in chmod), default is 666 in octal'
)
parser.add_argument(
'--version',
action='version',
version='%(prog)s 1.0'
)
return parser.parse_args()
def create_fifo(name, mode):
"""
Create a FIFO with the given name and mode.
Args:
name (str): Name of the FIFO to create
mode (int): Permission bits for the FIFO
Returns:
bool: True if successful, False if an error occurred
"""
try:
os.mkfifo(name, mode)
return True
except OSError as e:
if e.errno == errno.EEXIST:
print(f"mkfifo: cannot create fifo '{name}': File exists",
file=sys.stderr)
elif e.errno == errno.EACCES:
print(f"mkfifo: cannot create fifo '{name}': Permission denied",
file=sys.stderr)
else:
print(f"mkfifo: cannot create fifo '{name}': {e.strerror}",
file=sys.stderr)
return False
def main():
"""Main program entry point."""
args = parse_args()
# Create each requested FIFO
exit_code = 0
for name in args.names:
if not create_fifo(name, args.mode):
exit_code = 1
sys.exit(exit_code)
if __name__ == '__main__':
main()