|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2019 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "finfo.h" |
|
30 |
|
31 #include <stdio.h> |
|
32 #include <stdlib.h> |
|
33 #include <unistd.h> |
|
34 #include <sys/stat.h> |
|
35 |
|
36 #include <ucx/string.h> |
|
37 |
|
38 uint32_t parse_finfo(const char *str) { |
|
39 scstr_t s = scstr(str); |
|
40 |
|
41 if(!sstrcmp(s, SC("*")) || !sstrcmp(s, SC("a")) || !sstrcmp(s, SC("all"))) { |
|
42 return FINFO_DATE|FINFO_OWNER|FINFO_MODE; |
|
43 } |
|
44 |
|
45 size_t count = 0; |
|
46 sstr_t *fs = sstrsplit(s, SC(","), &count); |
|
47 |
|
48 uint32_t finfo = 0; |
|
49 for(int i=0;i<count;i++) { |
|
50 sstr_t f = fs[i]; |
|
51 if(!sstrcasecmp(f, SC("date"))) { |
|
52 finfo |= FINFO_DATE; |
|
53 } else if(!sstrcasecmp(f, SC("owner"))) { |
|
54 finfo |= FINFO_OWNER; |
|
55 } else if(!sstrcasecmp(f, SC("mode"))) { |
|
56 finfo |= FINFO_MODE; |
|
57 } |
|
58 free(f.ptr); |
|
59 } |
|
60 |
|
61 free(fs); |
|
62 return finfo; |
|
63 } |
|
64 |
|
65 int resource_set_finfo(const char *path, DavResource *res, uint32_t finfo) { |
|
66 if(!path || finfo == 0) { |
|
67 return 0; |
|
68 } |
|
69 |
|
70 struct stat s; |
|
71 if(stat(path, &s)) { |
|
72 fprintf(stderr, "failed to stat: %s\n", path); |
|
73 return 1; |
|
74 } |
|
75 return resource_set_finfo_s(&s, res, finfo); |
|
76 } |
|
77 |
|
78 static int buf_write_date(UcxBuffer *buf, time_t t) { |
|
79 struct tm *date = gmtime(&t); |
|
80 char str[32]; |
|
81 size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT", date); |
|
82 return ucx_buffer_write(str, 1, len, buf); |
|
83 } |
|
84 |
|
85 int resource_set_finfo_s(struct stat *s, DavResource *res, uint32_t finfo) { |
|
86 if(finfo == 0) { |
|
87 return 0; |
|
88 } |
|
89 |
|
90 DavXmlNode *content = NULL; |
|
91 |
|
92 if(finfo & FINFO_DATE == FINFO_DATE) { |
|
93 char str[32]; |
|
94 struct tm *date = gmtime(&s->st_mtime); |
|
95 strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT", date); |
|
96 DavXmlNode *mtime = dav_xml_createnode_with_text(DAV_NS, "mtime", str); |
|
97 content = mtime; |
|
98 } |
|
99 #ifndef _WIN32 |
|
100 if(finfo & FINFO_OWNER == FINFO_OWNER) { |
|
101 // TODO |
|
102 } |
|
103 if(finfo & FINFO_MODE == FINFO_MODE) { |
|
104 // TODO |
|
105 } |
|
106 #endif |
|
107 |
|
108 |
|
109 dav_set_property(res, "idav:finfo", content);; |
|
110 |
|
111 return 0; |
|
112 } |