NAME
fcntl - manipulate file descriptor
SYNOPSIS
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );
DESCRIPTION
fcntl() performs one of the operations described below on the open file descriptor fd. The operation is determined by cmd.
fcntl() can take an optional third argument. Whether or not this argument is required is determined by cmd. The required argument type
is indicated in parentheses after each cmd name (in most cases, the required type is int, and we identify the argument using the name
arg), or void is specified if the argument is not required.
Advisory record locking
Linux implements traditional ("process-associated") UNIX record locks, as standardized by POSIX. For a Linux-specific alternative with
better semantics, see the discussion of open file description locks below.
F_SETLK, F_SETLKW, and F_GETLK are used to acquire, release, and test for the existence of record locks (also known as byte-range, file-
segment, or file-region locks). The third argument, lock, is a pointer to a structure that has at least the following fields (in
unspecified order).
struct flock {
...
short l_type; /* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock
(set by F_GETLK and F_OFD_GETLK) */
...
};
import fcntl
class Lock(object):
def __init__(self, file_name):
self.file_name = file_name
self.handle = open(file_name, 'w')
def lock(self):
fcntl.flock(self.handle, fcntl.LOCK_EX)
def unlock(self):
fcntl.flock(self.handle, fcntl.LOCK_UN)
def __del__(self):
try:
self.handle.close()
except:
pass
if __name__ == "__main__":
parser = OptionParser()
group = OptionGroup(parser, "FTP download tool", "Download build from ftp server")
group.add_option("--server", type="string", help="FTP server's IP address")
group.add_option("--username", type="string", help="User name")
group.add_option("--password", type="string", help="User's password")
group.add_option("--buildpath", type="string", help="Build path in the ftp server")
group.add_option("--buildname", type="string", help="Build name to be downloaded")
parser.add_option_group(group)
(options, args) = parser.parse_args()
local_dir = "/exports/images"
lock_file = "/var/tmp/flock.txt"
flock = Lock(lock_file)
flock.lock()
if os.path.isfile(os.path.join(local_dir, options.buildname)):
log.info("build exists, nothing needs to be done")
log.info("Download completed")
flock.unlock()
exit(0)
log.info("start to download build " + options.buildname)
t = paramiko.Transport((options.server, 22))
t.connect(username=options.username, password=options.password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(os.path.join(options.buildpath, options.buildname),
os.path.join(local_dir, options.buildname))
sftp.close()
t.close()
log.info("Download completed")
flock.unlock()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有