00001
00017 #include <mysql/mysql.h>
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include <stdlib.h>
00021 #include "fsd_debug.h"
00022 #include "fsd_defines.h"
00023
00024
00026 #define UNLOCK_TABLES() \
00027 if (mysql_query(conn, "UNLOCK TABLES")) { \
00028 fprintf(stderr, "%s\n", mysql_error(conn)); \
00029 strcpy(return_text, "ER DB_ERROR"); \
00030 return EDB_ERROR; \
00031 }
00032
00034 #define FSD_MYSQL_QUERY(qtext, args...) \
00035 snprintf(query, sizeof(query), qtext, ## args); \
00036 if (mysql_query(conn, query)) { \
00037 fprintf(stderr, "%s\n", mysql_error(conn)); \
00038 strcpy(return_text, "ER DB_ERROR"); \
00039 return EDB_ERROR; \
00040 }
00041
00043 #define FSD_MYSQL_QUERY_SMPL(qtext, args...) \
00044 snprintf(query, sizeof(query), qtext, ## args); \
00045 if (mysql_query(conn, query)) { \
00046 fprintf(stderr, "%s\n", mysql_error(conn)); \
00047 return EDB_ERROR; \
00048 }
00049
00062 MYSQL *fsd_db_connect(char *server, char *user, char* password,
00063 char *database)
00064 {
00065
00066 MYSQL *conn;
00067 conn = mysql_init(NULL);
00068
00069
00070 if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
00071
00072 printf("%s\n", mysql_error(conn));
00073 return NULL;
00074 }
00075 return conn;
00076 }
00077
00078
00087 void fsd_db_close(MYSQL *conn) {
00088 mysql_close(conn);
00089 }
00090
00091
00103 int fsd_test(MYSQL *conn) {
00104 MYSQL_RES *res;
00105
00106
00107 if (mysql_query(conn, "SELECT `id` FROM `dirs` WHERE id='1'")) {
00108 PDEBUG("%s\n", mysql_error(conn));
00109 return EDB_ERROR;
00110 }
00111 else {
00112 res = mysql_use_result(conn);
00113 mysql_free_result(res);
00114 return OK;
00115 }
00116 }
00117
00118
00133 int fsd_dir_exists(MYSQL *conn, char *dir) {
00134 MYSQL_RES *res;
00135 MYSQL_ROW row;
00136 char query[MAX_QUERY_LENGTH + 1] = "";
00137
00138
00139 FSD_MYSQL_QUERY_SMPL("SELECT `id` FROM `dirs` WHERE id='%s'", dir);
00140
00141
00142 res = mysql_use_result(conn);
00143
00144
00145 if ((row = mysql_fetch_row(res)) == NULL) {
00146 mysql_free_result(res);
00147 return EDIR_NOT_EXISTS;
00148 }
00149
00150
00151 mysql_free_result(res);
00152 return OK;
00153 }
00154
00155
00170 int fsd_file_exists(MYSQL *conn, char *file) {
00171 MYSQL_RES *res;
00172 MYSQL_ROW row;
00173 char query[MAX_QUERY_LENGTH + 1] = "";
00174
00175
00176 FSD_MYSQL_QUERY_SMPL("SELECT `id` FROM `files` WHERE id='%s'", file);
00177
00178
00179 res = mysql_use_result(conn);
00180
00181
00182 if ((row = mysql_fetch_row(res)) == NULL) {
00183 mysql_free_result(res);
00184 return EFILE_NOT_EXISTS;
00185 }
00186
00187
00188 mysql_free_result(res);
00189 return OK;
00190 }
00191
00192
00209 int fsd_dir_name_exists(MYSQL *conn, char *dir, char *dirname) {
00210 MYSQL_RES *res;
00211 MYSQL_ROW row;
00212 char query[MAX_QUERY_LENGTH + 1] = "";
00213 char dirname2[MAX_NAME_LENGTH * 2 + 1] = "";
00214 int dir_id;
00215
00216
00217 mysql_real_escape_string(conn, dirname2, dirname, strlen(dirname));
00218
00219
00220 FSD_MYSQL_QUERY_SMPL("SELECT `id` FROM `dirs` WHERE parent_dir_id='%s' AND name='%s'", dir, dirname2);
00221
00222
00223 res = mysql_use_result(conn);
00224
00225
00226 if ((row = mysql_fetch_row(res)) == NULL) {
00227 mysql_free_result(res);
00228 return EDIR_NAME_NOT_EXISTS;
00229 }
00230
00231
00232 dir_id = atoi(row[0]);
00233 mysql_free_result(res);
00234 return dir_id;
00235 }
00236
00237
00254 int fsd_file_name_exists(MYSQL *conn, char *dir, char *filename) {
00255 MYSQL_RES *res;
00256 MYSQL_ROW row;
00257 char query[MAX_QUERY_LENGTH + 1] = "";
00258 char filename2[MAX_NAME_LENGTH * 2 + 1] = "";
00259 int file_id;
00260
00261
00262 mysql_real_escape_string(conn, filename2, filename, strlen(filename));
00263
00264
00265 FSD_MYSQL_QUERY_SMPL("SELECT `id` FROM `files` WHERE parent_dir_id='%s' AND name='%s'", dir, filename2);
00266
00267
00268 res = mysql_use_result(conn);
00269
00270
00271 if ((row = mysql_fetch_row(res)) == NULL) {
00272 mysql_free_result(res);
00273 return EFILE_NAME_NOT_EXISTS;
00274 }
00275
00276
00277 file_id = atoi(row[0]);
00278 mysql_free_result(res);
00279 return file_id;
00280 }
00281
00282
00299 int fsd_create(MYSQL *conn, char *dir, char *filename, char *return_text) {
00300 MYSQL_RES *res = NULL;
00301 MYSQL_ROW row;
00302 char query[MAX_QUERY_LENGTH + 1] = "";
00303 char filename2[MAX_NAME_LENGTH * 2 + 1] = "";
00304
00305
00306 mysql_real_escape_string(conn, filename2, filename, strlen(filename));
00307
00308
00309 FSD_MYSQL_QUERY("LOCK TABLES `dirs` READ, `files` WRITE")
00310
00311
00312 int tst_dir_exists = fsd_dir_exists(conn, dir);
00313 if (tst_dir_exists == EDB_ERROR) {
00314 strcpy(return_text, "ER DB_ERROR");
00315 return EDB_ERROR;
00316 }
00317 if (tst_dir_exists == EDIR_NOT_EXISTS) {
00318
00319 UNLOCK_TABLES();
00320
00321 strcpy(return_text, "ER DIR_NOT_EXISTS");
00322 return EDIR_NOT_EXISTS;
00323 }
00324
00325
00326 int tst_file_name_exists = fsd_file_name_exists(conn, dir, filename);
00327 if (tst_file_name_exists == EDB_ERROR) {
00328 strcpy(return_text, "ER DB_ERROR");
00329 return EDB_ERROR;
00330 }
00331 if (tst_file_name_exists != EFILE_NAME_NOT_EXISTS) {
00332
00333 UNLOCK_TABLES()
00334
00335 strcpy(return_text, "ER NAME_EXISTS");
00336 return ENAME_EXISTS;
00337 }
00338
00339
00340 int tst_dir_name_exists = fsd_dir_name_exists(conn, dir, filename);
00341 if (tst_dir_name_exists == EDB_ERROR) {
00342 strcpy(return_text, "ER DB_ERROR");
00343 return EDB_ERROR;
00344 }
00345 if (tst_dir_name_exists != EDIR_NAME_NOT_EXISTS) {
00346
00347 UNLOCK_TABLES()
00348
00349 strcpy(return_text, "ER NAME_EXISTS");
00350 return ENAME_EXISTS;
00351 }
00352
00353
00354
00355
00356 FSD_MYSQL_QUERY("INSERT INTO `files` (`parent_dir_id`, `name`) VALUES('%s', '%s')", dir, filename2)
00357
00358
00359 FSD_MYSQL_QUERY("SELECT LAST_INSERT_ID()")
00360
00361
00362 res = mysql_use_result(conn);
00363
00364
00365 if ((row = mysql_fetch_row(res)) == NULL) {
00366 strcpy(return_text, "ER DB_ERROR");
00367 return EDB_ERROR;
00368 }
00369
00370
00371 strcpy(return_text, "OK ");
00372 if (row[0]) {
00373 strcat(return_text, row[0]);
00374 }
00375
00376
00377 else {
00378 strcpy(return_text, "ER DB_ERROR");
00379 return EDB_ERROR;
00380 }
00381 mysql_free_result(res);
00382
00383 UNLOCK_TABLES()
00384
00385 return OK;
00386 }
00387
00388
00404 int fsd_mkdir(MYSQL *conn, char *dir, char *dirname, char *return_text) {
00405 MYSQL_RES *res = NULL;
00406 MYSQL_ROW row;
00407 char query[MAX_QUERY_LENGTH + 1] = "";
00408 char dirname2[MAX_NAME_LENGTH * 2 + 1] = "";
00409
00410 mysql_real_escape_string(conn, dirname2, dirname, strlen(dirname));
00411
00412
00413 FSD_MYSQL_QUERY("LOCK TABLES `dirs` WRITE, `files` READ")
00414
00415
00416 int tst_dir_exists = fsd_dir_exists(conn, dir);
00417 if (tst_dir_exists == EDB_ERROR) {
00418 strcpy(return_text, "ER DB_ERROR");
00419 return EDB_ERROR;
00420 }
00421 if (tst_dir_exists == EDIR_NOT_EXISTS) {
00422
00423 UNLOCK_TABLES()
00424
00425 strcpy(return_text, "ER DIR_NOT_EXISTS");
00426 return EDIR_NOT_EXISTS;
00427 }
00428
00429
00430 int tst_file_name_exists = fsd_file_name_exists(conn, dir, dirname);
00431 if (tst_file_name_exists == EDB_ERROR) {
00432 strcpy(return_text, "ER DB_ERROR");
00433 return EDB_ERROR;
00434 }
00435 if (tst_file_name_exists != EFILE_NAME_NOT_EXISTS) {
00436
00437 UNLOCK_TABLES()
00438
00439 strcpy(return_text, "ER NAME_EXISTS");
00440 return ENAME_EXISTS;
00441 }
00442
00443
00444 int tst_dir_name_exists = fsd_dir_name_exists(conn, dir, dirname);
00445 if (tst_dir_name_exists == EDB_ERROR) {
00446 strcpy(return_text, "ER DB_ERROR");
00447 return EDB_ERROR;
00448 }
00449 if (tst_dir_name_exists != EDIR_NAME_NOT_EXISTS) {
00450
00451 UNLOCK_TABLES()
00452
00453 strcpy(return_text, "ER NAME_EXISTS");
00454 return ENAME_EXISTS;
00455 }
00456
00457
00458
00459
00460 FSD_MYSQL_QUERY("INSERT INTO `dirs` (`parent_dir_id`, `name`) VALUES('%s', '%s')", dir, dirname2)
00461
00462
00463 FSD_MYSQL_QUERY("SELECT LAST_INSERT_ID()")
00464
00465
00466 res = mysql_use_result(conn);
00467
00468
00469 if ((row = mysql_fetch_row(res)) == NULL) {
00470 strcpy(return_text, "ER DB_ERROR");
00471 return EDB_ERROR;
00472 }
00473
00474
00475 strcpy(return_text, "OK ");
00476 if (row[0]) {
00477 strcat(return_text, row[0]);
00478 }
00479
00480
00481 else {
00482 strcpy(return_text, "ER DB_ERROR");
00483 return EDB_ERROR;
00484 }
00485 mysql_free_result(res);
00486
00487 UNLOCK_TABLES()
00488
00489 return OK;
00490 }
00491
00492
00507 int fsd_clear_file(MYSQL *conn, char *file, char *return_text) {
00508 char query[MAX_QUERY_LENGTH + 1] = "";
00509
00510
00511 FSD_MYSQL_QUERY("LOCK TABLES `data` WRITE, `files` READ")
00512
00513
00514 int tst_file_exists = fsd_file_exists(conn, file);
00515 if (tst_file_exists == EDB_ERROR) {
00516 strcpy(return_text, "ER DB_ERROR");
00517 return EDB_ERROR;
00518 }
00519 if (tst_file_exists == EFILE_NOT_EXISTS) {
00520
00521 UNLOCK_TABLES()
00522
00523 strcpy(return_text, "ER FILE_NOT_EXISTS");
00524 return EFILE_NOT_EXISTS;
00525 }
00526
00527
00528
00529
00530 FSD_MYSQL_QUERY("DELETE FROM `data` WHERE `file_id` = '%s'", file)
00531
00532 strcpy(return_text, "OK");
00533
00534 UNLOCK_TABLES()
00535
00536 return OK;
00537 }
00538
00539
00556 int fsd_add_block(MYSQL *conn, char *file, int block_size, char *block, char *return_text) {
00557
00558 char query[MAX_BLOCK_LENGTH * 2 + 1 + MAX_QUERY_LENGTH + 1] = "";
00559 int query_len;
00560 char *query_pointer;
00561
00562 char block2[MAX_BLOCK_LENGTH * 2 + 1] = "";
00563
00564
00565 block_size = mysql_real_escape_string(conn, block2, block, block_size);
00566
00567
00568 FSD_MYSQL_QUERY("LOCK TABLES `data` WRITE, `files` READ")
00569
00570
00571 int tst_file_exists = fsd_file_exists(conn, file);
00572 if (tst_file_exists == EDB_ERROR) {
00573 strcpy(return_text, "ER DB_ERROR");
00574 return EDB_ERROR;
00575 }
00576 if (tst_file_exists == EFILE_NOT_EXISTS) {
00577
00578 UNLOCK_TABLES()
00579
00580 strcpy(return_text, "ER FILE_NOT_EXISTS");
00581 return EFILE_NOT_EXISTS;
00582 }
00583
00584
00585
00586
00587 strcpy(query, "INSERT INTO `data` VALUES (NULL, '");
00588 strcat(query, file);
00589 strcat(query, "', '");
00590 query_pointer = query + strlen(query);
00591 query_len = strlen(query);
00592 memcpy(query_pointer, block2, block_size);
00593 query_len += block_size;
00594 query_pointer = query + query_len;
00595 memcpy(query_pointer, "')", 2);
00596 query_len += 2;
00597
00598
00599
00600 if (mysql_real_query(conn, query, query_len)) {
00601 fprintf(stderr, "%s\n", mysql_error(conn));
00602 strcpy(return_text, "ER DB_ERROR");
00603 return EDB_ERROR;
00604 }
00605
00606
00607 strcpy(return_text, "OK");
00608
00609 UNLOCK_TABLES()
00610
00611 return OK;
00612 }
00613
00614
00629 int fsd_remove_file(MYSQL *conn, char *file, char *return_text) {
00630 char query[MAX_QUERY_LENGTH + 1] = "";
00631
00632
00633 FSD_MYSQL_QUERY("LOCK TABLES `data` WRITE, `files` WRITE")
00634
00635
00636 int tst_file_exists = fsd_file_exists(conn, file);
00637 if (tst_file_exists == EDB_ERROR) {
00638 strcpy(return_text, "ER DB_ERROR");
00639 return EDB_ERROR;
00640 }
00641 if (tst_file_exists == EFILE_NOT_EXISTS) {
00642
00643 UNLOCK_TABLES()
00644
00645 strcpy(return_text, "ER FILE_NOT_EXISTS");
00646 return EFILE_NOT_EXISTS;
00647 }
00648
00649
00650
00651
00652 FSD_MYSQL_QUERY("DELETE FROM `data` WHERE `file_id` = '%s'", file)
00653
00654
00655 FSD_MYSQL_QUERY("DELETE FROM `files` WHERE `id` = '%s'", file)
00656
00657
00658 strcpy(return_text, "OK");
00659
00660 UNLOCK_TABLES()
00661
00662 return OK;
00663 }
00664
00665
00681 int fsd_remove_dir(MYSQL *conn, char *dir, char *return_text) {
00682 MYSQL_RES *res;
00683 MYSQL_ROW row;
00684 char query[MAX_QUERY_LENGTH + 1] = "";
00685
00686
00687 FSD_MYSQL_QUERY("LOCK TABLES `dirs` WRITE, `files` READ")
00688
00689
00690 int tst_dir_exists = fsd_dir_exists(conn, dir);
00691 if (tst_dir_exists == EDB_ERROR) {
00692 strcpy(return_text, "ER DB_ERROR");
00693 return EDB_ERROR;
00694 }
00695 if (tst_dir_exists == EDIR_NOT_EXISTS) {
00696
00697 UNLOCK_TABLES()
00698
00699 strcpy(return_text, "ER DIR_NOT_EXISTS");
00700 return EDIR_NOT_EXISTS;
00701 }
00702
00703
00704
00705
00706 FSD_MYSQL_QUERY("SELECT `id` FROM `files` WHERE parent_dir_id='%s'", dir)
00707
00708
00709 res = mysql_use_result(conn);
00710
00711
00712 if ((row = mysql_fetch_row(res)) != NULL) {
00713 mysql_free_result(res);
00714 strcpy(return_text, "ER DIR_IS_NOT_EMPTY");
00715 return EDIR_IS_NOT_EMPTY;
00716 }
00717
00718 mysql_free_result(res);
00719
00720
00721
00722
00723 FSD_MYSQL_QUERY("SELECT `id` FROM `dirs` WHERE parent_dir_id='%s'", dir)
00724
00725
00726 res = mysql_use_result(conn);
00727
00728
00729 if ((row = mysql_fetch_row(res)) != NULL) {
00730 mysql_free_result(res);
00731 strcpy(return_text, "ER DIR_IS_NOT_EMPTY");
00732 return EDIR_IS_NOT_EMPTY;
00733 }
00734
00735 mysql_free_result(res);
00736
00737
00738
00739
00740
00741 FSD_MYSQL_QUERY("DELETE FROM `dirs` WHERE `id` = '%s'", dir)
00742
00743
00744 strcpy(return_text, "OK");
00745
00746 UNLOCK_TABLES()
00747
00748 return OK;
00749 }
00750
00751
00769 int fsd_get_block(MYSQL *conn, char *file, char *offset, int *block_size, char *block, char *return_text) {
00770 MYSQL_RES *res;
00771 MYSQL_ROW row;
00772 char query[MAX_QUERY_LENGTH + 1] = "";
00773
00774
00775 FSD_MYSQL_QUERY("LOCK TABLES `data` READ, `files` READ")
00776
00777
00778 int tst_file_exists = fsd_file_exists(conn, file);
00779 if (tst_file_exists == EDB_ERROR) {
00780 strcpy(return_text, "ER DB_ERROR");
00781 return EDB_ERROR;
00782 }
00783 if (tst_file_exists == EFILE_NOT_EXISTS) {
00784
00785 UNLOCK_TABLES()
00786
00787 strcpy(return_text, "ER FILE_NOT_EXISTS");
00788 return EFILE_NOT_EXISTS;
00789 }
00790
00791
00792
00793
00794 FSD_MYSQL_QUERY("SELECT data, length(data) FROM `data` WHERE `file_id`='%s' ORDER BY `id` LIMIT %s, 1", file, offset)
00795
00796
00797 res = mysql_use_result(conn);
00798
00799
00800 if ((row = mysql_fetch_row(res)) == NULL) {
00801 mysql_free_result(res);
00802 strcpy(return_text, "ER BLOCK_NOT_EXISTS");
00803 return EBLOCK_NOT_EXISTS;
00804 }
00805
00806
00807 *block_size = atoi(row[1]);
00808 memcpy(block, row[0], *block_size);
00809
00810
00811 strcpy(return_text, "OK X ");
00812
00813
00814 mysql_free_result(res);
00815
00816 UNLOCK_TABLES()
00817
00818 return OK;
00819 }
00820
00821
00840 int fsd_lookup(MYSQL *conn, char *dir, char *name, char *return_text) {
00841 MYSQL_RES *res;
00842 MYSQL_ROW row;
00843 char query[MAX_QUERY_LENGTH + 1] = "";
00844 char name_id_char[12];
00845
00846
00847 FSD_MYSQL_QUERY("LOCK TABLES `dirs` READ, `files` READ, `data` READ")
00848
00849
00850 int tst_dir_exists = fsd_dir_exists(conn, dir);
00851 if (tst_dir_exists == EDB_ERROR) {
00852 strcpy(return_text, "ER DB_ERROR");
00853 return EDB_ERROR;
00854 }
00855 if (tst_dir_exists == EDIR_NOT_EXISTS) {
00856
00857 UNLOCK_TABLES()
00858
00859 strcpy(return_text, "ER DIR_NOT_EXISTS");
00860 return EDIR_NOT_EXISTS;
00861 }
00862
00863
00864 int tst_file_name_exists = fsd_file_name_exists(conn, dir, name);
00865 if (tst_file_name_exists == EDB_ERROR) {
00866 strcpy(return_text, "ER DB_ERROR");
00867 return EDB_ERROR;
00868 }
00869 if (tst_file_name_exists != EFILE_NAME_NOT_EXISTS) {
00870
00871 strcpy(return_text, "OK IS_FILE ");
00872 snprintf(name_id_char, sizeof(name_id_char), "%i", tst_file_name_exists);
00873 strcat(return_text, name_id_char);
00874
00875
00876
00877
00878 FSD_MYSQL_QUERY("SELECT SUM(LENGTH(data)) FROM `data` where `file_id`='%s'", name_id_char)
00879
00880
00881 res = mysql_use_result(conn);
00882
00883
00884 if ((row = mysql_fetch_row(res)) == NULL) {
00885 mysql_free_result(res);
00886
00887 UNLOCK_TABLES()
00888
00889 strcat(return_text, "/0");
00890 return OK;
00891 }
00892
00893
00894 if (!row[0]) {
00895 mysql_free_result(res);
00896
00897 UNLOCK_TABLES()
00898
00899 strcat(return_text, "/0");
00900 return OK;
00901 }
00902
00903 strcat(return_text, "/");
00904 strcat(return_text, row[0]);
00905 mysql_free_result(res);
00906
00907 UNLOCK_TABLES()
00908
00909 return IS_FILE;
00910 }
00911
00912
00913 int tst_dir_name_exists = fsd_dir_name_exists(conn, dir, name);
00914 if (tst_dir_name_exists == EDB_ERROR) {
00915 strcpy(return_text, "ER DB_ERROR");
00916 return EDB_ERROR;
00917 }
00918 if (tst_dir_name_exists != EDIR_NAME_NOT_EXISTS) {
00919
00920 UNLOCK_TABLES()
00921
00922 strcpy(return_text, "OK IS_DIR ");
00923 snprintf(name_id_char, sizeof(name_id_char), "%i", tst_dir_name_exists);
00924 strcat(return_text, name_id_char);
00925
00926
00927
00928
00929 FSD_MYSQL_QUERY("SELECT COUNT(*) FROM `dirs` where parent_dir_id='%s'", name_id_char)
00930
00931
00932 res = mysql_use_result(conn);
00933
00934
00935 if ((row = mysql_fetch_row(res)) == NULL) {
00936 mysql_free_result(res);
00937
00938 UNLOCK_TABLES()
00939
00940 strcpy(return_text, "/0");
00941 return OK;
00942 }
00943
00944 strcat(return_text, "/");
00945 strcat(return_text, row[0]);
00946 mysql_free_result(res);
00947
00948
00949
00950
00951 FSD_MYSQL_QUERY("SELECT COUNT(*) FROM `files` where parent_dir_id='%s'", name_id_char)
00952
00953
00954 res = mysql_use_result(conn);
00955
00956
00957 if ((row = mysql_fetch_row(res)) == NULL) {
00958 mysql_free_result(res);
00959
00960 UNLOCK_TABLES()
00961
00962 strcpy(return_text, "/0");
00963 return OK;
00964 }
00965
00966 strcat(return_text, "/");
00967 strcat(return_text, row[0]);
00968 mysql_free_result(res);
00969
00970 UNLOCK_TABLES()
00971
00972 return IS_DIR;
00973 }
00974
00975
00976 strcpy(return_text, "ER NOT_EXISTS");
00977
00978 UNLOCK_TABLES()
00979
00980 return ENAME_NOT_EXISTS;
00981 }
00982
00983
01000 int fsd_read_inode(MYSQL *conn, char *inode_nr, char *return_text) {
01001 MYSQL_RES *res;
01002 MYSQL_ROW row;
01003 char query[MAX_QUERY_LENGTH + 1] = "";
01004
01005
01006 FSD_MYSQL_QUERY("LOCK TABLES `dirs` READ, `files` READ, `data` READ")
01007
01008
01009 int tst_dir_exists = fsd_dir_exists(conn, inode_nr);
01010 if (tst_dir_exists == EDB_ERROR) {
01011 strcpy(return_text, "ER DB_ERROR");
01012 return EDB_ERROR;
01013 }
01014 if (tst_dir_exists == OK) {
01015
01016 UNLOCK_TABLES()
01017
01018 strcpy(return_text, "OK IS_DIR ");
01019
01020
01021
01022
01023 FSD_MYSQL_QUERY("SELECT COUNT(*) FROM `dirs` where parent_dir_id='%s'", inode_nr)
01024
01025
01026 res = mysql_use_result(conn);
01027
01028
01029 if ((row = mysql_fetch_row(res)) == NULL) {
01030 mysql_free_result(res);
01031
01032 UNLOCK_TABLES()
01033
01034 strcpy(return_text, "0");
01035 return OK;
01036 }
01037
01038 strcat(return_text, row[0]);
01039 mysql_free_result(res);
01040
01041
01042
01043
01044 FSD_MYSQL_QUERY("SELECT COUNT(*) FROM `files` where parent_dir_id='%s'", inode_nr)
01045
01046
01047 res = mysql_use_result(conn);
01048
01049
01050 if ((row = mysql_fetch_row(res)) == NULL) {
01051 mysql_free_result(res);
01052
01053 UNLOCK_TABLES()
01054
01055 strcpy(return_text, "/0");
01056 return OK;
01057 }
01058
01059 strcat(return_text, "/");
01060 strcat(return_text, row[0]);
01061 mysql_free_result(res);
01062
01063 UNLOCK_TABLES()
01064
01065 return IS_DIR;
01066 }
01067
01068
01069 int tst_file_exists = fsd_file_exists(conn, inode_nr);
01070 if (tst_file_exists == EDB_ERROR) {
01071 strcpy(return_text, "ER DB_ERROR");
01072 return EDB_ERROR;
01073 }
01074 if (tst_file_exists == OK) {
01075
01076 UNLOCK_TABLES()
01077
01078 strcpy(return_text, "OK IS_FILE ");
01079
01080
01081
01082
01083 FSD_MYSQL_QUERY("SELECT SUM(LENGTH(data)) FROM `data` where `file_id`='%s'", inode_nr)
01084
01085
01086 res = mysql_use_result(conn);
01087
01088
01089 if ((row = mysql_fetch_row(res)) == NULL) {
01090 mysql_free_result(res);
01091
01092 UNLOCK_TABLES()
01093
01094 strcat(return_text, "0");
01095 return OK;
01096 }
01097
01098
01099 if (!row[0]) {
01100 mysql_free_result(res);
01101
01102 UNLOCK_TABLES()
01103
01104 strcat(return_text, "0");
01105 return OK;
01106 }
01107
01108 strcat(return_text, row[0]);
01109 mysql_free_result(res);
01110
01111 UNLOCK_TABLES()
01112
01113 return IS_FILE;
01114 }
01115
01116
01117 strcpy(return_text, "ER NOT_EXISTS");
01118
01119 UNLOCK_TABLES()
01120
01121 return EINODE_NOT_EXISTS;
01122 }
01123
01124
01142 int fsd_read_dir(MYSQL *conn, char *dir, rd_item **rd_list, char *return_text) {
01143 MYSQL_RES *res;
01144 MYSQL_ROW row;
01145 char query[MAX_QUERY_LENGTH + 1] = "";
01146 int item_count = 0;
01147 char item_count_ch[18];
01148
01149
01150 rd_item *curr = NULL;
01151 rd_item *prev = NULL;
01152
01153
01154 FSD_MYSQL_QUERY("LOCK TABLES `dirs` READ, `files` READ")
01155
01156
01157 int tst_dir_exists = fsd_dir_exists(conn, dir);
01158 if (tst_dir_exists == EDB_ERROR) {
01159 strcpy(return_text, "ER DB_ERROR");
01160 return EDB_ERROR;
01161 }
01162 if (tst_dir_exists == EDIR_NOT_EXISTS) {
01163
01164 UNLOCK_TABLES()
01165
01166 strcpy(return_text, "ER DIR_NOT_EXISTS");
01167 return EDIR_NOT_EXISTS;
01168 }
01169
01170
01171 FSD_MYSQL_QUERY("SELECT id, name FROM `dirs` WHERE `parent_dir_id`='%s' ORDER BY `name`", dir)
01172
01173
01174 res = mysql_use_result(conn);
01175 (*rd_list) = NULL;
01176
01177
01178 while ((row = mysql_fetch_row(res)) != NULL) {
01179 item_count++;
01180 curr = (rd_item *) malloc(sizeof(rd_item));
01181 strcpy(curr->id, row[0]);
01182 strcpy(curr->name, row[1]);
01183 strcpy(curr->type, "d");
01184 curr->next = NULL;
01185 if (!(*rd_list)) {
01186 (*rd_list) = curr;
01187 }
01188 if (prev) {
01189 prev->next = curr;
01190 }
01191 prev = curr;
01192 }
01193 mysql_free_result(res);
01194
01195
01196 FSD_MYSQL_QUERY("SELECT id, name FROM `files` WHERE `parent_dir_id`='%s' ORDER BY `name`", dir)
01197
01198
01199 res = mysql_use_result(conn);
01200
01201
01202 while ((row = mysql_fetch_row(res)) != NULL) {
01203 item_count++;
01204 curr = (rd_item *) malloc(sizeof(rd_item));
01205 strcpy(curr->id, row[0]);
01206 strcpy(curr->name, row[1]);
01207 strcpy(curr->type, "f");
01208 curr->next = NULL;
01209 if (!(*rd_list)) {
01210 *rd_list = curr;
01211 }
01212 if (prev) {
01213 prev->next = curr;
01214 }
01215 prev = curr;
01216 }
01217
01218 mysql_free_result(res);
01219
01220
01221 strcpy(return_text, "OK ");
01222 snprintf(item_count_ch, sizeof(item_count_ch), "%i", item_count);
01223 strcat(return_text, item_count_ch);
01224 strcat(return_text, " ");
01225
01226 UNLOCK_TABLES()
01227
01228 return OK;
01229 }
01230
01231
01244 int fsd_file_info(MYSQL *conn, char *file, char *return_text) {
01245 MYSQL_RES *res;
01246 MYSQL_ROW row;
01247 char query[MAX_QUERY_LENGTH + 1] = "";
01248
01249
01250 FSD_MYSQL_QUERY("LOCK TABLES `files` READ, `data` READ")
01251
01252
01253 int tst_file_exists = fsd_file_exists(conn, file);
01254 if (tst_file_exists == EDB_ERROR) {
01255 strcpy(return_text, "ER DB_ERROR");
01256 return EDB_ERROR;
01257 }
01258 if (tst_file_exists == EFILE_NOT_EXISTS) {
01259
01260 UNLOCK_TABLES()
01261
01262 strcpy(return_text, "ER FILE_NOT_EXISTS");
01263 return EFILE_NOT_EXISTS;
01264 }
01265
01266
01267 FSD_MYSQL_QUERY("SELECT SUM(LENGTH(data)) FROM `data` where `file_id`='%s'", file)
01268
01269
01270 res = mysql_use_result(conn);
01271
01272
01273 if ((row = mysql_fetch_row(res)) == NULL) {
01274 mysql_free_result(res);
01275
01276 UNLOCK_TABLES()
01277
01278 strcpy(return_text, "OK 0");
01279 return OK;
01280 }
01281
01282
01283 if (!row[0]) {
01284 mysql_free_result(res);
01285
01286 UNLOCK_TABLES()
01287
01288 strcpy(return_text, "OK 0");
01289 return OK;
01290 }
01291
01292
01293 strcpy(return_text, "OK ");
01294 strcat(return_text, row[0]);
01295 mysql_free_result(res);
01296
01297 UNLOCK_TABLES()
01298
01299 return OK;
01300 }
01301
01302
01316 int fsd_dir_info(MYSQL *conn, char *dir, char *return_text) {
01317 MYSQL_RES *res;
01318 MYSQL_ROW row;
01319 char query[MAX_QUERY_LENGTH + 1] = "";
01320
01321
01322 FSD_MYSQL_QUERY("LOCK TABLES `dirs` READ, `files` READ")
01323
01324
01325 int tst_dir_exists = fsd_dir_exists(conn, dir);
01326 if (tst_dir_exists == EDB_ERROR) {
01327 strcpy(return_text, "ER DB_ERROR");
01328 return EDB_ERROR;
01329 }
01330 if (tst_dir_exists == EDIR_NOT_EXISTS) {
01331
01332 UNLOCK_TABLES()
01333
01334 strcpy(return_text, "ER DIR_NOT_EXISTS");
01335 return EDIR_NOT_EXISTS;
01336 }
01337
01338
01339
01340
01341 FSD_MYSQL_QUERY("SELECT COUNT(*) FROM `dirs` where parent_dir_id='%s'", dir)
01342
01343
01344 res = mysql_use_result(conn);
01345
01346
01347 if ((row = mysql_fetch_row(res)) == NULL) {
01348 mysql_free_result(res);
01349
01350 UNLOCK_TABLES()
01351
01352 strcpy(return_text, "OK 0");
01353 return OK;
01354 }
01355
01356 strcpy(return_text, "OK ");
01357 strcat(return_text, row[0]);
01358 mysql_free_result(res);
01359
01360
01361
01362
01363 FSD_MYSQL_QUERY("SELECT COUNT(*) FROM `files` where parent_dir_id='%s'", dir)
01364
01365
01366 res = mysql_use_result(conn);
01367
01368
01369 if ((row = mysql_fetch_row(res)) == NULL) {
01370 mysql_free_result(res);
01371
01372 UNLOCK_TABLES()
01373
01374 strcpy(return_text, "/0");
01375 return OK;
01376 }
01377
01378 strcat(return_text, "/");
01379 strcat(return_text, row[0]);
01380 mysql_free_result(res);
01381
01382 UNLOCK_TABLES()
01383
01384 return OK;
01385 }
01386
01387
01404 int fsd_replace_block(MYSQL *conn, char *file, char *offset, int block_size,
01405 char *block, char *return_text) {
01406 MYSQL_RES *res;
01407 MYSQL_ROW row;
01408
01409 char query[MAX_BLOCK_LENGTH * 2 + MAX_QUERY_LENGTH + 1] = "";
01410 int query_len;
01411 char *query_pointer;
01412 char block_id[MAX_ID_LENGTH + 1];
01413
01414 char block2[MAX_BLOCK_LENGTH * 2 + 1] = "";
01415
01416
01417 block_size = mysql_real_escape_string(conn, block2, block + strlen(offset) + 1, block_size);
01418
01419
01420 FSD_MYSQL_QUERY("LOCK TABLES `data` WRITE, `files` READ")
01421
01422
01423 int tst_file_exists = fsd_file_exists(conn, file);
01424 if (tst_file_exists == EDB_ERROR) {
01425 strcpy(return_text, "ER DB_ERROR");
01426 return EDB_ERROR;
01427 }
01428 if (tst_file_exists == EFILE_NOT_EXISTS) {
01429
01430 UNLOCK_TABLES()
01431
01432 strcpy(return_text, "ER FILE_NOT_EXISTS");
01433 return EFILE_NOT_EXISTS;
01434 }
01435
01436
01437 FSD_MYSQL_QUERY("SELECT id FROM `data` WHERE `file_id`='%s' ORDER BY `id` LIMIT %s, 1", file, offset)
01438
01439
01440 res = mysql_use_result(conn);
01441
01442
01443 if ((row = mysql_fetch_row(res)) == NULL) {
01444 mysql_free_result(res);
01445 strcpy(return_text, "BLOCK_NOT_EXISTS");
01446
01447 UNLOCK_TABLES()
01448
01449 return EBLOCK_NOT_EXISTS;
01450 }
01451
01452 strcpy(block_id, row[0]);
01453 mysql_free_result(res);
01454
01455
01456
01457
01458 strcpy(query, "UPDATE `data` SET data='");
01459 query_len = strlen("UPDATE `data` SET data='");
01460 query_pointer = query + query_len;
01461 memcpy(query_pointer, block2, block_size);
01462 query_len += block_size;
01463 query_pointer = query + query_len;
01464 strcpy(query_pointer, "' WHERE id='");
01465 query_len += strlen("' WHERE id='");
01466 query_pointer = query + query_len;
01467 strcpy(query_pointer, block_id);
01468 query_len += strlen(block_id);
01469 query_pointer = query + query_len;
01470 strcpy(query_pointer, "'");
01471 query_len += strlen("'");
01472 query_pointer = query + query_len;
01473 *query_pointer = '\0';
01474
01475
01476 if (mysql_real_query(conn, query, query_len)) {
01477 fprintf(stderr, "%s\n", mysql_error(conn));
01478 strcpy(return_text, "ER DB_ERROR");
01479 return EDB_ERROR;
01480 }
01481
01482
01483 strcpy(return_text, "OK");
01484
01485 UNLOCK_TABLES()
01486
01487 return OK;
01488 }
01489
01490
01504 int fsd_move_file(MYSQL *conn, char *file, char *dir, char *return_text) {
01505 char query[MAX_QUERY_LENGTH + 1] = "";
01506
01507
01508 FSD_MYSQL_QUERY("LOCK TABLES `files` WRITE, `dirs` WRITE")
01509
01510
01511 int tst_dir_exists = fsd_dir_exists(conn, dir);
01512 if (tst_dir_exists == EDB_ERROR) {
01513 strcpy(return_text, "ER DB_ERROR");
01514 return EDB_ERROR;
01515 }
01516 if (tst_dir_exists == EDIR_NOT_EXISTS) {
01517
01518 UNLOCK_TABLES()
01519
01520 strcpy(return_text, "ER PARENT_NOT_EXISTS");
01521 return EPARENT_NOT_EXISTS;
01522 }
01523
01524
01525 int tst_file_exists = fsd_file_exists(conn, file);
01526 if (tst_file_exists == EDB_ERROR) {
01527 strcpy(return_text, "ER DB_ERROR");
01528 return EDB_ERROR;
01529 }
01530
01531
01532 if (tst_file_exists != EFILE_NOT_EXISTS) {
01533
01534 FSD_MYSQL_QUERY("UPDATE `files` SET `parent_dir_id`= '%s' WHERE `id` = '%s'", dir, file)
01535
01536 strcpy(return_text, "OK");
01537 UNLOCK_TABLES()
01538 return OK;
01539
01540 }
01541
01542
01543 tst_dir_exists = fsd_dir_exists(conn, file);
01544 if (tst_dir_exists == EDB_ERROR) {
01545 strcpy(return_text, "ER DB_ERROR");
01546 return EDB_ERROR;
01547 }
01548
01549
01550 if (tst_dir_exists != EDIR_NOT_EXISTS) {
01551 FSD_MYSQL_QUERY("UPDATE `dirs` SET `parent_dir_id`= '%s' WHERE `id` = '%s'", dir, file)
01552
01553 strcpy(return_text, "OK");
01554 UNLOCK_TABLES()
01555 return OK;
01556
01557 }
01558
01559 UNLOCK_TABLES()
01560
01561 strcpy(return_text, "ER FILE_NOT_EXISTS");
01562 return EFILE_NOT_EXISTS;
01563
01564 }
01565
01566
01581 int fsd_rename_file(MYSQL *conn, char *file, char *name, char *return_text) {
01582 char query[MAX_QUERY_LENGTH + 1] = "";
01583 char parent_dir_id[MAX_ID_LENGTH + 1] = "";
01584 char name2[MAX_NAME_LENGTH + 1] = "";
01585 MYSQL_RES *res;
01586 MYSQL_ROW row;
01587
01588
01589 mysql_real_escape_string(conn, name2, name, strlen(name));
01590
01591
01592 FSD_MYSQL_QUERY("LOCK TABLES `files` WRITE, `dirs` WRITE")
01593
01594
01595 int tst_file_exists = fsd_file_exists(conn, file);
01596 if (tst_file_exists == EDB_ERROR) {
01597 strcpy(return_text, "ER DB_ERROR");
01598 return EDB_ERROR;
01599 }
01600 if (tst_file_exists == EFILE_NOT_EXISTS) {
01601
01602 UNLOCK_TABLES()
01603
01604 strcpy(return_text, "ER FILE_NOT_EXISTS");
01605 return EFILE_NOT_EXISTS;
01606 }
01607
01608
01609 FSD_MYSQL_QUERY("SELECT `parent_dir_id` FROM `files` where `id`='%s'", file)
01610
01611
01612 res = mysql_use_result(conn);
01613
01614
01615 if ((row = mysql_fetch_row(res)) == NULL) {
01616 mysql_free_result(res);
01617
01618 UNLOCK_TABLES()
01619
01620 strcpy(return_text, "ER FILE_NOT_EXISTS");
01621 return EFILE_NOT_EXISTS;
01622 }
01623
01624 strcat(parent_dir_id, row[0]);
01625 mysql_free_result(res);
01626
01627
01628 int tst_file_name_exists = fsd_file_name_exists(conn, parent_dir_id, name);
01629 if (tst_file_name_exists == EDB_ERROR) {
01630 strcpy(return_text, "ER DB_ERROR");
01631 return EDB_ERROR;
01632 }
01633 if (tst_file_name_exists != EFILE_NAME_NOT_EXISTS) {
01634
01635 UNLOCK_TABLES()
01636
01637 strcpy(return_text, "ER NAME_EXISTS");
01638 return ENAME_EXISTS;
01639 }
01640
01641
01642 int tst_dir_name_exists = fsd_dir_name_exists(conn, parent_dir_id, name);
01643 if (tst_dir_name_exists == EDB_ERROR) {
01644 strcpy(return_text, "ER DB_ERROR");
01645 return EDB_ERROR;
01646 }
01647 if (tst_dir_name_exists != EDIR_NAME_NOT_EXISTS) {
01648
01649 UNLOCK_TABLES()
01650
01651 strcpy(return_text, "ER NAME_EXISTS");
01652 return ENAME_EXISTS;
01653 }
01654
01655
01656
01657
01658 FSD_MYSQL_QUERY("UPDATE `files` SET `name`= '%s' WHERE `id` = '%s'", name, file)
01659
01660 strcpy(return_text, "OK");
01661
01662 UNLOCK_TABLES()
01663
01664 return OK;
01665 }
01666
01681 int fsd_rename_dir(MYSQL *conn, char *dir, char *name, char *return_text) {
01682 char query[MAX_QUERY_LENGTH + 1] = "";
01683 char parent_dir_id[MAX_NAME_LENGTH + 1] = "";
01684 char name2[MAX_NAME_LENGTH + 1] = "";
01685 MYSQL_RES *res;
01686 MYSQL_ROW row;
01687
01688
01689 mysql_real_escape_string(conn, name2, name, strlen(name));
01690
01691
01692 FSD_MYSQL_QUERY("LOCK TABLES `files` WRITE, `dirs` WRITE")
01693
01694
01695 int tst_dir_exists = fsd_dir_exists(conn, dir);
01696 if (tst_dir_exists == EDB_ERROR) {
01697 strcpy(return_text, "ER DB_ERROR");
01698 return EDB_ERROR;
01699 }
01700 if (tst_dir_exists == EFILE_NOT_EXISTS) {
01701
01702 UNLOCK_TABLES()
01703
01704 strcpy(return_text, "ER FILE_NOT_EXISTS");
01705 return EFILE_NOT_EXISTS;
01706 }
01707
01708
01709 FSD_MYSQL_QUERY("SELECT `parent_dir_id` FROM `dirs` where `id`='%s'", dir)
01710
01711
01712 res = mysql_use_result(conn);
01713
01714
01715 if ((row = mysql_fetch_row(res)) == NULL) {
01716 mysql_free_result(res);
01717
01718 UNLOCK_TABLES()
01719
01720 strcpy(return_text, "ER FILE_NOT_EXISTS");
01721 return EFILE_NOT_EXISTS;
01722 }
01723
01724 strcat(parent_dir_id, row[0]);
01725 mysql_free_result(res);
01726
01727
01728 int tst_file_name_exists = fsd_file_name_exists(conn, parent_dir_id, name);
01729 if (tst_file_name_exists == EDB_ERROR) {
01730 strcpy(return_text, "ER DB_ERROR");
01731 return EDB_ERROR;
01732 }
01733 if (tst_file_name_exists != EFILE_NAME_NOT_EXISTS) {
01734
01735 UNLOCK_TABLES()
01736
01737 strcpy(return_text, "ER NAME_EXISTS");
01738 return ENAME_EXISTS;
01739 }
01740
01741
01742 int tst_dir_name_exists = fsd_dir_name_exists(conn, parent_dir_id, name);
01743 if (tst_dir_name_exists == EDB_ERROR) {
01744 strcpy(return_text, "ER DB_ERROR");
01745 return EDB_ERROR;
01746 }
01747 if (tst_dir_name_exists != EDIR_NAME_NOT_EXISTS) {
01748
01749 UNLOCK_TABLES()
01750
01751 strcpy(return_text, "ER NAME_EXISTS");
01752 return ENAME_EXISTS;
01753 }
01754
01755
01756
01757
01758 FSD_MYSQL_QUERY("UPDATE `dirs` SET `name`= '%s' WHERE `id` = '%s'", name, dir)
01759
01760 strcpy(return_text, "OK");
01761
01762 UNLOCK_TABLES()
01763
01764 return OK;
01765 }
01766
01780 int fsd_truncate_file(MYSQL *conn, char *file, char *new_file_size, char *return_text) {
01781 MYSQL_RES *res;
01782 MYSQL_ROW row;
01783
01784 char query[MAX_BLOCK_LENGTH * 2 + 1 + MAX_QUERY_LENGTH + 1] = "";
01785
01786 char block2[MAX_BLOCK_LENGTH * 2 + 1] = "";
01787 int query_len;
01788 char *query_pointer;
01789 char block_id[MAX_ID_LENGTH + 1] = "";
01790 long new_file_size_long = atol(new_file_size);
01791 int fillchar = 0;
01792 long file_size;
01793 int block_number, end_block_number, count;
01794
01795
01796 FSD_MYSQL_QUERY("LOCK TABLES `files` READ, `data` WRITE")
01797
01798
01799 int tst_file_exists = fsd_file_exists(conn, file);
01800 if (tst_file_exists == EDB_ERROR) {
01801 strcpy(return_text, "ER DB_ERROR");
01802 return EDB_ERROR;
01803 }
01804 if (tst_file_exists == EFILE_NOT_EXISTS) {
01805
01806 UNLOCK_TABLES()
01807
01808 strcpy(return_text, "ER FILE_NOT_EXISTS");
01809 return EFILE_NOT_EXISTS;
01810 }
01811
01812
01813 FSD_MYSQL_QUERY("SELECT SUM(LENGTH(data)) FROM `data` where `file_id`='%s'", file)
01814 res = mysql_use_result(conn);
01815
01816 if ((row = mysql_fetch_row(res)) == NULL) {
01817
01818 mysql_free_result(res);
01819
01820 UNLOCK_TABLES()
01821
01822 strcpy(return_text, "OK");
01823 return OK;
01824 }
01825
01826
01827 if (row[0] == NULL) {
01828 file_size = 0;
01829 }
01830 else {
01831 file_size = atol(row[0]);
01832 }
01833
01834 mysql_free_result(res);
01835
01836
01837 if (new_file_size_long <= file_size) {
01838
01839 block_number = new_file_size_long / MAX_BLOCK_LENGTH;
01840 count = new_file_size_long - block_number * MAX_BLOCK_LENGTH;
01841
01842 FSD_MYSQL_QUERY("SELECT data, id FROM `data` WHERE `file_id`='%s' ORDER BY `id` LIMIT %d, 1", file, block_number)
01843
01844
01845 res = mysql_use_result(conn);
01846 if ((row = mysql_fetch_row(res)) == NULL) {
01847 mysql_free_result(res);
01848
01849 UNLOCK_TABLES()
01850
01851 strcpy(return_text, "OK");
01852 return OK;
01853 }
01854 strcpy(block_id, row[1]);
01855
01856 count = mysql_real_escape_string(conn, block2, row[0], count);
01857
01858
01859 strcpy(query, "UPDATE `data` SET data='");
01860 query_len = strlen("UPDATE `data` SET data='");
01861 query_pointer = query + query_len;
01862 memcpy(query_pointer, block2, count);
01863 query_len += count;
01864 query_pointer = query + query_len;
01865 strcpy(query_pointer, "' WHERE id='");
01866 query_len += strlen("' WHERE id='");
01867 query_pointer = query + query_len;
01868 strcpy(query_pointer, block_id);
01869 query_len += strlen(block_id);
01870 query_pointer = query + query_len;
01871 strcpy(query_pointer, "'");
01872 query_len += strlen("'");
01873 query_pointer = query + query_len;
01874 *query_pointer = '\0';
01875
01876 mysql_free_result(res);
01877 if (mysql_real_query(conn, query, query_len)) {
01878 fprintf(stderr, "%s\n", mysql_error(conn));
01879 strcpy(return_text, "ER DB_ERROR");
01880 return EDB_ERROR;
01881 }
01882
01883
01884
01885 while (1) {
01886
01887
01888 FSD_MYSQL_QUERY("SELECT `id` FROM `data` where `file_id`='%s' LIMIT %i, 1", file, block_number+1)
01889
01890 res = mysql_use_result(conn);
01891
01892
01893 if ((row = mysql_fetch_row(res)) == NULL) {
01894 mysql_free_result(res);
01895
01896 UNLOCK_TABLES()
01897
01898 strcpy(return_text, "OK");
01899 return OK;
01900 }
01901
01902 strcpy(block_id, row[0]);
01903 mysql_free_result(res);
01904
01905
01906
01907
01908
01909 FSD_MYSQL_QUERY("DELETE FROM `data` WHERE `id` = '%s'", block_id)
01910
01911 }
01912 }
01913
01914
01915 else {
01916
01917 block_number = file_size / MAX_BLOCK_LENGTH;
01918 count = file_size - block_number * MAX_BLOCK_LENGTH;
01919
01920 if (file_size > 0) {
01921 FSD_MYSQL_QUERY("SELECT data, id FROM `data` WHERE `file_id`='%s' ORDER BY `id` LIMIT %d, 1", file, block_number)
01922
01923
01924 res = mysql_use_result(conn);
01925 if ((row = mysql_fetch_row(res)) == NULL) {
01926 mysql_free_result(res);
01927
01928 UNLOCK_TABLES()
01929
01930 strcpy(return_text, "OK");
01931 return OK;
01932 }
01933 strcpy(block_id, row[1]);
01934
01935 memcpy(query, row[0], count);
01936 memset((char*)query + count, fillchar, MAX_BLOCK_LENGTH - count);
01937
01938 count = mysql_real_escape_string(conn, block2, query, MAX_BLOCK_LENGTH);
01939
01940
01941 strcpy(query, "UPDATE `data` SET data='");
01942 query_len = strlen("UPDATE `data` SET data='");
01943 query_pointer = query + query_len;
01944 memcpy(query_pointer, block2, count);
01945 query_len += count;
01946 query_pointer = query + query_len;
01947 strcpy(query_pointer, "' WHERE id='");
01948 query_len += strlen("' WHERE id='");
01949 query_pointer = query + query_len;
01950 strcpy(query_pointer, block_id);
01951 query_len += strlen(block_id);
01952 query_pointer = query + query_len;
01953 strcpy(query_pointer, "'");
01954 query_len += strlen("'");
01955 query_pointer = query + query_len;
01956 *query_pointer = '\0';
01957
01958
01959 mysql_free_result(res);
01960 if (mysql_real_query(conn, query, query_len)) {
01961 fprintf(stderr, "%s\n", mysql_error(conn));
01962 strcpy(return_text, "ER DB_ERROR");
01963 return EDB_ERROR;
01964 }
01965
01966 }
01967
01968
01969 memset((char*)query, fillchar, MAX_BLOCK_LENGTH);
01970 count = mysql_real_escape_string(conn, block2, query, MAX_BLOCK_LENGTH);
01971
01972
01973 strcpy(query, "INSERT INTO `data` VALUES (NULL, '");
01974 strcat(query, file);
01975 strcat(query, "', '");
01976 query_pointer = query + strlen(query);
01977 query_len = strlen(query);
01978 memcpy(query_pointer, block2, count);
01979 query_len += count;
01980 query_pointer = query + query_len;
01981 memcpy(query_pointer, "')", strlen("')"));
01982 query_len += strlen("')");
01983
01984 end_block_number = new_file_size_long / MAX_BLOCK_LENGTH;
01985 count = new_file_size_long - end_block_number * MAX_BLOCK_LENGTH;
01986
01987 while (block_number++ < end_block_number) {
01988 if (mysql_real_query(conn, query, query_len)) {
01989 fprintf(stderr, "%s\n", mysql_error(conn));
01990 strcpy(return_text, "ER DB_ERROR");
01991 return EDB_ERROR;
01992 }
01993 }
01994
01995 memset((char*)query, fillchar, count);
01996 count = mysql_real_escape_string(conn, block2, query, count);
01997
01998
01999 strcpy(query, "INSERT INTO `data` VALUES (NULL, '");
02000 strcat(query, file);
02001 strcat(query, "', '");
02002 query_pointer = query + strlen(query);
02003 query_len = strlen(query);
02004 memcpy(query_pointer, block2, count);
02005 query_len += count;
02006 query_pointer = query + query_len;
02007 memcpy(query_pointer, "')", strlen("')"));
02008 query_len += strlen("')");
02009 query_pointer = query + query_len;
02010 *query_pointer = '\0';
02011 if (mysql_real_query(conn, query, query_len)) {
02012 fprintf(stderr, "%s\n", mysql_error(conn));
02013 strcpy(return_text, "ER DB_ERROR");
02014 return EDB_ERROR;
02015 }
02016
02017 }
02018
02019 UNLOCK_TABLES()
02020
02021 strcpy(return_text, "OK");
02022 return OK;
02023 }
02024
02025