经过大量的试验和错误,这里似乎是目前效果最好的:
在(后)安装期间生成 sqlite db 文件
我在 postinst 文件中这样做了,用作 bash 脚本并使用常量来创建表和主/外键分配语句,如下所示
#!/bin/bash
set -e
CREATE_TBL_PROBLEMS="CREATE TABLE tblProblems(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL UNIQUE
)"
CREATE_TBL_MAIN="CREATE TABLE tblMain (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
DBVersion TEXT NOT NULL,
DBrDate TEXT NOT NULL)"
CREATE_TBL_EVENTS="CREATE TABLE tblEvents (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
User TEXT NOT NULL,
NoOfInfections INTEGER NOT NULL,
NoOfHealings INTEGER NOT NULL,
DateTime TEXT NOT NULL,
VirusDBID INTEGER NOT NULL,
FOREIGN KEY(tblMainID) REFERENCES tblMain(ID)
)"
CREATE_TBL_ISSUES="CREATE TABLE tblIssues (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
FilePath TEXT NOT NULL,
Inode INTEGER NOT NULL UNIQUE,
ScanEventID INTEGER NOT NULL,
MalwareID INTEGER NOT NULL,
FOREIGN KEY(EventID) REFERENCES tblEvents(ID),
FOREIGN KEY(ProblemID) REFERENCES tblProblems(ID)
)"
主要挑战如下:应用程序应该按照惯例进入 root 拥有的 /usr/share/myapplication 中,并且 db 文件(sqlite)应该是用户拥有的文件。所以后者放在/home/user1/.myapplication中如下:
USER_HOME=$(eval echo ~${SUDO_USER})
echo "USER HOME IS " ${USER_HOME}
case "$1" in
configure)
mkdir -p "${USER_HOME}"/.myapplication
echo "Creating tables needed for myapplication"
sqlite3 "${USER_HOME}"/.myapplication/mydbfile.sqlite "${CREATE_TBL_MAIN}; ${CREATE_TBL_EVENTS}; ${CREATE_TABLE_ISSUES}; ${CREATE_TBL_PROBLEMS}"
echo "Assigning myapplication.sqlite file to user " "${SUDO_USER}"
chown -R "${SUDO_USER}":"${SUDO_USER}" "${USER_HOME}"/.myapplication/
;;
abort-upgrade|abort-remove|abort-deconfigure)
exit 0
;;
*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac
myapplication 的安装是通过连续调用 install -m 在规则文件中进行的
关于依赖关系,Tshepang 是对的,它们应该在控制文件中声明。但是,我还没有弄清楚,在某些情况下(机器),如果它们丢失,它们会自动安装,而在其他情况下,一旦依赖包丢失,安装就会中止。