chiark / gitweb /
condition: add ConditionFileNotEmpty=
authorLennart Poettering <lennart@poettering.net>
Mon, 10 Sep 2012 23:29:46 +0000 (01:29 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 10 Sep 2012 23:29:46 +0000 (01:29 +0200)
https://bugs.freedesktop.org/show_bug.cgi?id=54448

man/systemd.unit.xml
src/core/condition.c
src/core/condition.h

index bf22ca9..00f6066 100644 (file)
                                 <term><varname>ConditionPathIsMountPoint=</varname></term>
                                 <term><varname>ConditionPathIsReadWrite=</varname></term>
                                 <term><varname>ConditionDirectoryNotEmpty=</varname></term>
+                                <term><varname>ConditionFileNotEmpty=</varname></term>
                                 <term><varname>ConditionFileIsExecutable=</varname></term>
                                 <term><varname>ConditionKernelCommandLine=</varname></term>
                                 <term><varname>ConditionVirtualization=</varname></term>
                                 (i.e. not mounted
                                 read-only).</para>
 
-                                <para><varname>ConditionFileIsExecutable=</varname>
+                                <para><varname>ConditionDirectoryNotEmpty=</varname>
                                 is similar to
                                 <varname>ConditionPathExists=</varname>
                                 but verifies whether a certain path
-                                exists, is a regular file and marked
-                                executable.</para>
+                                exists and is a non-empty
+                                directory.</para>
 
-                                <para><varname>ConditionDirectoryNotEmpty=</varname>
+                                <para><varname>ConditionFileNotEmpty=</varname>
                                 is similar to
                                 <varname>ConditionPathExists=</varname>
                                 but verifies whether a certain path
-                                exists and is a non-empty
-                                directory.</para>
+                                exists and refers to a regular file
+                                with a non-zero size.</para>
+
+                                <para><varname>ConditionFileIsExecutable=</varname>
+                                is similar to
+                                <varname>ConditionPathExists=</varname>
+                                but verifies whether a certain path
+                                exists, is a regular file and marked
+                                executable.</para>
 
-                                <para>Similarly,
+                                <para>Similar,
                                 <varname>ConditionKernelCommandLine=</varname>
                                 may be used to check whether a
                                 specific kernel command line option is
index e5cda21..32a37cc 100644 (file)
@@ -261,6 +261,15 @@ bool condition_test(Condition *c) {
                 return !(k == -ENOENT || k > 0) == !c->negate;
         }
 
+        case CONDITION_FILE_NOT_EMPTY: {
+                struct stat st;
+
+                if (stat(c->parameter, &st) < 0)
+                        return c->negate;
+
+                return (S_ISREG(st.st_mode) && st.st_size > 0) == !c->negate;
+        }
+
         case CONDITION_FILE_IS_EXECUTABLE: {
                 struct stat st;
 
@@ -350,6 +359,7 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
         [CONDITION_PATH_IS_MOUNT_POINT] = "ConditionPathIsMountPoint",
         [CONDITION_PATH_IS_READ_WRITE] = "ConditionPathIsReadWrite",
         [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
+        [CONDITION_FILE_NOT_EMPTY] = "ConditionFileNotEmpty",
         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
         [CONDITION_SECURITY] = "ConditionSecurity",
index 55b331e..03954e4 100644 (file)
@@ -33,6 +33,7 @@ typedef enum ConditionType {
         CONDITION_PATH_IS_MOUNT_POINT,
         CONDITION_PATH_IS_READ_WRITE,
         CONDITION_DIRECTORY_NOT_EMPTY,
+        CONDITION_FILE_NOT_EMPTY,
         CONDITION_FILE_IS_EXECUTABLE,
         CONDITION_KERNEL_COMMAND_LINE,
         CONDITION_VIRTUALIZATION,