2
2
delagent: Remove an upload from the DB and repository
3
3
4
4
Copyright (C) 2007-2013 Hewlett-Packard Development Company, L.P.
5
+ Copyright (C) 2015-2016 Siemens AG
5
6
6
7
This program is free software; you can redistribute it and/or
7
8
modify it under the terms of the GNU General Public License
@@ -31,30 +32,74 @@ char BuildVersion[]="delagent build version: NULL.\n";
31
32
#endif
32
33
33
34
35
+ /***********************************************
36
+ usage():
37
+ Command line options allow you to write the agent so it works
38
+ stand alone, in addition to working with the scheduler.
39
+ This simplifies code development and testing.
40
+ So if you have options, have a usage().
41
+ Here are some suggested options (in addition to the program
42
+ specific options you may already have).
43
+ ***********************************************/
44
+ void usage (char * Name )
45
+ {
46
+ fprintf (stderr ,"Usage: %s [options]\n" ,Name );
47
+ fprintf (stderr ," List or delete uploads.\n" );
48
+ fprintf (stderr ," Options\n" );
49
+ fprintf (stderr ," -i :: Initialize the DB, then exit.\n" );
50
+ fprintf (stderr ," -u :: List uploads IDs.\n" );
51
+ fprintf (stderr ," -U # :: Delete upload ID.\n" );
52
+ //fprintf(stderr," -L # :: Delete ALL licenses associated with upload ID.\n");
53
+ fprintf (stderr ," -f :: List folder IDs.\n" );
54
+ fprintf (stderr ," -F # :: Delete folder ID and all uploads under this folder.\n" );
55
+ fprintf (stderr ," Folder '1' is the default folder. '-F 1' will delete\n" );
56
+ fprintf (stderr ," every upload and folder in the navigation tree.\n" );
57
+ fprintf (stderr ," -s :: Run from the scheduler.\n" );
58
+ fprintf (stderr ," -T :: TEST -- do not update the DB or delete any files (just pretend)\n" );
59
+ fprintf (stderr ," -v :: Verbose (-vv for more verbose)\n" );
60
+ fprintf (stderr ," -c # :: Specify the directory for the system configuration\n" );
61
+ fprintf (stderr ," -V :: print the version info, then exit.\n" );
62
+ fprintf (stderr ," --user|-n # :: user name\n" );
63
+ fprintf (stderr ," --password|-p # :: password\n" );
64
+ } /* usage() */
65
+
66
+ void writeMessageAfterDelete (char * kind , long id , char * user_name , int returnedCode )
67
+ {
68
+ if (0 == returnedCode )
69
+ {
70
+ fprintf (stdout , "The %s '%ld' is deleted by the user '%s'.\n" , kind , id , user_name );
71
+ }
72
+ else
73
+ {
74
+ fprintf (stdout , "Deletion failed: user '%s' does not have the permsssion to delete the %s '%ld', or the %s '%ld' does not exist.\n" , user_name , kind , id , kind , id );
75
+ exit (returnedCode );
76
+ }
77
+ }
78
+
34
79
/**
35
80
* \brief main function for the delagent
36
81
*
37
82
* There are 2 ways to use the delagent agent:
38
- * 1. Command Line :: delete/list upload from the command line
83
+ * 1. Command Line :: delete/list upload/folder/license from the command line
39
84
* 2. Agent Based :: run from the scheduler
40
85
*
41
86
* +-----------------------+
42
87
* | Command Line Analysis |
43
88
* +-----------------------+
44
89
*
45
90
* List or delete uploads.
46
- * -h :: help (print this message), then exit.
47
- * -i :: Initialize the DB
48
- * -u :: List uploads IDs.
49
- * -U # :: Delete upload ID.
50
- * -L # :: Delete ALL licenses associated with upload ID.
51
- * -f :: List folder IDs.
52
- * -F # :: Delete folder ID and all uploads under this folder.
53
- * -T :: TEST -- do not update the DB or delete any files (just pretend).
54
- * -v :: Verbose (-vv for more verbose).
55
- * -V :: print the version info, then exit.
91
+ * -h :: help (print this message), then exit.
92
+ * -i :: Initialize the DB
93
+ * -u :: List uploads IDs.
94
+ * -U # :: Delete upload ID.
95
+ * -L # :: Delete ALL licenses associated with upload ID.
96
+ * -f :: List folder IDs.
97
+ * -F # :: Delete folder ID and all uploads under this folder.
98
+ * -T :: TEST -- do not update the DB or delete any files (just pretend).
99
+ * -v :: Verbose (-vv for more verbose).
100
+ * -V :: print the version info, then exit.
56
101
* -c SYSCONFDIR :: Specify the directory for the system configuration.
57
- * --user # :: user name
102
+ * --user # :: user name
58
103
* --password # :: password
59
104
*
60
105
* +----------------------+
@@ -72,13 +117,11 @@ char BuildVersion[]="delagent build version: NULL.\n";
72
117
int main (int argc , char * argv [])
73
118
{
74
119
int c ;
75
- int ListProj = 0 , ListFolder = 0 ;
76
- long DelUpload = 0 , DelFolder = 0 , DelLicense = 0 ;
77
- int Scheduler = 0 ; /* should it run from the scheduler? */
78
- int GotArg = 0 ;
120
+ int listProj = 0 , listFolder = 0 ;
121
+ long delUpload = 0 , delFolder = 0 , delLicense = 0 ;
122
+ int scheduler = 0 ; /* should it run from the scheduler? */
123
+ int gotArg = 0 ;
79
124
char * agent_desc = "Deletes upload. Other list/delete options available from the command line." ;
80
- char * Parm = NULL ;
81
- //int Agent_pk = 0;
82
125
char * COMMIT_HASH ;
83
126
char * VERSION ;
84
127
char agent_rev [myBUFSIZ ];
@@ -87,6 +130,7 @@ int main (int argc, char *argv[])
87
130
char * password = NULL ;
88
131
int user_id = -1 ;
89
132
int user_perm = -1 ;
133
+ int returnedCode = 0 ;
90
134
91
135
fo_scheduler_connect (& argc , argv , & db_conn );
92
136
@@ -96,84 +140,128 @@ int main (int argc, char *argv[])
96
140
{"password" , required_argument , 0 , 'p' },
97
141
{0 , 0 , 0 , 0 }
98
142
};
99
-
143
+
100
144
while ((c = getopt_long (argc , argv , "n:p:ifF:lL:sTuU:vVc:h" ,
101
145
long_options , & option_index )) != -1 )
102
146
{
103
147
switch (c )
104
148
{
105
- case 'n' :
149
+ case 'n' :
106
150
user_name = optarg ;
107
- break ;
151
+ break ;
108
152
case 'p' :
109
153
password = optarg ;
110
154
break ;
111
155
case 'i' :
112
156
PQfinish (db_conn );
113
157
return (0 );
114
- case 'f' : ListFolder = 1 ; GotArg = 1 ; break ;
115
- case 'F' : DelFolder = atol (optarg ); GotArg = 1 ; break ;
116
- case 'L' : DelLicense = atol (optarg ); GotArg = 1 ; break ;
117
- case 's' : Scheduler = 1 ; GotArg = 1 ; break ;
118
- case 'T' : Test ++ ; break ;
119
- case 'u' : ListProj = 1 ; GotArg = 1 ; break ;
120
- case 'U' : DelUpload = atol (optarg ); GotArg = 1 ; break ;
121
- case 'v' : Verbose ++ ; break ;
122
- case 'c' : GotArg = 1 ; break ; /* handled by fo_scheduler_connect() */
123
- case 'V' : printf ("%s" , BuildVersion ); PQfinish (db_conn ); return (0 );
124
- default : Usage (argv [0 ]); exit (-1 );
158
+ case 'f' :
159
+ listFolder = 1 ;
160
+ gotArg = 1 ;
161
+ break ;
162
+ case 'F' :
163
+ delFolder = atol (optarg );
164
+ gotArg = 1 ;
165
+ break ;
166
+ case 'L' :
167
+ delLicense = atol (optarg );
168
+ gotArg = 1 ;
169
+ break ;
170
+ case 's' :
171
+ scheduler = 1 ;
172
+ gotArg = 1 ;
173
+ break ;
174
+ case 'T' :
175
+ Test ++ ;
176
+ break ;
177
+ case 'u' :
178
+ listProj = 1 ;
179
+ gotArg = 1 ;
180
+ break ;
181
+ case 'U' :
182
+ delUpload = atol (optarg );
183
+ gotArg = 1 ;
184
+ break ;
185
+ case 'v' :
186
+ Verbose ++ ;
187
+ break ;
188
+ case 'c' :
189
+ gotArg = 1 ;
190
+ break ; /* handled by fo_scheduler_connect() */
191
+ case 'V' :
192
+ printf ("%s" , BuildVersion );
193
+ PQfinish (db_conn );
194
+ return (0 );
195
+ default :
196
+ usage (argv [0 ]);
197
+ exit (-1 );
125
198
}
126
199
}
127
200
128
- if (!GotArg )
129
- {
130
- Usage (argv [0 ]);
131
- exit (-1 );
132
- }
133
-
134
- if (Scheduler != 1 && 1 != authentication (user_name , password , & user_id , & user_perm ))
201
+ if (!gotArg )
135
202
{
136
- LOG_FATAL ( "User name or password is invalid.\n" );
203
+ usage ( argv [ 0 ] );
137
204
exit (-1 );
138
205
}
139
206
140
- COMMIT_HASH = fo_sysconfig ("delagent" , "COMMIT_HASH" );
141
- VERSION = fo_sysconfig ("delagent" , "VERSION" );
142
- sprintf (agent_rev , "%s.%s" , VERSION , COMMIT_HASH );
143
- /* Get the Agent Key from the DB */
144
- fo_GetAgentKey (db_conn , basename (argv [0 ]), 0 , agent_rev , agent_desc );
145
-
146
- if (ListProj ) ListUploads (user_id , user_perm );
147
- if (ListFolder ) ListFolders (user_id );
148
-
149
- alarm (60 ); /* from this point on, handle the alarm */
150
- if (DelUpload )
207
+ if (scheduler != 1 )
151
208
{
152
- if (1 != check_permission_del ( DelUpload , user_id , user_perm ))
209
+ if (0 != authentication ( user_name , password , & user_id , & user_perm ))
153
210
{
154
- LOG_FATAL ("You '%s' does not have the permsssion to delete the upload '%ld', or the upload '%ld' does not exist .\n" , user_name , DelUpload , DelUpload );
211
+ LOG_FATAL ("User name or password is invalid .\n" );
155
212
exit (-1 );
156
213
}
157
- DeleteUpload (DelUpload );
158
- fprintf (stdout , "The upload '%ld' is deleted by the user '%s'.\n" , DelUpload , user_name );
159
- }
160
- if (DelFolder ) { DeleteFolder (DelFolder ); }
161
- if (DelLicense ) { DeleteLicense (DelLicense ); }
162
214
163
- /* process from the scheduler */
164
- if (Scheduler )
165
- {
166
- while (fo_scheduler_next ())
215
+ COMMIT_HASH = fo_sysconfig ("delagent" , "COMMIT_HASH" );
216
+ VERSION = fo_sysconfig ("delagent" , "VERSION" );
217
+ sprintf (agent_rev , "%s.%s" , VERSION , COMMIT_HASH );
218
+ /* Get the Agent Key from the DB */
219
+ fo_GetAgentKey (db_conn , basename (argv [0 ]), 0 , agent_rev , agent_desc );
220
+
221
+ if (listProj )
167
222
{
168
- Parm = fo_scheduler_current ();
169
-
170
- if (ReadParameter (Parm ) < 0 )
171
- exit (-1 );
223
+ returnedCode = listUploads (user_id , user_perm );
172
224
}
225
+ if (returnedCode < 0 )
226
+ {
227
+ return returnedCode ;
228
+ }
229
+ if (listFolder )
230
+ {
231
+ returnedCode = listFolders (user_id , user_perm );
232
+ }
233
+ if (returnedCode < 0 )
234
+ {
235
+ return returnedCode ;
236
+ }
237
+
238
+ alarm (60 ); /* from this point on, handle the alarm */
239
+ if (delUpload )
240
+ {
241
+ returnedCode = deleteUpload (delUpload , user_id , user_perm );
242
+
243
+ writeMessageAfterDelete ("upload" , delUpload , user_name , returnedCode );
244
+ }
245
+ if (delFolder )
246
+ {
247
+ returnedCode = deleteFolder (delFolder , user_id , user_perm );
248
+
249
+ writeMessageAfterDelete ("folder" , delFolder , user_name , returnedCode );
250
+ }
251
+ if (delLicense )
252
+ {
253
+ returnedCode = deleteLicense (delLicense , user_perm );
254
+
255
+ writeMessageAfterDelete ("license" , delLicense , user_name , returnedCode );
256
+ }
257
+ }
258
+ else
259
+ {
260
+ /* process from the scheduler */
261
+ doSchedulerTasks ();
173
262
}
263
+ fo_scheduler_disconnect (0 );
174
264
175
265
PQfinish (db_conn );
176
- fo_scheduler_disconnect (0 );
177
- return (0 );
266
+ return (returnedCode );
178
267
} /* main() */
179
-
0 commit comments