file locking

This commit is contained in:
Udo 2021-12-07 01:32:46 +00:00
parent 0685fdf29a
commit 1f0f08f787
6 changed files with 74 additions and 26 deletions

Binary file not shown.

View File

@ -6,7 +6,8 @@
#include <arpa/inet.h>
#include <netdb.h>
#include <execinfo.h>
#include <fcntl.h>
#include <sys/file.h>
#include "sys.h"
String shell_exec(String cmd)
@ -91,33 +92,48 @@ bool file_exists(String path)
String file_get_contents(String file_name)
{
try
{
std::ifstream ifs(file_name);
String content(
(std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
return(content);
}
catch(std::exception e)
/*std::ifstream ifs(file_name);
printf("stream file desc %i\n", ifs.filedesc());
String content(
(std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );*/
char buf[512];
String content;
s32 fd = open(file_name.c_str(), O_RDONLY);
if(fd == -1)
{
printf("(!) Could not read %s\n", file_name.c_str());
return("");
}
flock(fd, LOCK_SH);
s64 bytes_read = 0;
//s64 size = lseek(fd, 0, SEEK_END);
//lseek(fd, 0, SEEK_SET);
//content.reserve(size+1);
while((bytes_read = read(fd, buf, 512)) > 0)
{
content.append(buf, bytes_read);
}
flock(fd, LOCK_UN);
close(fd);
return(content);
}
bool file_put_contents(String file_name, String content)
{
try
{
std::ofstream out(file_name);
out << content;
out.close();
return(true);
}
catch(std::exception e)
s32 fd = open(file_name.c_str(), O_WRONLY | O_CREAT | O_TRUNC);
if(fd == -1)
{
printf("(!) Could not write %s\n", file_name.c_str());
return(false);
}
flock(fd, LOCK_EX);
write(fd, content.data(), content.length());
flock(fd, LOCK_UN);
close(fd);
return(true);
}
String get_cwd()

22
test/fileio.uce Normal file
View File

@ -0,0 +1,22 @@
RENDER()
{
DTree t;
<>
<link rel="stylesheet" href='style.css'></link>
<h1>
<a href="index.uce">UCE Test</a>:
FileI/O
</h1>
File IO:
<pre style="white-space: pre-wrap"><?= file_get_contents("fileio.uce") ?></pre>
Params
<pre><?= var_dump(context->params) ?></pre>
</>
}

View File

@ -23,6 +23,7 @@ RENDER()
<li><a href="json.uce">JSON</a></li>
<li><a href="memcached.uce">Memcached</a></li>
<li><a href="mysql.uce">MySQL Connector</a></li>
<li><a href="fileio.uce">File I/O</a></li>
</ul>
<pre><?
echo("Worker PID: "+std::to_string(my_pid)+"\n");

View File

@ -2,7 +2,7 @@
void show_form()
{
<form action="?" method="post" enctype="multipart/form-data">
<><form action="?" method="post" enctype="multipart/form-data">
<div>
<label>Some text:</label>
<input type="text" name="fühld" value="<?= context->post["fühld"] ?>"/>
@ -14,7 +14,7 @@ void show_form()
<div>
<input type="submit" value="Submit Form"/>
</div>
</form>
</form></>
}

View File

@ -1,6 +1,7 @@
Library
=================================
- sha1 / md5 / meow
- fold StringList into DTree _OR_ make better StringList
Bugs
@ -10,19 +11,27 @@ Bugs
Framework
=================================
- Locks for file i/o
- Locks for compiler
- Check: File Upload
- Proxy mode
- WebSockets
- Persistent code / Cron / Tick?
- Fork
- Multithreading
- Optionally store compiled units alongside source
- Resident code / Cron / Tick?
- Automated Test Suite
Performance
=================================
- Arena Allocator
- Array implementation
Nice to Have
=================================
- pseudorandom
- XML components
- Optionally store compiled units alongside source
Finally
=================================
- DO WE DO OUR OWN LANGUAGE OR DO WE KEEP USING C?!?