src/server/plugins/postgresql/vfs.c

Sat, 19 Feb 2022 16:34:29 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 19 Feb 2022 16:34:29 +0100
branch
webdav
changeset 287
a171da778817
parent 285
96e53bd94958
child 289
285d483db2fb
permissions
-rw-r--r--

prepare build system for postgresql plugin tests

275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2022 Olaf Wintermann. All rights reserved.
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "vfs.h"
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
31 #include <inttypes.h>
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
32
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
33 #include "../../util/util.h"
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
34
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 static VFS pg_vfs_class = {
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 pg_vfs_open,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 pg_vfs_stat,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 pg_vfs_fstat,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 pg_vfs_opendir,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 pg_vfs_fdopendir,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 pg_vfs_mkdir,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 pg_vfs_unlink,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 pg_vfs_rmdir
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 };
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 static VFS_IO pg_vfs_io_class = {
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 pg_vfs_io_read,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 pg_vfs_io_write,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 pg_vfs_io_pread,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 pg_vfs_io_pwrite,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 pg_vfs_io_seek,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 pg_vfs_io_close,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 NULL, // no pg aio implementation yet
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 NULL
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 };
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 static VFS_DIRIO pg_vfs_dirio_class = {
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 pg_vfs_dirio_readdir,
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 pg_vfs_dirio_close
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 };
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
276
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
62
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
63 /*
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
64 * SQL Queries
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
65 */
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
66
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
67 // Resolves a path into resource_id and parent_id
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
68 // params: $1: path string
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
69 static const char *sql_resolve_path =
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
70 "with recursive resolvepath as (\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
71 select\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
72 resource_id,\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
73 parent_id,\n\
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
74 '' as fullpath,\n\
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
75 resoid,\n\
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
76 iscollection,\n\
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
77 lastmodified,\n\
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
78 creationdate,\n\
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
79 contentlength,\n\
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
80 regexp_split_to_array($1, '/') as pathelm,\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
81 1 as pathdepth\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
82 from Resource\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
83 where parent_id is null\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
84 union\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
85 select\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
86 r.resource_id,\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
87 r.parent_id,\n\
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
88 p.fullpath || '/' || r.nodename,\n\
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
89 r.resoid,\n\
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
90 r.iscollection,\n\
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
91 r.lastmodified,\n\
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
92 r.creationdate,\n\
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
93 r.contentlength,\n\
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
94 p.pathelm,\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
95 p.pathdepth + 1\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
96 from Resource r\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
97 inner join resolvepath p on r.parent_id = p.resource_id\n\
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
98 where p.pathelm[p.pathdepth+1] = r.nodename\n\
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
99 )\n\
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
100 select resource_id, parent_id, fullpath, resoid, iscollection, lastmodified, creationdate, contentlength from resolvepath\n\
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
101 where fullpath = $1 ;";
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
102
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
103 // Same as sql_resolve_path, but it returns the root collection
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
104 // params: $1: path string (should be '/')
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
105 static const char *sql_get_root = "select resource_id, parent_id, $1 as fullpath, resoid, true as iscollection, lastmodified, creationdate, contentlength from Resource where parent_id is null;";
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
106
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
107 // Get all children of a specific collection
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
108 // params: $1: parent resource_id
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
109 static const char *sql_get_children = "select resource_id, nodename, iscollection, lastmodified, creationdate, contentlength from Resource where parent_id = $1;";
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
110
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
111 // Get resource
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
112 // params: $1: resource_id
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
113 static const char *sql_get_resource = "select resource_id, nodename, iscollection, lastmodified, creationdate, contentlength from Resource where resource_id = $1;";
276
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
114
285
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
115 // Create resource
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
116 // params: $1: parent_id
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
117 // $2: node name
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
118 static const char *sql_create_resource =
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
119 "insert into Resource (parent_id, nodename, iscollection, lastmodified, creationdate, contentlength, resoid) values\n\
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
120 ($1, $2, false, now(), now(), 0, lo_creat(-1))\n\
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
121 returning resource_id, resoid, lastmodified, creationdate;";
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
122
276
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
123 VFS* pg_vfs_create(Session *sn, Request *rq, pblock *pb) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
124 // resourcepool is required
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
125 char *resource_pool = pblock_findval("resourcepool", pb);
276
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
126 if(!resource_pool) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
127 log_ereport(LOG_MISCONFIG, "pg_vfs_create: missing resourcepool parameter");
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
128 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
129 }
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
130
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
131 // get the resource first (most likely to fail due to misconfig)
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
132 ResourceData *resdata = resourcepool_lookup(sn, rq, resource_pool, 0);
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
133 if(!resdata) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
134 log_ereport(LOG_MISCONFIG, "postgresql vfs: resource pool %s not found", resource_pool);
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
135 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
136 }
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
137 // resdata will be freed automatically when the request is finished
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
138
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
139 // Create a new VFS object and a separate instance object
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
140 // VFS contains fptrs that can be copied from pg_vfs_class
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
141 // instance contains request specific data (PGconn)
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
142 VFS *vfs = pool_malloc(sn->pool, sizeof(VFS));
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
143 if(!vfs) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
144 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
145 }
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
146
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
147 PgVFS *vfs_priv = pool_malloc(sn->pool, sizeof(PgVFS));
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
148 if(!vfs_priv) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
149 pool_free(sn->pool, vfs);
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
150 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
151 }
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
152 vfs_priv->connection = resdata->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
153 vfs_priv->pg_resource = resdata;
276
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
154
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
155 memcpy(vfs, &pg_vfs_class, sizeof(VFS));
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
156 vfs->flags = 0;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
157 vfs->instance = vfs_priv;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
158
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
159 return vfs;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
160 }
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
161
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
162
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
163 int pg_resolve_path(
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
164 VFSContext *ctx,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
165 const char *path,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
166 int64_t *parent_id,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
167 int64_t *resource_id,
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
168 Oid *oid,
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
169 const char **resource_name,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
170 WSBool *iscollection,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
171 struct stat *s)
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
172 {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
173 // basic path validation
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
174 if(!path) return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
175 size_t pathlen = strlen(path);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
176 if(pathlen == 0) return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
177 if(path[0] != '/') {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
178 return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
179 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
180
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
181 char *pathf = NULL;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
182 if(pathlen > 1 && path[pathlen-1] == '/') {
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
183 pathf = malloc(pathlen);
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
184 memcpy(pathf, path, pathlen);
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
185 pathf[pathlen-1] = 0; // remove trailing '/'
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
186 path = pathf;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
187 }
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
188
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
189 // get last node of path
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
190 *resource_name = util_resource_name(path);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
191
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
192 VFS *vfs = ctx->vfs;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
193 PgVFS *pg = vfs->instance;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
194
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
195 const char *sql = pathlen == 1 ? sql_get_root : sql_resolve_path;
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
196 PGresult *result = PQexecParams(
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
197 pg->connection,
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
198 sql,
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
199 1, // number of parameters
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
200 NULL,
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
201 &path, // parameter value
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
202 NULL,
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
203 NULL,
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
204 0); // 0: result in text format
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
205
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
206 if(pathf) {
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
207 free(pathf);
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
208 }
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
209
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
210 if(!result) return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
211
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
212 int ret = 1;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
213 //int nfields = PQnfields(result);
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
214 int nrows = PQntuples(result);
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
215 if(nrows == 1) {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
216 char *resource_id_str = PQgetvalue(result, 0, 0);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
217 char *parent_id_str = PQgetvalue(result, 0, 1);
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
218 char *iscol = PQgetvalue(result, 0, 4);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
219 char *lastmodified = PQgetvalue(result, 0, 5);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
220 char *creationdate = PQgetvalue(result, 0, 6);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
221 char *contentlength = PQgetvalue(result, 0, 7);
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
222 if(resource_id_str && parent_id_str) {
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
223 if(util_strtoint(resource_id_str, resource_id)) {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
224 ret = 0; // success
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
225 }
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
226 // optionally get parent_id
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
227 util_strtoint(parent_id_str, parent_id);
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
228 }
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
229
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
230 if(oid) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
231 char *resoid = PQgetvalue(result, 0, 3);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
232 int64_t roid;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
233 if(resoid && util_strtoint(resoid, &roid)) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
234 *oid = roid;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
235 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
236 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
237
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
238 if(iscollection && iscol) {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
239 *iscollection = iscol[0] == 't' ? TRUE : FALSE;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
240 }
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
241
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
242 if(s) {
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
243 pg_set_stat(s, iscol, lastmodified, creationdate, contentlength);
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
244 }
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
245 } else {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
246 ctx->vfs_errno = ENOENT;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
247 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
248
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
249 PQclear(result);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
250
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
251 return ret;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
252 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
253
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
254
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
255 void pg_set_stat(
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
256 struct stat *s,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
257 const char *iscollection,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
258 const char *lastmodified,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
259 const char *creationdate,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
260 const char *contentlength)
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
261 {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
262 memset(s, 0, sizeof(struct stat));
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
263 if(iscollection) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
264 WSBool iscol = iscollection[0] == 't' ? TRUE : FALSE;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
265 if(iscol) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
266 s->st_mode |= 0x4000;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
267 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
268 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
269 // TODO: lastmodified, creationdate
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
270 // set some test values != 0
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
271 s->st_mtime = time(NULL);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
272
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
273 if(contentlength) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
274 int64_t len;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
275 if(util_strtoint(contentlength, &len)) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
276 s->st_size = len;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
277 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
278 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
279 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
280
285
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
281 int pg_create_file(
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
282 VFSContext *ctx,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
283 PgVFS *pg,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
284 const char *path,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
285 int64_t *new_resource_id,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
286 int64_t *res_parent_id,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
287 Oid *oid,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
288 const char **resource_name,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
289 struct stat *s)
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
290 {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
291 char *parent_path = util_parent_path(path);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
292 if(!parent_path) return 1;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
293
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
294 const char *nodename = util_resource_name(path);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
295
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
296 // resolve the parent path
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
297 // if the parent path can't be resolved, we are done
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
298 const char *resname;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
299 int64_t resource_id, parent_id;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
300 resource_id = -1;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
301 parent_id = -1;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
302 WSBool iscollection;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
303 Oid unused_oid = 0;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
304
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
305 int err = pg_resolve_path(ctx, parent_path, &parent_id, &resource_id, &unused_oid, &resname, &iscollection, NULL);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
306 FREE(parent_path);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
307 if(err) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
308 return 1;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
309 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
310
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
311 // parent path exists, check if it is a collection
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
312 if(!iscollection) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
313 return 1;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
314 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
315
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
316 // create new Resource
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
317 char resid_str[32];
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
318 snprintf(resid_str, 32, "%" PRId64, resource_id); // convert parent resource_id to string
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
319
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
320 const char* params[2] = { resid_str, nodename };
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
321 PGresult *result = PQexecParams(
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
322 pg->connection,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
323 sql_create_resource,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
324 2, // number of parameters
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
325 NULL,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
326 params, // parameter value
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
327 NULL,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
328 NULL,
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
329 0); // 0: result in text format
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
330
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
331 if(!result) return 1;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
332
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
333 int ret = 1;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
334 if(PQntuples(result) == 1) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
335 // sql insert succesful
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
336 ret = 0;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
337
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
338 char *id_str = PQgetvalue(result, 0, 0);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
339 char *oid_str = PQgetvalue(result, 0, 1);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
340 char *lastmodified = PQgetvalue(result, 0, 2);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
341 char *creationdate = PQgetvalue(result, 0, 3);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
342
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
343 if(new_resource_id) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
344 if(!id_str || !util_strtoint(id_str, new_resource_id)) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
345 ret = 1; // shouldn't happen
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
346 log_ereport(LOG_FAILURE, "Postgresql VFS: sql_create_resource: Could not convert resource_id to int");
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
347 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
348 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
349 if(res_parent_id) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
350 // resource_id is still the id of the parent from previous pg_resolve_path call
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
351 *res_parent_id = resource_id;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
352 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
353 if(oid) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
354 int64_t i;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
355 if(!oid_str || !util_strtoint(oid_str, &i)) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
356 ret = 1; // shouldn't happen
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
357 log_ereport(LOG_FAILURE, "Postgresql VFS: sql_create_resource: Could not convert oid to int");
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
358 } else {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
359 *oid = i;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
360 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
361 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
362 if(resource_name) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
363 *resource_name = nodename;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
364 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
365 if(s) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
366 pg_set_stat(s, 0, lastmodified, creationdate, NULL);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
367 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
368 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
369
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
370 PQclear(result);
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
371
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
372 return ret;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
373 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
374
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 /* -------------------------- VFS functions -------------------------- */
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 SYS_FILE pg_vfs_open(VFSContext *ctx, const char *path, int oflags) {
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
378 VFS *vfs = ctx->vfs;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
379 PgVFS *pg = vfs->instance;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
380
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
381 const char *resname;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
382 int64_t resource_id, parent_id;
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
383 resource_id = -1;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
384 parent_id = -1;
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
385 WSBool iscollection;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
386 struct stat s;
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
387 Oid oid = 0;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
388 if(pg_resolve_path(ctx, path, &parent_id, &resource_id, &oid, &resname, &iscollection, &s)) {
285
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
389 if((oflags & O_CREAT) == O_CREAT) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
390 if(pg_create_file(ctx, pg, path, &resource_id, &parent_id, &oid, &resname, &s)) {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
391 return NULL;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
392 }
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
393 iscollection = 0;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
394 } else {
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
395 return NULL;
96e53bd94958 add support for O_CREAT flag in postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 284
diff changeset
396 }
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
397 }
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
399 VFSFile *file = pool_malloc(ctx->pool, sizeof(VFSFile));
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
400 if(!file) {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
401 return NULL;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
402 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
403 PgFile *pgfile = pool_malloc(ctx->pool, sizeof(PgFile));
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
404 if(!pgfile) {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
405 pool_free(ctx->pool, file);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
406 return NULL;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
407 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
408
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
409 int fd = -1;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
410 if(!iscollection) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
411 if (PQstatus(pg->connection) != CONNECTION_OK) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
412 fd = -2;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
413 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
414
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
415 int lo_mode = INV_READ; // TODO: evaluate oflags
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
416 fd = lo_open(pg->connection, oid, lo_mode);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
417 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
418
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
419 pgfile->iscollection = iscollection;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
420 pgfile->resource_id = resource_id;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
421 pgfile->parent_id = parent_id;
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
422 pgfile->oid = oid;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
423 pgfile->fd = fd;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
424 pgfile->s = s;
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
425
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
426 file->ctx = ctx;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
427 file->io = iscollection ? &pg_vfs_io_class : &pg_vfs_io_class;
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
428 file->fd = -1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
429 file->data = pgfile;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
430
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
431 return file;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 int pg_vfs_stat(VFSContext *ctx, const char *path, struct stat *buf) {
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
435 int64_t parent_id, resource_id;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
436 const char *resname;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
437 WSBool iscollection;
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
438 return pg_resolve_path(ctx, path, &parent_id, &resource_id, NULL, &resname, &iscollection, buf);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 int pg_vfs_fstat(VFSContext *ctx, SYS_FILE fd, struct stat *buf) {
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
442 PgFile *pgfile = fd->data;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
443 memcpy(buf, &pgfile->s, sizeof(struct stat));
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
444 return 0;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 VFS_DIR pg_vfs_opendir(VFSContext *ctx, const char *path) {
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
448 VFSFile *file = pg_vfs_open(ctx, path, O_RDONLY);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
449 if(!file) return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
450 return pg_vfs_fdopendir(ctx, file);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 VFS_DIR pg_vfs_fdopendir(VFSContext *ctx, SYS_FILE fd) {
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
454 PgFile *pg = fd->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
455 if(!pg->iscollection) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
456 ctx->vfs_errno = ENOTDIR;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
457 return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
458 }
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
460 VFSDir *dir = pool_malloc(ctx->pool, sizeof(VFSDir));
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
461 if(!dir) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
462 fd->io->close(fd);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
463 ctx->vfs_errno = ENOMEM;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
464 return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
465 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
466
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
467 PgDir *pgdir = pool_malloc(ctx->pool, sizeof(PgDir));
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
468 if(!pgdir) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
469 fd->io->close(fd);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
470 pool_free(ctx->pool, dir);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
471 ctx->vfs_errno = ENOMEM;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
472 return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
473 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
474 memset(pgdir, 0, sizeof(PgDir));
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
475 pgdir->file = fd;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
476
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
477 dir->ctx = ctx;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
478 dir->io = &pg_vfs_dirio_class;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
479 dir->data = pgdir;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
480 dir->fd = -1;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
481
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
482 return dir;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 int pg_vfs_mkdir(VFSContext *ctx, const char *path) {
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
486 return 1;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 int pg_vfs_unlink(VFSContext *ctx, const char *path) {
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
490 return 1;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 int pg_vfs_rmdir(VFSContext *Ctx, const char *path) {
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
494 return 1;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 /* -------------------------- VFS_IO functions -------------------------- */
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 ssize_t pg_vfs_io_read(SYS_FILE fd, void *buf, size_t nbyte) {
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
501 PgVFS *pgvfs = fd->ctx->vfs->instance;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
502 PgFile *pg = fd->data;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
503 return lo_read(pgvfs->connection, pg->fd, buf, nbyte);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
506 ssize_t pg_vfs_io_write(SYS_FILE fd, const void *buf, size_t nbyte) {
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
507 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
508 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
509 return lo_write(pgvfs->connection, pg->fd, buf, nbyte);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 ssize_t pg_vfs_io_pread(SYS_FILE fd, void *buf, size_t nbyte, off_t offset) {
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
513 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
514 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
515 if(lo_lseek64(pgvfs->connection, pg->fd, offset, SEEK_SET) == -1) {
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
516 return -1;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
517 }
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
518 return lo_read(pgvfs->connection, pg->fd, buf, nbyte);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 ssize_t pg_vfs_io_pwrite(SYS_FILE fd, const void *buf, size_t nbyte, off_t offset) {
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
522 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
523 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
524 if(lo_lseek64(pgvfs->connection, pg->fd, offset, SEEK_SET) == -1) {
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
525 return -1;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
526 }
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
527 return lo_write(pgvfs->connection, pg->fd, buf, nbyte);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 off_t pg_vfs_io_seek(SYS_FILE fd, off_t offset, int whence) {
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
531 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
532 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
533 return lo_lseek64(pgvfs->connection, pg->fd, offset, whence);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536 void pg_vfs_io_close(SYS_FILE fd) {
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
537 pool_handle_t *pool = fd->ctx->pool;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
538 PgFile *pg = fd->data;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
539
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
540 if(pg->fd >= 0) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
541 PgVFS *pgvfs = fd->ctx->vfs->instance;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
542 lo_close(pgvfs->connection, pg->fd);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
543 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
544
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
545 pool_free(pool, pg);
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
546 pool_free(pool, fd);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 /* -------------------------- VFS_DIRIO functions -------------------------- */
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
552 static int load_dir(VFSDir *dir, PgDir *pg) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
553 VFS *vfs = dir->ctx->vfs;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
554 PgVFS *pgvfs = vfs->instance;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
555 PgFile *pgfd = pg->file->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
556 PgDir *pgdir = dir->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
557
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
558 char resid_param[32];
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
559 snprintf(resid_param, 32, "%" PRId64, pgfd->resource_id);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
560
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
561 const char *param = resid_param;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
562
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
563 PGresult *result = PQexecParams(
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
564 pgvfs->connection,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
565 sql_get_children,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
566 1, // number of parameters
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
567 NULL,
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
568 &param, // param: parent resource_id
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
569 NULL,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
570 NULL,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
571 0); // 0: result in text format
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
572 if(!result) return 1;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
573
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
574 pgdir->result = result;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
575 pgdir->nrows = PQntuples(result);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
576 return 0;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
577 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
578
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 int pg_vfs_dirio_readdir(VFS_DIR dir, VFS_ENTRY *entry, int getstat) {
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
580 PgDir *pg = dir->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
581 if(!pg->result) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
582 if(load_dir(dir, pg)) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
583 return 0;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
584 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
585 }
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
587 if(pg->row >= pg->nrows) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
588 return 0; // EOF
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
589 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
590
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
591 entry->name = PQgetvalue(pg->result, pg->row, 1);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
592
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
593 if(getstat) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
594 memset(&entry->stat, 0, sizeof(struct stat));
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
595
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
596 char *iscollection = PQgetvalue(pg->result, pg->row, 2);
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
597 char *lastmodified = PQgetvalue(pg->result, pg->row, 3);
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
598 char *creationdate = PQgetvalue(pg->result, pg->row, 4);
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
599 char *contentlength = PQgetvalue(pg->result, pg->row, 5);
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
600 pg_set_stat(&entry->stat, iscollection, lastmodified, creationdate, contentlength);
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
601 }
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
602
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
603 pg->row++;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
604 return 1;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 void pg_vfs_dirio_close(VFS_DIR dir) {
280
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
608 pool_handle_t *pool = dir->ctx->pool;
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
609 PgDir *pg = dir->data;
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
610 if(pg->result) {
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
611 PQclear(pg->result);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
612 }
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
613 PgFile *pgfile = pg->file->data;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
614
280
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
615 pool_free(pool, pgfile);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
616 pool_free(pool, pg->file);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
617 pool_free(pool, pg);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
618 pool_free(pool, dir);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
619 }

mercurial