]> git.pld-linux.org Git - projects/pld-ftp-admin.git/commitdiff
- ftp io daemon; does locking/unlocking; just need logging to have the
authorMariusz Mazur <mmazur@pld-linux.org>
Sun, 9 Jan 2005 23:12:39 +0000 (23:12 +0000)
committerMariusz Mazur <mmazur@pld-linux.org>
Sun, 9 Jan 2005 23:12:39 +0000 (23:12 +0000)
  functionality I need for local scripts; www interface is a different matter

Changed files:
    ftpiod/ftpiod.py -> 1.1
    modules/cmds.py -> 1.1
    modules/common.py -> 1.5
    modules/cons.py -> 1.1
    modules/ftpio.py -> 1.1

ftpiod/ftpiod.py [new file with mode: 0755]
modules/cmds.py [new file with mode: 0644]
modules/common.py
modules/cons.py [new file with mode: 0644]
modules/ftpio.py [new file with mode: 0644]

diff --git a/ftpiod/ftpiod.py b/ftpiod/ftpiod.py
new file mode 100755 (executable)
index 0000000..08e178b
--- /dev/null
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
+
+import sys, os
+sys.path.insert(0, os.environ['HOME']+'/pld-ftp-admin/modules')
+import cons
+
+def daemonize():
+    sys.stdin.close()
+    sys.stdout.close()
+    sys.stderr.close()
+    pid=os.fork()
+    if pid!=0:
+        sys.exit(0)
+
+#daemonize()
+
+
+while True:
+    for readable in cons.readables():
+        if readable==cons.privlistener:
+            newsock,addr=readable.accept()
+            cons.add(cons.Connection(newsock, False))
+        elif readable==cons.publistener:
+            newsock,addr=readable.accept()
+            cons.add(cons.Connection(newsock, True))
+        else:
+            readable.handleinput()
+
diff --git a/modules/cmds.py b/modules/cmds.py
new file mode 100644 (file)
index 0000000..4025ae8
--- /dev/null
@@ -0,0 +1,42 @@
+# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
+
+def parse(con):
+    if '\0' not in con.data:
+        return
+    cmds=con.data.split('\0')[:-1]
+
+    for cmd in cmds:
+        con.data=con.data[len(cmd)+1:]
+        cmdname=cmd[:4]
+        if cmdname in cmdlist:
+            cmdlist[cmdname](con, cmd[5:])
+        else:
+            raise CmdError
+            # TODO: log this
+
+locks={}
+
+def lock(con, arg, hard):
+    if arg not in locks:
+        locks[arg]=hard
+        con.sock.send("OK")
+    elif locks[arg]:
+        con.sock.send("HARD") # Hard lock - you can go get a cup of tea
+    else:
+        con.sock.send("SOFT") # Soft lock - try in a second or two
+        
+    
+def unlock(con, arg):
+    if arg in locks:
+        del locks[arg]
+
+def lock_soft(con, arg):
+    lock(con, arg, False)
+
+def lock_hard(con, arg):
+    lock(con, arg, True)
+
+CmdError="CmdError"
+
+cmdlist={'lcks':lock_soft, 'lckh':lock_hard, 'ulck':unlock}
+
index a86682fedb6acbe100c99f11d8e02c343c7fdfc1..938250b6c15303a0d8bf8d9f68cc4b467b9b3f2b 100644 (file)
@@ -7,13 +7,7 @@ def fileexists(path):
         fullpath=path
     else:
         fullpath=config.ftp_dir+path
-
-    try:
-        os.stat(fullpath)
-    except OSError, (errno, errmsg):
-        return False
-    else:
-        return True
+    return os.path.exists(fullpath)
 
 def checkdir(dir):
     if not fileexists(sys.argv[1]):
diff --git a/modules/cons.py b/modules/cons.py
new file mode 100644 (file)
index 0000000..2e81fb7
--- /dev/null
@@ -0,0 +1,63 @@
+# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
+
+import socket
+import os
+import select
+from common import fileexists
+import ftpio
+import cmds
+
+class Connection:
+    def __init__(self, sock, authorized):
+        sock.setblocking(False)
+        self.sock=sock
+        self.authorized=authorized
+        self.fileno=sock.fileno
+        self.data=""
+    def destroy(self):
+        self.sock.close()
+        rm(self)
+    def handleinput(self):
+        newdata=self.sock.recv(8192)
+        if not newdata:
+            self.destroy()
+        self.data=self.data+newdata
+        try:
+            cmds.parse(self)
+        except cmds.CmdError:
+            self.destroy()
+
+def add(con):
+    cons.append(con)
+   
+def rm(con):
+    cons.remove(con)
+
+def readables():
+    lst=cons[:]
+    lst.append(privlistener)
+    lst.append(publistener)
+    inlst,outlst,errlst = select.select(lst, [], [], 0)
+    return inlst
+
+def createlistener(path):
+    if fileexists(path):
+        os.remove(path)
+
+    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+    s.setblocking(False)
+    s.bind(path)
+    if path==ftpio.pubsock:
+        os.chmod(path, 0606)
+    else:
+        os.chmod(path, 0600)
+    s.listen(3)
+    return s
+
+cons=[]
+
+privlistener=createlistener(ftpio.privsock)
+publistener=createlistener(ftpio.pubsock)
+
+
+
diff --git a/modules/ftpio.py b/modules/ftpio.py
new file mode 100644 (file)
index 0000000..2659d6e
--- /dev/null
@@ -0,0 +1,41 @@
+# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
+
+import os
+import socket
+import time
+import config
+
+privsock=os.environ['HOME']+'/pld-ftp-admin/var/privsock'
+pubsock=config.value['pubsock']
+
+if os.environ.has_key('HOME'):
+    socketname=privsock
+else:
+    socketname=pubsock
+
+def connect():
+    global sock
+    sock=socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+    sock.connect(socketname)
+
+def lock(path, hard=False):
+    def dolock():
+        if hard:
+            sock.send('lckh %s\0' % path)
+        else:
+            sock.send('lcks %s\0' % path)
+        return sock.recv(20)
+    for i in range(3):
+        retcode=dolock()
+        if retcode=="OK":
+            return True
+        elif retcode=="HARD":
+            return False
+        if i!=2:
+            time.sleep(1)
+    return False
+    
+def unlock(path):
+    sock.send('ulck %s\0' % path)
+
+
This page took 0.051685 seconds and 4 git commands to generate.