30 if (!path || stat(path, &stt))
return -1;
46 if (!path || stat(path, &stt))
return 0;
54 ut.modtime = new_time;
56 int res = utime(path, &ut);
63 if (stat(path, &stt) == 0)
return stt.st_mode;
71 if (lstat(path, &stt) == 0)
return stt.st_mode;
78 if (!path)
return false;
80 return stat(path, &stt) == 0 && S_ISREG(stt.st_mode);
84 if (!path)
return false;
86 return lstat(path, &stt) == 0 && S_ISLNK(stt.st_mode);
90 if (!path)
return false;
92 return stat(path, &stt) == 0 && S_ISFIFO(stt.st_mode);
96 if (!fp)
return false;
98 return fstat(fileno(fp), &stt) == 0 && S_ISFIFO(stt.st_mode);
103 bool executable =
false;
105 if (stat(path, &stt) == 0) {
106 uid_t my_userid = geteuid();
107 if (stt.st_uid == my_userid) {
108 executable = !!(stt.st_mode&S_IXUSR);
111 gid_t my_groupid = getegid();
112 if (stt.st_gid == my_groupid) {
113 executable = !!(stt.st_mode&S_IXGRP);
116 executable = !!(stt.st_mode&S_IXOTH);
133 bool isprivate =
true;
135 if (stat(path, &stt) == 0) {
137 isprivate = (stt.st_mode & (S_IWGRP|S_IWOTH|S_IRGRP|S_IROTH)) == 0;
140 isprivate = (stt.st_mode & (S_IWGRP|S_IWOTH)) == 0;
149 bool writable =
false;
163 FILE *in = fopen(file_or_dir,
"r");
179 return path && stat(path, &stt) == 0 && S_ISDIR(stt.st_mode);
184 if (stat(path, &stt))
return -1;
196 if (unlink(path) != 0) {
197 if (errno == ENOENT) {
216 if (error && !*error) *error = unlink_error;
223 if (symlink(target, link)<0) {
246 int permissions_mask = S_IRWXU | S_IRWXG | S_IRWXO ;
250 if (stat(path, &sb) == -1) {
254 if (((
int)sb.st_mode & permissions_mask) == ((
int)mode & permissions_mask)) {
258 if (chmod(path, (
int)mode))
return GB_IO_error(
"changing mode of", path);
267 int len = readlink(path, buffer, 999);
268 if (len<0)
return NULp;
270 if (path[0] ==
'/')
return ARB_strdup(buffer);
273 pos = strrchr(path2,
'/');
292 if (rename(oldpath, newpath) != 0) {
305 int src = open(srcpath, O_RDONLY, 0);
306 if (src == -1) error =
GB_IO_error(
"reading", srcpath);
308 int dst = open(dstpath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
309 if (dst == -1) error =
GB_IO_error(
"writing", dstpath);
314 ssize_t got = read(src, buf, BUFSIZ);
315 if (got == -1) error =
GB_IO_error(
"reading", srcpath);
317 ssize_t wrote = write(dst, buf, got);
318 if (wrote == -1) error =
GB_IO_error(
"writing", dstpath);
335 error =
"file already exists";
345 error =
"file already exists";
360 void TEST_basic_file_checks() {
361 const char *someDir =
"general";
362 const char *someFile =
"general/text.input";
363 const char *noFile =
"general/nosuch.input";
371 const char *linkToFile =
"fileLink";
372 const char *linkToDir =
"dirLink";
373 const char *linkNowhere =
"brokenLink";
429 #define MODE_MASK 0xFFC0
430 #define TEST_EXPECT_MODE_EQUAL(got,expd) TEST_EXPECT_EQUAL((got)&MODE_MASK, (expd)&MODE_MASK)
432 void TEST_basic_file_ops() {
433 const char *smallerFile =
"general/mac.input";
434 const char *biggerFile =
"general/dos.input";
435 const char *f1 =
"general/tmp1.input";
436 const char *f2 =
"general/tmp2.input";
437 const char *noFile =
"general/nosuch.input";
477 long mode_wprot = mode & ~(S_IWUSR | S_IWGRP | S_IWOTH);
489 long mode_unread = mode & ~(S_IRUSR | S_IRGRP | S_IROTH);
long GB_size_of_FILE(FILE *in)
bool GB_is_executablefile(const char *path)
void GB_warning(const char *message)
GB_ERROR GB_safe_copy_file(const char *oldpath, const char *newpath)
long GB_mode_of_link(const char *path)
void GB_unlink_or_warn(const char *path, GB_ERROR *error)
GB_ERROR GB_copy_file(const char *srcpath, const char *dstpath)
long GB_mode_of_file(const char *path)
GB_ERROR GB_symlink(const char *target, const char *link)
GB_ERROR GB_IO_error(const char *action, const char *filename)
char * ARB_strdup(const char *str)
const char * GBS_global_string(const char *templat,...)
long GB_getuid_of_file(const char *path)
int GB_unlink(const char *path)
bool mode_is_user_writeable(long mode)
long GB_size_of_file(const char *path)
GB_ERROR GB_set_time_of_file(const char *path, unsigned long new_time)
char buffer[MESSAGE_BUFFERSIZE]
GB_ERROR GB_safe_rename_file(const char *oldpath, const char *newpath)
#define TEST_PUBLISH(testfunction)
GB_ERROR GB_export_error(const char *error)
GB_ERROR GB_await_error()
#define TEST_EXPECT(cond)
unsigned long GB_time_of_file(const char *path)
GB_ERROR GB_move_file(const char *oldpath, const char *newpath)
#define TEST_REJECT(cond)
static void error(const char *msg)
bool GB_is_fifo(const char *path)
GB_ERROR GB_set_mode_of_file(const char *path, long mode)
bool GB_is_writeablefile(const char *filename)
bool GB_is_directory(const char *path)
#define TEST_EXPECT_NO_ERROR(call)
bool GB_is_regularfile(const char *path)
bool GB_is_readablefile(const char *filename)
#define TEST_EXPECT_ERROR_CONTAINS(call, part)
#define TEST_EXPECT_DIFFERENT(expr, want)
bool GB_is_link(const char *path)
char * GB_follow_unix_link(const char *path)
static bool GB_is_readable(const char *file_or_dir)
#define TEST_EXPECT_EQUAL(expr, want)
char * GBS_global_string_copy(const char *templat,...)
bool GB_is_privatefile(const char *path, bool read_private)