Implemented PTS attributes Request File Metadata, Unix-Style File Metadata
[strongswan.git] / src / libpts / pts / pts_file_meta.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pts_file_meta.h"
17
18 #include <utils/linked_list.h>
19 #include <debug.h>
20
21 typedef struct private_pts_file_meta_t private_pts_file_meta_t;
22
23 /**
24 * Private data of a pts_file_meta_t object.
25 *
26 */
27 struct private_pts_file_meta_t {
28
29 /**
30 * Public pts_file_meta_t interface.
31 */
32 pts_file_meta_t public;
33
34 /**
35 * List of File Metadata
36 */
37 linked_list_t *list;
38 };
39
40 /**
41 * Free an pts_file_metadata_t object
42 */
43 static void free_entry(pts_file_metadata_t *entry)
44 {
45 if (entry)
46 {
47 free(entry->filename);
48 free(entry);
49 }
50 }
51
52 METHOD(pts_file_meta_t, get_file_count, int,
53 private_pts_file_meta_t *this)
54 {
55 return this->list->get_count(this->list);
56 }
57
58 METHOD(pts_file_meta_t, add, void,
59 private_pts_file_meta_t *this, char *filename, pts_file_type_t type,
60 u_int64_t filesize, time_t create_time, time_t last_modify_time, time_t last_access_time,
61 u_int64_t owner_id, u_int64_t group_id)
62 {
63 pts_file_metadata_t *entry;
64
65 entry = malloc_thing(pts_file_metadata_t);
66
67 entry->filename = strdup(filename);
68 entry->meta_length = PTS_FILE_METADATA_SIZE + strlen(entry->filename);
69 entry->type = type;
70 entry->filesize = filesize;
71 entry->create_time = create_time;
72 entry->last_modify_time = last_modify_time;
73 entry->last_access_time = last_access_time;
74 entry->owner_id = owner_id;
75 entry->group_id = group_id;
76
77 this->list->insert_last(this->list, entry);
78 }
79
80 /**
81 * Enumerate file metadata entries
82 */
83 static bool entry_filter(void *null, pts_file_metadata_t **entry,
84 char **filename, void *i2, u_int16_t *meta_length, void *i3,
85 pts_file_type_t *type, void *i4, u_int64_t *filesize, void *i5,
86 time_t *create_time, void *i6, time_t *last_modify_time, void *i7,
87 time_t *last_access_time, void *i8, u_int64_t *owner_id, void *i9,
88 u_int64_t *group_id)
89 {
90 *filename = (*entry)->filename;
91 *meta_length = (*entry)->meta_length;
92 *type = (*entry)->type;
93 *filesize = (*entry)->filesize;
94 *create_time = (*entry)->create_time;
95 *last_modify_time = (*entry)->last_modify_time;
96 *last_access_time = (*entry)->last_access_time;
97 *owner_id = (*entry)->owner_id;
98 *group_id = (*entry)->group_id;
99 return TRUE;
100 }
101
102 METHOD(pts_file_meta_t, create_enumerator, enumerator_t*,
103 private_pts_file_meta_t *this)
104 {
105 return enumerator_create_filter(this->list->create_enumerator(this->list),
106 (void*)entry_filter, NULL, NULL);
107 }
108
109 METHOD(pts_file_meta_t, destroy, void,
110 private_pts_file_meta_t *this)
111 {
112 this->list->destroy_function(this->list, (void *)free_entry);
113 free(this);
114 }
115
116 /**
117 * See header
118 */
119 pts_file_meta_t *pts_file_meta_create()
120 {
121 private_pts_file_meta_t *this;
122
123 INIT(this,
124 .public = {
125 .get_file_count = _get_file_count,
126 .add = _add,
127 .create_enumerator = _create_enumerator,
128 .destroy = _destroy,
129 },
130 .list = linked_list_create(),
131 );
132
133 return &this->public;
134 }
135