Coverage Report

Created: 2025-11-16 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebsockets/lib/plat/unix/unix-file.c
Line
Count
Source
1
/*
2
 * libwebsockets - small server side websockets and web server implementation
3
 *
4
 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to
8
 * deal in the Software without restriction, including without limitation the
9
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
 * sell copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * IN THE SOFTWARE.
23
 */
24
25
#if !defined(_GNU_SOURCE)
26
#define _GNU_SOURCE
27
#endif
28
#include "private-lib-core.h"
29
30
#include <pwd.h>
31
#include <grp.h>
32
33
#ifdef LWS_WITH_PLUGINS
34
#include <dlfcn.h>
35
#endif
36
#include <dirent.h>
37
38
int lws_plat_apply_FD_CLOEXEC(int n)
39
0
{
40
0
  if (n == -1)
41
0
    return 0;
42
43
0
  return fcntl(n, F_SETFD, FD_CLOEXEC);
44
0
}
45
46
int
47
lws_plat_write_file(const char *filename, void *buf, size_t len)
48
0
{
49
0
  ssize_t m;
50
0
  int fd;
51
52
0
  fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
53
54
0
  if (fd == -1)
55
0
    return 1;
56
57
0
  m = write(fd, buf, len);
58
0
  close(fd);
59
60
0
  if (m < 0)
61
0
    return 1;
62
63
0
  return (size_t)m != len;
64
0
}
65
66
int
67
lws_plat_read_file(const char *filename, void *buf, size_t len)
68
0
{
69
0
  int fd = lws_open(filename, O_RDONLY);
70
0
  ssize_t n;
71
72
0
  if (fd == -1)
73
0
    return -1;
74
75
0
  n = read(fd, buf, len);
76
0
  close(fd);
77
78
0
  return (int)n;
79
0
}
80
81
lws_fop_fd_t
82
_lws_plat_file_open(const struct lws_plat_file_ops *fops_own,
83
        const struct lws_plat_file_ops *fops, const char *filename,
84
        const char *vpath, lws_fop_flags_t *flags)
85
0
{
86
0
  struct stat stat_buf;
87
0
  int ret = lws_open(filename, (int)((*flags) & LWS_FOP_FLAGS_MASK), 0664);
88
0
  lws_fop_fd_t fop_fd;
89
90
0
  if (ret < 0)
91
0
    return NULL;
92
93
0
  if (fstat(ret, &stat_buf) < 0)
94
0
    goto bail;
95
96
0
  fop_fd = malloc(sizeof(*fop_fd));
97
0
  if (!fop_fd)
98
0
    goto bail;
99
100
0
  fop_fd->fops = fops;
101
0
  fop_fd->flags = *flags;
102
0
  fop_fd->fd = ret;
103
0
  fop_fd->filesystem_priv = NULL; /* we don't use it */
104
0
  fop_fd->len = (lws_filepos_t)stat_buf.st_size;
105
0
  fop_fd->pos = 0;
106
107
0
  return fop_fd;
108
109
0
bail:
110
0
  close(ret);
111
0
  return NULL;
112
0
}
113
114
int
115
_lws_plat_file_close(lws_fop_fd_t *fop_fd)
116
0
{
117
0
  int fd = (*fop_fd)->fd;
118
119
0
  free(*fop_fd);
120
0
  *fop_fd = NULL;
121
122
0
  return close(fd);
123
0
}
124
125
lws_fileofs_t
126
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
127
0
{
128
0
  lws_fileofs_t r;
129
130
0
  if (offset > 0 &&
131
0
      offset > (lws_fileofs_t)fop_fd->len - (lws_fileofs_t)fop_fd->pos)
132
0
    offset = (lws_fileofs_t)(fop_fd->len - fop_fd->pos);
133
134
0
  if ((lws_fileofs_t)fop_fd->pos + offset < 0)
135
0
    offset = (lws_fileofs_t)(-fop_fd->pos);
136
137
0
  r = lseek(fop_fd->fd, (off_t)offset, SEEK_CUR);
138
139
0
  if (r >= 0)
140
0
    fop_fd->pos = (lws_filepos_t)r;
141
0
  else
142
0
    lwsl_err("error seeking from cur %ld, offset %ld\n",
143
0
                        (long)fop_fd->pos, (long)offset);
144
145
0
  return r;
146
0
}
147
148
int
149
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
150
        uint8_t *buf, lws_filepos_t len)
151
0
{
152
0
  ssize_t n;
153
154
0
  n = read((int)fop_fd->fd, buf, (size_t)len);
155
0
  if (n == -1l) {
156
0
    *amount = 0;
157
0
    return -1;
158
0
  }
159
0
  fop_fd->pos = (lws_filepos_t)(fop_fd->pos + (lws_filepos_t)n);
160
0
  lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__,
161
0
      (long)n, (long)len, (long)fop_fd->pos,
162
0
      (long)fop_fd->len);
163
0
  *amount = (lws_filepos_t)n;
164
165
0
  return 0;
166
0
}
167
168
int
169
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
170
         uint8_t *buf, lws_filepos_t len)
171
0
{
172
0
  ssize_t n;
173
174
0
  n = write((int)fop_fd->fd, buf, (size_t)len);
175
0
  if (n == -1) {
176
0
    *amount = 0;
177
0
    return -1;
178
0
  }
179
180
0
  fop_fd->pos = (lws_filepos_t)(fop_fd->pos + (lws_filepos_t)n);
181
0
  *amount = (lws_filepos_t)n;
182
183
0
  return 0;
184
0
}
185