Zesstra | 19d657d | 2017-01-29 21:40:38 +0100 | [diff] [blame] | 1 | #include "/sys/files.h" |
| 2 | |
| 3 | public varargs string read_data(string file, int start, anzahl) |
| 4 | { |
| 5 | if (!stringp(file)) |
| 6 | raise_error("Bad arg 1 to read_data(): expected 'string'.\n"); |
| 7 | |
| 8 | // Wenn Pfad nicht absolut, ist das nen Fehler, daher wird das nicht |
| 9 | // korrigiert. |
| 10 | |
| 11 | // wenn kein /data/ vorn steht, wird es vorangestellt. |
| 12 | if (strstr(file,"/"LIBDATADIR"/") != 0) |
| 13 | { |
| 14 | file = "/"LIBDATADIR + file; |
| 15 | } |
| 16 | // read_file() nicht direkt rufen, sondern vorher als closure ans |
| 17 | // aufrufende Objekt binden. Sonst laeuft die Rechtepruefung in |
| 18 | // valid_write() im Master unter der Annahme, dass die simul_efun.c mit |
| 19 | // ihrer root id was will. |
| 20 | return funcall(bind_lambda(#'efun::read_file, previous_object()), |
| 21 | file, start, anzahl); |
| 22 | } |
| 23 | |
| 24 | public varargs int write_data(string file, string str, int flags) |
| 25 | { |
| 26 | if (!stringp(file)) |
| 27 | raise_error("Bad arg 1 to write_data(): expected 'string'.\n"); |
| 28 | if (!stringp(str)) |
| 29 | raise_error("Bad arg 2 to write_data(): expected 'string'.\n"); |
| 30 | |
| 31 | // Wenn Pfad nicht absolut, ist das nen Fehler, daher wird das nicht |
| 32 | // korrigiert. |
| 33 | |
| 34 | // wenn kein /data/ vorn steht, wird es vorangestellt. |
| 35 | if (strstr(file,"/"LIBDATADIR"/") != 0) |
| 36 | { |
| 37 | file = "/"LIBDATADIR + file; |
| 38 | } |
| 39 | // write_file() nicht direkt rufen, sondern vorher als closure ans |
| 40 | // aufrufende Objekt binden. Sonst laeuft die Rechtepruefung in |
| 41 | // valid_write() im Master unter der Annahme, dass die simul_efun.c mit |
| 42 | // ihrer root id was will. |
| 43 | return funcall(bind_lambda(#'efun::write_file, previous_object()), |
| 44 | file, str, flags); |
| 45 | } |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame^] | 46 | |
| 47 | // * Bei 50k Groesse Log-File rotieren |
| 48 | varargs int log_file(string file, string txt, int size_to_break) |
| 49 | { |
| 50 | mixed *st; |
| 51 | |
| 52 | file="/log/"+file; |
| 53 | file=implode((efun::explode(file,"/")-({".."})),"/"); |
| 54 | |
| 55 | if (!funcall(bind_lambda(#'efun::call_other,PO),"secure/master",//') |
| 56 | "valid_write",file,geteuid(PO),"log_file",PO)) |
| 57 | return 0; |
| 58 | if ( size_to_break >= 0 & ( |
| 59 | sizeof(st = get_dir(file,2) ) && st[0] >= (size_to_break|MAX_LOG_SIZE))) |
| 60 | catch(rename(file, file + ".old");publish); /* No panic if failure */ |
| 61 | |
| 62 | return(write_file(file,txt)); |
| 63 | } |
| 64 | |
| 65 | #if !__EFUN_DEFINED__(copy_file) |
| 66 | #define MAXLEN 50000 |
| 67 | public nomask int copy_file(string source, string dest) |
| 68 | { |
| 69 | |
| 70 | int ptr; |
| 71 | string bytes; |
| 72 | |
| 73 | set_this_object(previous_object()); |
| 74 | if (!sizeof(source)||!sizeof(dest)||source==dest||(file_size(source)==-1)|| |
| 75 | (!call_other(master(),"valid_read",source, |
| 76 | getuid(this_interactive()|| |
| 77 | previous_object()),"read_file",previous_object()))|| |
| 78 | (!call_other(master(),"valid_read",source, |
| 79 | getuid(this_interactive()|| |
| 80 | previous_object()),"write_file",previous_object()))) |
| 81 | return 1; |
| 82 | switch (file_size(dest)) |
| 83 | { |
| 84 | case -1: |
| 85 | break; |
| 86 | case -2: |
| 87 | if (dest[<1]!='/') dest+="/"; |
| 88 | dest+=efun::explode(source,"/")[<1]; |
| 89 | if (file_size(dest)==-2) return 1; |
| 90 | if (file_size(dest)!=-1) break; |
| 91 | default: |
| 92 | if (!rm(dest)) return 1; |
| 93 | break; |
| 94 | } |
| 95 | do |
| 96 | { |
| 97 | bytes = read_bytes(source, ptr, MAXLEN); ptr += MAXLEN; |
| 98 | if (!bytes) bytes=""; |
| 99 | write_file(dest, bytes); |
| 100 | } |
| 101 | while(sizeof(bytes) == MAXLEN); |
| 102 | return 0; |
| 103 | } |
| 104 | #endif //!__EFUN_DEFINED__(copy_file) |
| 105 | |
| 106 | |