28
28
#include "papi.h"
29
29
#include "plibs.h"
30
30
#include "pnetlibs.h"
31
+ #include "pfolder.h"
31
32
32
33
#include <stdio.h>
33
34
typedef struct _email_vis_params {
@@ -672,4 +673,50 @@ void psync_update_cryptostatus(){
672
673
psync_sql_free_result (q );
673
674
674
675
676
+ }
677
+
678
+ static int check_write_permissions (psync_folderid_t folderid ) {
679
+ psync_sql_res * res ;
680
+ psync_uint_row row ;
681
+ int ret = 0 ;
682
+
683
+ res = psync_sql_query_nolock ("SELECT permissions, flags, name FROM folder WHERE id=?" );
684
+ psync_sql_bind_uint (res , 1 , folderid );
685
+ row = psync_sql_fetch_rowint (res );
686
+ if (unlikely (!row ))
687
+ debug (D_ERROR , "could not find folder of folderid %lu" , (unsigned long )folderid );
688
+ else if (((row [1 ])& 3 )!= O_RDONLY && !((row [0 ])& (PSYNC_PERM_MODIFY |PSYNC_PERM_CREATE )))
689
+ ret = 1 ;
690
+
691
+ psync_sql_free_result (res );
692
+ return ret ;
693
+ }
694
+
695
+ psync_folderid_t psync_check_and_create_folder (const char * path ) {
696
+ psync_folderid_t folderid = psync_get_folderid_by_path_or_create (path );
697
+ if (folderid == PSYNC_INVALID_FOLDERID || (!check_write_permissions (folderid ))){
698
+ char * buff ;
699
+ uint32_t bufflen ;
700
+ int ind = 1 ;
701
+ char * err ;
702
+
703
+ while (ind < 100 ) {
704
+ folderid = PSYNC_INVALID_FOLDERID ;
705
+ bufflen = strlen (path ) + 1 /*zero char*/ + 3 /*parenthesis*/ + 3 /*up to 3 digit index*/ ;
706
+ buff = (char * ) psync_malloc (bufflen );
707
+ snprintf (buff , bufflen - 1 , "%s (%d)" , path , ind );
708
+ if (psync_create_remote_folder_by_path (buff , & err )!= 0 ) {
709
+ folderid = psync_get_folderid_by_path_or_create (buff );
710
+ if ((folderid != PSYNC_INVALID_FOLDERID )&& check_write_permissions (folderid )) {
711
+ psync_free (buff );
712
+ break ;
713
+ }
714
+ } else
715
+ debug (D_NOTICE ,"Unable to create folder %s error is %s." , buff , err );
716
+ ++ ind ;
717
+ psync_free (buff );
718
+ }
719
+ }
720
+
721
+ return folderid ;
675
722
}
0 commit comments