Skip to content

Commit

Permalink
filesize is support bigger than 2gb
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed Apr 5, 2024
1 parent 8c4ae0d commit dc49628
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 58 deletions.
11 changes: 4 additions & 7 deletions src/ccode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public extern void set_archive_type (string filt, string form);
//DOC: ### file.c
//DOC: `int filesize (string path):`
//DOC: calculate file size
public extern int filesize (string path);
public extern uint64 filesize (string path);
//DOC: `void sync ():`
//DOC: sync call
public extern void fs_sync ();
Expand All @@ -93,12 +93,9 @@ public extern bool isfile (string path);
//DOC: `bool issymlink (string path)`:
//DOC: check path is symlink
public extern bool issymlink (string path);
#if no_libmagic
#else
//DOC: `get_magic_mime_type (string path):`
//DOC: get mimetype string from libmagic
public extern string get_magic_mime_type (string path);
#endif
//DOC: `get_content_type (string path):`
//DOC: get mimetype string from gio
public extern string get_content_type (string path);
//DOC: ### signal.c
//DOC: `void block_sigint ():`
//DOC: block ctrl-c
Expand Down
27 changes: 11 additions & 16 deletions src/ccode/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>

#define FILE_OK 0
#define FILE_NOT_EXIST 1
Expand All @@ -38,7 +39,7 @@ void debug(char* msg);
#endif
char* str_add(char* s1, char* s2);

long filesize(char* path){
uint64_t filesize(char* path){
stat(path, &st);
return st.st_size;
}
Expand Down Expand Up @@ -287,21 +288,15 @@ int isfile(char* path){
return (!ls || !fs) && ! isdir (path);
}

#ifndef no_libmagic
#include <magic.h>
char* get_magic_mime_type(char* path){
const char *mime;
magic_t magic;

magic = magic_open(MAGIC_MIME_TYPE);
magic_load(magic, NULL);
mime = magic_file(magic, path);

magic_close(magic);
return strdup(mime);
#include <gio/gio.h>
char* get_content_type(char* path){
gboolean is_certain = FALSE;
char *content_type = g_content_type_guess (path, NULL, 0, &is_certain);
if (content_type != NULL) {
return content_type;
}
return "text/plain";
}
#endif
#endif

typedef ssize_t (*write_func_t)(int, const void *, size_t);

Expand Down Expand Up @@ -331,4 +326,4 @@ ssize_t write(int fd, const void *buf, size_t count) {
}
return bytes_written;
}

#endif
2 changes: 1 addition & 1 deletion src/data/repository.vala
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public string create_index_data (string fpath) {
a.add("index:\n");
string md5sum = "";
string sha256sum = "";
int size=0;
uint64 size=0;
string path = srealpath (fpath);
string index_name = get_value ("name");
if (index_name == "") {
Expand Down
2 changes: 1 addition & 1 deletion src/util/builder_target/ymp.vala
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void build_target_ymp_init() {
}
cd(curdir);
string hash = calculate_sha1sum(buildpath + "/output/data.tar.gz");
int size = filesize(buildpath + "/output/data.tar.gz");
uint64 size = filesize(buildpath + "/output/data.tar.gz");
string new_data = readfile(buildpath + "/output/metadata.yaml");
new_data += " archive-hash: " + hash + "\n";
new_data += " arch: " + getArch() + "\n";
Expand Down
10 changes: 5 additions & 5 deletions src/util/file.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

//DOC: `string readfile_byte (string path, long size):`
//DOC: read **n** byte from file
public string readfile_byte (string path, long n) {
public string readfile_byte (string path, uint64 n) {
debug (_ ("Read file bytes: %s").printf (path));
if (!isfile (path)) {
return "";
}
long size = filesize(path);
uint64 size = filesize(path);
FileStream stream = FileStream.open (path, "r");
if (stream == null) {
warning (_ ("Failed to read file: %s").printf (path));
Expand All @@ -19,7 +19,7 @@ public string readfile_byte (string path, long n) {
warning (_ ("File is empty: %s").printf (path));
return "";
}else if (size < n) {
warning (_ ("Read byte size is bigger than file size: %s (read: %lu size %lu)").printf (path, n, size));
warning (_ ("Read byte size is bigger than file size: %s (read: %lld size %lld)").printf (path, n, size));
return "";
}else if (n == 0) {
n = size;
Expand Down Expand Up @@ -238,7 +238,7 @@ public bool iself (string path) {
if (!isfile (path)) {
return false;
}
long size = filesize(path);
uint64 size = filesize(path);
if(size < 4){
return false;
}
Expand All @@ -258,7 +258,7 @@ public bool is64bit (string path) {
return false;
}

long size = filesize(path);
uint64 size = filesize(path);
if(size < 4){
return false;
}
Expand Down
34 changes: 6 additions & 28 deletions src/util/httpd.vala
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ async void process_request (SocketConnection conn) {
dos.put_string ("HTTP/1.1 403 Forbidden\n");
return;
}
long size = filesize (srealpath ("./" + path));
uint64 size = filesize (srealpath ("./" + path));

print_fn ("%s -- %s %s %s".printf (ip, date, srealpath ("./" + path), GLib.format_size ( (uint64)size)), true, true);

dos.put_string ("HTTP/1.1 200 OK\n");
dos.put_string ("Server: YMP httpd %s\n".printf (VERSION));
dos.put_string ("Content-Type: " + get_content_type ("./" + path) + "\n");
dos.put_string ("Content-Length: %s\n\n".printf (size.to_string ()));
debug ("Content-Length: %s\n\n".printf (size.to_string ()));
dos.put_string ("Content-Type: %s\n".printf(get_content_type ("./" + path)));
dos.put_string ("Content-Length: %s\n\n".printf (size.to_string()));
uint8[] buf = new uint8[BUFFER_LENGTH];
size_t read_size = 0;
size_t written = 0;
Expand All @@ -72,6 +71,7 @@ async void process_request (SocketConnection conn) {
}else if (isdir ("./" + path)) {
print_fn ("%s -- %s %s 0 bytes".printf (ip, date, srealpath ("./" + path)), true, true);
dos.put_string ("HTTP/1.1 200 OK\nContent-Type: text/html\n\n");
dos.put_string ("<!DOCTYPE html>");
dos.put_string ("<html> \n <head>");
dos.put_string ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" >\n");
dos.put_string ("<title> " + _ ("Index of /%s").printf (path) + " </title> \n");
Expand Down Expand Up @@ -101,8 +101,8 @@ async void process_request (SocketConnection conn) {
}
if (isfile ("./" + path + "/" + f)) {
string ff = f.replace (">", "&gt;").replace ("<", "&lt;");
long size = filesize ("./" + path + "/" + f);
dos.put_string ("&#x1F4C4; <a class=\"link\" href=\"/" + url_encode (path + f).replace ("%2F", "/") + "\">" + ff + " </a> (" + GLib.format_size ( (uint64)size) + ") <br> </li> \n");
uint64 size = filesize ("./" + path + "/" + f);
dos.put_string ("&#x1F4C4; <a class=\"link\" href=\"/" + url_encode (path + f).replace ("%2F", "/") + "\">" + ff + " </a> (" + GLib.format_size (size)+") <br> </li> \n");
dos.flush ();
}
}
Expand All @@ -118,28 +118,6 @@ async void process_request (SocketConnection conn) {
}
}

private string get_content_type (string path) {
#if no_libmagic
if (endswith (path.down (), ".html")) {
return "text/html";
}else if (endswith (path.down (), ".css")) {
return "text/css";
}else if (endswith (path.down (), ".js")) {
return "text/javascript";
}else if (endswith (path.down (), ".png")) {
return "image/png";
}else if (endswith (path.down (), ".jpeg") || endswith (path.down (), ".jpg")) {
return "image/jpeg";
}else if (endswith (path.down (), ".svg")) {
return "image/svg + xml";
}else {
return "text/plain";
}
#else
return get_magic_mime_type (path);
#endif
}

public int start_httpd () {
int port = 8000;
if (get_value ("port") != "") {
Expand Down

0 comments on commit dc49628

Please sign in to comment.