src/server/plugins/postgresql/vfs.c

Thu, 03 Feb 2022 18:03:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 03 Feb 2022 18:03:27 +0100
branch
webdav
changeset 284
eab579b8c80d
parent 283
25e5b771677d
child 285
96e53bd94958
permissions
-rw-r--r--

add missing postgresql vfs io functions

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
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
115 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
116 // resourcepool is required
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
117 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
118 if(!resource_pool) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
119 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
120 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
121 }
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
122
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
123 // 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
124 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
125 if(!resdata) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
126 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
127 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
128 }
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
129 // 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
130
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
131 // 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
132 // 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
133 // 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
134 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
135 if(!vfs) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
136 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
137 }
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 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
140 if(!vfs_priv) {
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
141 pool_free(sn->pool, vfs);
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
142 return NULL;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
143 }
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
144 vfs_priv->connection = resdata->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
145 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
146
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
147 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
148 vfs->flags = 0;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
149 vfs->instance = vfs_priv;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
150
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
151 return vfs;
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
152 }
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
153
0cb4eda146c4 add function to create a postgresql-based VFS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 275
diff changeset
154
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
155 int pg_resolve_path(
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
156 VFSContext *ctx,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
157 const char *path,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
158 int64_t *parent_id,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
159 int64_t *resource_id,
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
160 Oid *oid,
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
161 const char **resource_name,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
162 WSBool *iscollection,
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
163 struct stat *s)
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
164 {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
165 // basic path validation
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
166 if(!path) return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
167 size_t pathlen = strlen(path);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
168 if(pathlen == 0) return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
169 if(path[0] != '/') {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
170 return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
171 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
172
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
173 char *pathf = NULL;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
174 if(pathlen > 1 && path[pathlen-1] == '/') {
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
175 pathf = malloc(pathlen);
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
176 memcpy(pathf, path, pathlen);
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
177 pathf[pathlen-1] = 0; // remove trailing '/'
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
178 path = pathf;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
179 }
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
180
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
181 // get last node of path
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
182 *resource_name = util_resource_name(path);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
183
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
184 VFS *vfs = ctx->vfs;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
185 PgVFS *pg = vfs->instance;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
186
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
187 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
188 PGresult *result = PQexecParams(
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
189 pg->connection,
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
190 sql,
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
191 1, // number of parameters
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
192 NULL,
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
193 &path, // parameter value
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
194 NULL,
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
195 NULL,
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
196 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
197
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
198 if(pathf) {
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
199 free(pathf);
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
200 }
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
201
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
202 if(!result) return 1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
203
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
204 int ret = 1;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
205 //int nfields = PQnfields(result);
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
206 int nrows = PQntuples(result);
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
207 if(nrows == 1) {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
208 char *resource_id_str = PQgetvalue(result, 0, 0);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
209 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
210 char *iscol = PQgetvalue(result, 0, 4);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
211 char *lastmodified = PQgetvalue(result, 0, 5);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
212 char *creationdate = PQgetvalue(result, 0, 6);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
213 char *contentlength = PQgetvalue(result, 0, 7);
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
214 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
215 if(util_strtoint(resource_id_str, resource_id)) {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
216 ret = 0; // success
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
217 }
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
218 // optionally get parent_id
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
219 util_strtoint(parent_id_str, parent_id);
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
220 }
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
221
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
222 if(oid) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
223 char *resoid = PQgetvalue(result, 0, 3);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
224 int64_t roid;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
225 if(resoid && util_strtoint(resoid, &roid)) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
226 *oid = roid;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
227 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
228 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
229
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
230 if(iscollection && iscol) {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
231 *iscollection = iscol[0] == 't' ? TRUE : FALSE;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
232 }
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
233
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
234 if(s) {
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
235 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
236 }
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
237 } else {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
238 ctx->vfs_errno = ENOENT;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
239 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
240
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
241 PQclear(result);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
242
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
243 return ret;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
244 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
245
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
246
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
247 void pg_set_stat(
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
248 struct stat *s,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
249 const char *iscollection,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
250 const char *lastmodified,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
251 const char *creationdate,
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
252 const char *contentlength)
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
253 {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
254 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
255 if(iscollection) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
256 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
257 if(iscol) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
258 s->st_mode |= 0x4000;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
259 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
260 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
261 // TODO: lastmodified, creationdate
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
262 // set some test values != 0
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
263 s->st_mtime = time(NULL);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
264
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
265 if(contentlength) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
266 int64_t len;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
267 if(util_strtoint(contentlength, &len)) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
268 s->st_size = len;
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
269 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
270 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
271 }
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
272
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 /* -------------------------- VFS functions -------------------------- */
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 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
276 VFS *vfs = ctx->vfs;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
277 PgVFS *pg = vfs->instance;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
278
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
279 const char *resname;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
280 int64_t resource_id, parent_id;
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
281 resource_id = -1;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
282 parent_id = -1;
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
283 WSBool iscollection;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
284 struct stat s;
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
285 Oid oid = 0;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
286 if(pg_resolve_path(ctx, path, &parent_id, &resource_id, &oid, &resname, &iscollection, &s)) {
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
287 return NULL;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
288 }
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
290 VFSFile *file = pool_malloc(ctx->pool, sizeof(VFSFile));
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
291 if(!file) {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
292 return NULL;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
293 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
294 PgFile *pgfile = pool_malloc(ctx->pool, sizeof(PgFile));
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
295 if(!pgfile) {
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
296 pool_free(ctx->pool, file);
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
297 return NULL;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
298 }
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
299
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
300 int fd = -1;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
301 if(!iscollection) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
302 if (PQstatus(pg->connection) != CONNECTION_OK) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
303 fd = -2;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
304 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
305
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
306 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
307 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
308 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
309
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
310 pgfile->iscollection = iscollection;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
311 pgfile->resource_id = resource_id;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
312 pgfile->parent_id = parent_id;
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
313 pgfile->oid = oid;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
314 pgfile->fd = fd;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
315 pgfile->s = s;
278
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
316
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
317 file->ctx = ctx;
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
318 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
319 file->fd = -1;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
320 file->data = pgfile;
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
321
38bf7b42b58c implement pg_vfs_open
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 276
diff changeset
322 return file;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 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
326 int64_t parent_id, resource_id;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
327 const char *resname;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
328 WSBool iscollection;
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
329 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
330 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 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
333 PgFile *pgfile = fd->data;
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
334 memcpy(buf, &pgfile->s, sizeof(struct stat));
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
335 return 0;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 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
339 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
340 if(!file) return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
341 return pg_vfs_fdopendir(ctx, file);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 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
345 PgFile *pg = fd->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
346 if(!pg->iscollection) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
347 ctx->vfs_errno = ENOTDIR;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
348 return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
349 }
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
351 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
352 if(!dir) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
353 fd->io->close(fd);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
354 ctx->vfs_errno = ENOMEM;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
355 return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
356 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
357
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
358 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
359 if(!pgdir) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
360 fd->io->close(fd);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
361 pool_free(ctx->pool, dir);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
362 ctx->vfs_errno = ENOMEM;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
363 return NULL;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
364 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
365 memset(pgdir, 0, sizeof(PgDir));
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
366 pgdir->file = fd;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
367
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
368 dir->ctx = ctx;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
369 dir->io = &pg_vfs_dirio_class;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
370 dir->data = pgdir;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
371 dir->fd = -1;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
372
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
373 return dir;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 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
377 return 1;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 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
381 return 1;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 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
385 return 1;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 /* -------------------------- VFS_IO functions -------------------------- */
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 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
392 PgVFS *pgvfs = fd->ctx->vfs->instance;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
393 PgFile *pg = fd->data;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
394 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
395 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 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
398 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
399 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
400 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
401 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 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
404 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
405 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
406 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
407 return -1;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
408 }
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
409 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
410 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 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
413 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
414 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
415 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
416 return -1;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
417 }
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
418 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
419 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 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
422 PgVFS *pgvfs = fd->ctx->vfs->instance;
284
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
423 PgFile *pg = fd->data;
eab579b8c80d add missing postgresql vfs io functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 283
diff changeset
424 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
425 }
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 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
428 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
429 PgFile *pg = fd->data;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430
283
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
431 if(pg->fd >= 0) {
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
432 PgVFS *pgvfs = fd->ctx->vfs->instance;
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
433 lo_close(pgvfs->connection, pg->fd);
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
434 }
25e5b771677d minimal working send_file with postgresql vfs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 282
diff changeset
435
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
436 pool_free(pool, pg);
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
437 pool_free(pool, fd);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 }
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 /* -------------------------- VFS_DIRIO functions -------------------------- */
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
443 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
444 VFS *vfs = dir->ctx->vfs;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
445 PgVFS *pgvfs = vfs->instance;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
446 PgFile *pgfd = pg->file->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
447 PgDir *pgdir = dir->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
448
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
449 char resid_param[32];
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
450 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
451
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
452 const char *param = resid_param;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
453
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
454 PGresult *result = PQexecParams(
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
455 pgvfs->connection,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
456 sql_get_children,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
457 1, // number of parameters
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
458 NULL,
281
e9dc53661df4 add pg vfs stat/fstat implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 280
diff changeset
459 &param, // param: parent resource_id
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
460 NULL,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
461 NULL,
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
462 0); // 0: result in text format
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
463 if(!result) return 1;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
464
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
465 pgdir->result = result;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
466 pgdir->nrows = PQntuples(result);
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
467 return 0;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
468 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
469
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 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
471 PgDir *pg = dir->data;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
472 if(!pg->result) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
473 if(load_dir(dir, pg)) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
474 return 0;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
475 }
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
476 }
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
478 if(pg->row >= pg->nrows) {
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
479 return 0; // EOF
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
480 }
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 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
483
282
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
484 if(getstat) {
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
485 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
486
cfb588e27198 add support for getstat parameter in pg readdir
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 281
diff changeset
487 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
488 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
489 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
490 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
491 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
492 }
279
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
493
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
494 pg->row++;
79029fe26aae add basic postgresql vfs directory functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 278
diff changeset
495 return 1;
275
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 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
499 pool_handle_t *pool = dir->ctx->pool;
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
500 PgDir *pg = dir->data;
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
501 if(pg->result) {
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
502 PQclear(pg->result);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
503 }
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
504 PgFile *pgfile = pg->file->data;
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505
280
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
506 pool_free(pool, pgfile);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
507 pool_free(pool, pg->file);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
508 pool_free(pool, pg);
d0d5a970292f implement pg_vfs_dirio_close
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 279
diff changeset
509 pool_free(pool, dir);
275
535004faa1a5 add empty postgresql VFS functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 }

mercurial