1 /*----------------------------------------------------------------------*\
2 |* spkg - The Unofficial Slackware Linux Package Manager *|
3 |* designed by Ondøej Jirman, 2005 *|
4 |*----------------------------------------------------------------------*|
5 |* No copy/usage restrictions are imposed on anybody. *|
6 \*----------------------------------------------------------------------*/
21 //#include <pthread.h>
22 #include "win32/flock.h"
25 #define EWOULDBLOCK EAGAIN
28 #define S_ISLNK(m) (0)
31 #define lstat(x,y) stat(x,y)
33 int sigfillset(sigset_t
*set
) { *set
= ~(sigset_t
)0; return 0; }
35 int usleep(unsigned int usec
)
37 unsigned int dwMilliseconds
= usec
/1000;
38 if (dwMilliseconds
> 0)
40 Sleep (dwMilliseconds
);
47 #define e_set(e, n, fmt, args...) e_add(e, "sys", __func__, n, fmt, ##args)
49 sys_ftype
sys_file_type_stat(const gchar
* path
, gboolean deref
, struct stat
* s
)
62 if (S_ISREG(s
->st_mode
)) return SYS_REG
;
63 if (S_ISDIR(s
->st_mode
)) return SYS_DIR
;
64 if (S_ISLNK(s
->st_mode
)) return SYS_SYM
;
65 if (S_ISBLK(s
->st_mode
)) return SYS_BLK
;
66 if (S_ISCHR(s
->st_mode
)) return SYS_CHR
;
67 if (S_ISFIFO(s
->st_mode
)) return SYS_FIFO
;
68 if (S_ISSOCK(s
->st_mode
)) return SYS_SOCK
;
70 if (errno
== ENOENT
|| errno
== ENOTDIR
)
75 sys_ftype
sys_file_type(const gchar
* path
, gboolean deref
)
77 return sys_file_type_stat(path
, deref
, 0);
80 time_t sys_file_mtime(const gchar
* path
, gboolean deref
)
94 /* ripped off busybox and modified */
95 gint
sys_rm_rf(const gchar
* path
)
97 struct stat path_stat
;
99 /* if dir not exist return with error */
100 if (lstat(path
, &path_stat
) < 0)
104 if (S_ISDIR(path_stat
.st_mode
))
110 if ((dp
= opendir(path
)) == NULL
)
112 while ((d
= readdir(dp
)) != NULL
)
114 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
116 gchar
*new_path
= g_strdup_printf("%s/%s", path
, d
->d_name
);
117 if (sys_rm_rf(new_path
))
121 if (closedir(dp
) < 0)
129 if (unlink(path
) < 0)
135 gint
sys_mkdir_p(const gchar
* path
)
139 guint i
, j
, retval
= 1, pathv_len
;
140 gchar
* tmp
, *tmp_end
;
144 simple_path
= path_simplify(path
);
145 pathv
= path_get_elements(simple_path
);
146 pathv_len
= g_strv_length_compat(pathv
);
147 tmp
= tmp_end
= (gchar
*)g_malloc0(strlen(simple_path
)+10);
150 for (i
=0; i
<pathv_len
; i
++)
153 if (i
> 0) /* absolute path or not first element */
155 if (i
== 0 && **pathv
== 0)
157 for (j
=0; j
<strlen(pathv
[i
]); j
++)
158 *(tmp_end
++) = pathv
[i
][j
];
160 /* skip backreferences */
161 if (!strcmp(pathv
[i
], ".."))
165 sys_ftype type
= sys_file_type(tmp
, 1);
168 if (type
== SYS_NONE
)
171 if (mkdir(tmp
) == -1)
173 if (mkdir(tmp
, 0755) == -1)
189 void sys_sigblock(sigset_t
* sigs
)
195 rs
= sigprocmask(SIG_SETMASK
, &s
, sigs
);
198 printf("panic: can't block signals\n");
205 void sys_sigunblock(sigset_t
* sigs
)
209 rs
= sigprocmask(SIG_SETMASK
, sigs
, 0);
212 printf("panic: can't unblock signals\n");
218 gint
sys_lock_new(const gchar
* path
, struct error
* e
)
222 gint fd
= open(path
, O_CREAT
, 0644);
225 e_set(e
, E_FATAL
, "can't open lock file: %s", strerror(errno
));
231 gint
sys_lock_trywait(gint fd
, gint timeout
, struct error
* e
)
237 gint s
= flock(fd
, LOCK_NB
|LOCK_EX
);
238 if (s
== -1) /* we did not get lock */
240 if (errno
!= EWOULDBLOCK
) /* and will not get it */
242 e_set(e
, E_FATAL
, "flock failed: %s", strerror(errno
));
245 /* ok maybe we could get it a bit later */
248 e_set(e
, E_ERROR
, "timed out waiting for a lock");
251 usleep(100000); /* 100ms */
258 gint
sys_lock_put(gint fd
, struct error
* e
)
261 gint s
= flock(fd
, LOCK_NB
|LOCK_UN
);
262 if (s
== -1) /* error */
264 e_set(e
, E_FATAL
, "flock failed: %s", strerror(errno
));
270 void sys_lock_del(gint fd
)
275 gint
sys_write_buffer_to_file(const gchar
* file
, const gchar
* buf
, gsize len
, struct error
* e
)
281 FILE* f
= fopen(file
, "w");
284 e_set(e
, E_FATAL
, "can't open file for writing: %s", strerror(errno
));
289 if (1 != fwrite(buf
, len
, 1, f
))
291 e_set(e
, E_FATAL
, "can't write data to a file: %s", file
);
299 gint
sys_read_file_to_buffer(const gchar
* file
, gchar
* buf
, gsize len
, struct error
* e
)
301 g_assert(file
!= NULL
);
302 g_assert(buf
!= NULL
);
306 FILE* f
= fopen(file
, "r");
309 e_set(e
, E_FATAL
, "Can't open file for reading: %s (%s)", file
, strerror(errno
));
312 if (1 != fread(buf
, len
, 1, f
))
314 e_set(e
, E_FATAL
, "Can't read data from the file: %s (%s)", file
, strerror(errno
));