1414#include <sys/stat.h>
1515#include <unistd.h>
1616
17- int main () {
17+ void setup () {
1818 EM_ASM (
1919 FS .writeFile ('forbidden' , "" ); FS .chmod ('forbidden' , 0 o000 );
2020 FS .writeFile ('readable' , "" ); FS .chmod ('readable' , 0 o444 );
2121 FS .writeFile ('writeable' , "" ); FS .chmod ('writeable' , 0 o222 );
2222 FS .writeFile ('allaccess' , "" ); FS .chmod ('allaccess' , 0 o777 );
23- FS .writeFile ('fchmodtest' , "" );
2423 );
24+ }
2525
26- // Empty path checks #9136 fix
27- char * files [] = {"readable" , "writeable" ,
28- "allaccess" , "forbidden" , "nonexistent" , "" };
29- for (int i = 0 ; i < sizeof files / sizeof files [0 ]; i ++ ) {
30- printf ("F_OK('%s'): %s\n" , files [i ], access (files [i ], F_OK ) < 0 ? strerror (errno ) : "OK" );
31- printf ("R_OK('%s'): %s\n" , files [i ], access (files [i ], R_OK ) < 0 ? strerror (errno ) : "OK" );
32- printf ("X_OK('%s'): %s\n" , files [i ], access (files [i ], X_OK ) < 0 ? strerror (errno ) : "OK" );
33- printf ("W_OK('%s'): %s\n" , files [i ], access (files [i ], W_OK ) < 0 ? strerror (errno ) : "OK" );
34- printf ("\n" );
35- }
36-
37- EM_ASM ({FS .writeFile ('filetorename' , 'renametest' );});
26+ void test_rename () {
27+ EM_ASM ({FS .writeFile ('filetorename' , 'renametest' );});
3828
3929 int rename_ret = rename ("filetorename" , "renamedfile" );
4030 assert (rename_ret == 0 );
@@ -45,7 +35,10 @@ int main() {
4535 // Same againt with faccessat
4636 printf ("F_OK('%s'): %d\n" , "filetorename" , faccessat (AT_FDCWD , "filetorename" , F_OK , 0 ));
4737 printf ("F_OK('%s'): %d\n" , "renamedfile" , faccessat (AT_FDCWD , "renamedfile" , F_OK , 0 ));
38+ }
4839
40+ void test_fchmod () {
41+ EM_ASM ({FS .writeFile ('fchmodtest' , "" );});
4942 chmod ("fchmodtest" , S_IRUGO | S_IWUGO );
5043 struct stat fileStats ;
5144 stat ("fchmodtest" , & fileStats );
@@ -60,7 +53,9 @@ int main() {
6053 );
6154 stat ("fchmodtest" , & fileStats );
6255 assert ((fileStats .st_mode & 0777 ) == 0777 );
56+ }
6357
58+ void test_lchmod () {
6459#if !defined(NODEFS ) && !defined(NODERAWFS )
6560 // Node (and indeed linux) does not support lchmod
6661 // so skip this part of the test.
@@ -74,11 +69,14 @@ int main() {
7469 lstat ("symlinkfile" , & symlinkStats );
7570 assert ((symlinkStats .st_mode & 0777 ) == 0777 );
7671
72+ struct stat fileStats ;
7773 stat ("writeable" , & fileStats );
78- mode = fileStats .st_mode & 0777 ;
74+ int mode = fileStats .st_mode & 0777 ;
7975 assert (mode == S_IWUGO || mode == (S_IWUGO | S_IXUGO ));
8076#endif
77+ }
8178
79+ void test_chmod_errors () {
8280 EM_ASM (
8381 var ex ;
8482 try {
@@ -102,18 +100,26 @@ int main() {
102100 }
103101 assert (ex .name == = "ErrnoError" && ex .errno == = 44 /* ENOENT */ );
104102 );
103+ }
105104
105+ int main () {
106+ setup ();
106107
107- // Restore full permissions on all created files so that python test runner rmtree
108- // won't have problems on deleting the files. On Windows, calling shutil.rmtree()
109- // will fail if any of the files are read-only.
110- EM_ASM (
111- FS .chmod ('forbidden' , 0777 );
112- FS .chmod ('readable' , 0777 );
113- FS .chmod ('writeable' , 0777 );
114- FS .chmod ('allaccess' , 0777 );
115- FS .chmod ('fchmodtest' , 0777 );
116- );
108+ // Empty path checks #9136 fix
109+ char * files [] = {"readable" , "writeable" ,
110+ "allaccess" , "forbidden" , "nonexistent" , "" };
111+ for (int i = 0 ; i < sizeof files / sizeof files [0 ]; i ++ ) {
112+ printf ("F_OK('%s'): %s\n" , files [i ], access (files [i ], F_OK ) < 0 ? strerror (errno ) : "OK" );
113+ printf ("R_OK('%s'): %s\n" , files [i ], access (files [i ], R_OK ) < 0 ? strerror (errno ) : "OK" );
114+ printf ("X_OK('%s'): %s\n" , files [i ], access (files [i ], X_OK ) < 0 ? strerror (errno ) : "OK" );
115+ printf ("W_OK('%s'): %s\n" , files [i ], access (files [i ], W_OK ) < 0 ? strerror (errno ) : "OK" );
116+ printf ("\n" );
117+ }
118+
119+ test_rename ();
120+ test_fchmod ();
121+ test_lchmod ();
122+ test_chmod_errors ();
117123
118124 return 0 ;
119125}
0 commit comments