| 1 |
/* do not edit automatically generated by mc from mcStream. */ |
| 2 |
/* mcStream.mod provides an interface to create a file from fragments. |
| 3 |
|
| 4 |
Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 9 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 3, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 21 |
<http://www.gnu.org/licenses/>. */ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include "system.h" |
| 25 |
#include <stdbool.h> |
| 26 |
# if !defined (PROC_D) |
| 27 |
# define PROC_D |
| 28 |
typedef void (*PROC_t) (void); |
| 29 |
typedef struct { PROC_t proc; } PROC; |
| 30 |
# endif |
| 31 |
|
| 32 |
# if !defined (TRUE) |
| 33 |
# define TRUE (1==1) |
| 34 |
# endif |
| 35 |
|
| 36 |
# if !defined (FALSE) |
| 37 |
# define FALSE (1==0) |
| 38 |
# endif |
| 39 |
|
| 40 |
# include "GStorage.h" |
| 41 |
#if defined(__cplusplus) |
| 42 |
# undef NULL |
| 43 |
# define NULL 0 |
| 44 |
#endif |
| 45 |
#define _mcStream_C |
| 46 |
|
| 47 |
#include "GmcStream.h" |
| 48 |
# include "GFIO.h" |
| 49 |
# include "Glibc.h" |
| 50 |
# include "GIndexing.h" |
| 51 |
# include "GDynamicStrings.h" |
| 52 |
# include "GFormatStrings.h" |
| 53 |
# include "GSYSTEM.h" |
| 54 |
# include "GStorage.h" |
| 55 |
# include "Galists.h" |
| 56 |
# include "GSFIO.h" |
| 57 |
# include "GM2RTS.h" |
| 58 |
|
| 59 |
typedef FIO_File *mcStream_ptrToFile; |
| 60 |
|
| 61 |
# define maxBuffer 4096 |
| 62 |
static alists_alist listOfFiles; |
| 63 |
static Indexing_Index frag; |
| 64 |
static FIO_File destFile; |
| 65 |
static bool seenDest; |
| 66 |
|
| 67 |
/* |
| 68 |
openFrag - create and open fragment, id, and return the file. |
| 69 |
The file should not be closed by the user. |
| 70 |
*/ |
| 71 |
|
| 72 |
extern "C" FIO_File mcStream_openFrag (unsigned int id); |
| 73 |
|
| 74 |
/* |
| 75 |
setDest - informs the stream module and all fragments must be copied |
| 76 |
info, f. |
| 77 |
*/ |
| 78 |
|
| 79 |
extern "C" void mcStream_setDest (FIO_File f); |
| 80 |
|
| 81 |
/* |
| 82 |
combine - closes all fragments and then writes them in |
| 83 |
order to the destination file. The dest file |
| 84 |
is returned. |
| 85 |
*/ |
| 86 |
|
| 87 |
extern "C" FIO_File mcStream_combine (void); |
| 88 |
|
| 89 |
/* |
| 90 |
removeFiles - remove any fragment. |
| 91 |
*/ |
| 92 |
|
| 93 |
extern "C" void mcStream_removeFiles (void); |
| 94 |
|
| 95 |
/* |
| 96 |
removeLater - |
| 97 |
*/ |
| 98 |
|
| 99 |
static DynamicStrings_String removeLater (DynamicStrings_String filename); |
| 100 |
|
| 101 |
/* |
| 102 |
removeNow - removes a single file, s. |
| 103 |
*/ |
| 104 |
|
| 105 |
static void removeNow (DynamicStrings_String s); |
| 106 |
|
| 107 |
/* |
| 108 |
createTemporaryFile - |
| 109 |
*/ |
| 110 |
|
| 111 |
static FIO_File createTemporaryFile (unsigned int id); |
| 112 |
|
| 113 |
/* |
| 114 |
copy - copies contents of f to the destination file. |
| 115 |
*/ |
| 116 |
|
| 117 |
static void copy (mcStream_ptrToFile p); |
| 118 |
|
| 119 |
|
| 120 |
/* |
| 121 |
removeLater - |
| 122 |
*/ |
| 123 |
|
| 124 |
static DynamicStrings_String removeLater (DynamicStrings_String filename) |
| 125 |
{ |
| 126 |
alists_includeItemIntoList (listOfFiles, reinterpret_cast <void *> (filename)); |
| 127 |
return filename; |
| 128 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 129 |
__builtin_unreachable (); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/* |
| 134 |
removeNow - removes a single file, s. |
| 135 |
*/ |
| 136 |
|
| 137 |
static void removeNow (DynamicStrings_String s) |
| 138 |
{ |
| 139 |
if ((libc_unlink (DynamicStrings_string (s))) != 0) |
| 140 |
{} /* empty. */ |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/* |
| 145 |
createTemporaryFile - |
| 146 |
*/ |
| 147 |
|
| 148 |
static FIO_File createTemporaryFile (unsigned int id) |
| 149 |
{ |
| 150 |
DynamicStrings_String s; |
| 151 |
FIO_File f; |
| 152 |
int p; |
| 153 |
|
| 154 |
s = DynamicStrings_InitString ((const char *) "/tmp/frag-%d-%d.frag", 20); |
| 155 |
p = libc_getpid (); |
| 156 |
s = removeLater (FormatStrings_Sprintf2 (s, (const unsigned char *) &p, (sizeof (p)-1), (const unsigned char *) &id, (sizeof (id)-1))); |
| 157 |
f = SFIO_OpenToWrite (s); |
| 158 |
return f; |
| 159 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 160 |
__builtin_unreachable (); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/* |
| 165 |
copy - copies contents of f to the destination file. |
| 166 |
*/ |
| 167 |
|
| 168 |
static void copy (mcStream_ptrToFile p) |
| 169 |
{ |
| 170 |
typedef struct copy__T1_a copy__T1; |
| 171 |
|
| 172 |
struct copy__T1_a { char array[maxBuffer+1]; }; |
| 173 |
copy__T1 buffer; |
| 174 |
unsigned int b; |
| 175 |
DynamicStrings_String s; |
| 176 |
FIO_File f; |
| 177 |
|
| 178 |
if (p != NULL) |
| 179 |
{ |
| 180 |
f = (*p); |
| 181 |
s = DynamicStrings_InitStringCharStar (FIO_getFileName (f)); |
| 182 |
FIO_Close (f); |
| 183 |
f = SFIO_OpenToRead (s); |
| 184 |
while ((! (FIO_EOF (f))) && (FIO_IsNoError (f))) |
| 185 |
{ |
| 186 |
b = FIO_ReadNBytes (f, maxBuffer, &buffer); |
| 187 |
if (FIO_IsNoError (f)) |
| 188 |
{ |
| 189 |
b = FIO_WriteNBytes (destFile, b, &buffer); |
| 190 |
} |
| 191 |
else if (! (FIO_EOF (f))) |
| 192 |
{ |
| 193 |
/* avoid dangling else. */ |
| 194 |
libc_printf ((const char *) "mcStream.mod:copy: error seen when reading file fragment: %s\\n", 62, DynamicStrings_string (s)); |
| 195 |
libc_exit (1); |
| 196 |
} |
| 197 |
} |
| 198 |
FIO_Close (f); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/* |
| 204 |
openFrag - create and open fragment, id, and return the file. |
| 205 |
The file should not be closed by the user. |
| 206 |
*/ |
| 207 |
|
| 208 |
extern "C" FIO_File mcStream_openFrag (unsigned int id) |
| 209 |
{ |
| 210 |
FIO_File f; |
| 211 |
mcStream_ptrToFile p; |
| 212 |
|
| 213 |
f = createTemporaryFile (id); |
| 214 |
Storage_ALLOCATE ((void **) &p, sizeof (FIO_File)); |
| 215 |
(*p) = f; |
| 216 |
Indexing_PutIndice (frag, id, reinterpret_cast <void *> (p)); |
| 217 |
return f; |
| 218 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 219 |
__builtin_unreachable (); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/* |
| 224 |
setDest - informs the stream module and all fragments must be copied |
| 225 |
info, f. |
| 226 |
*/ |
| 227 |
|
| 228 |
extern "C" void mcStream_setDest (FIO_File f) |
| 229 |
{ |
| 230 |
seenDest = true; |
| 231 |
destFile = f; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/* |
| 236 |
combine - closes all fragments and then writes them in |
| 237 |
order to the destination file. The dest file |
| 238 |
is returned. |
| 239 |
*/ |
| 240 |
|
| 241 |
extern "C" FIO_File mcStream_combine (void) |
| 242 |
{ |
| 243 |
if (! seenDest) |
| 244 |
{ |
| 245 |
M2RTS_HALT (-1); |
| 246 |
__builtin_unreachable (); |
| 247 |
} |
| 248 |
Indexing_ForeachIndiceInIndexDo (frag, (Indexing_IndexProcedure) {(Indexing_IndexProcedure_t) copy}); |
| 249 |
mcStream_removeFiles (); |
| 250 |
return destFile; |
| 251 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 252 |
__builtin_unreachable (); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/* |
| 257 |
removeFiles - remove any fragment. |
| 258 |
*/ |
| 259 |
|
| 260 |
extern "C" void mcStream_removeFiles (void) |
| 261 |
{ |
| 262 |
alists_foreachItemInListDo (listOfFiles, (alists_performOperation) {(alists_performOperation_t) removeNow}); |
| 263 |
alists_killList (&listOfFiles); |
| 264 |
listOfFiles = alists_initList (); |
| 265 |
} |
| 266 |
|
| 267 |
extern "C" void _M2_mcStream_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 268 |
{ |
| 269 |
listOfFiles = alists_initList (); |
| 270 |
seenDest = false; |
| 271 |
frag = Indexing_InitIndex (1); |
| 272 |
} |
| 273 |
|
| 274 |
extern "C" void _M2_mcStream_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 275 |
{ |
| 276 |
} |