Move lock creation to own function for cache

SVN revision: 56786
This commit is contained in:
Sebastian Dransfeld 2011-02-07 21:33:22 +00:00
parent 918f284b38
commit 886a338b94
2 changed files with 28 additions and 15 deletions

View File

@ -53,3 +53,4 @@
* Don't create cache dir several times * Don't create cache dir several times
* Do efreet_init before using efreet_*() functions * Do efreet_init before using efreet_*() functions
* Move lock creation to own function for desktop cache

View File

@ -188,6 +188,30 @@ cache_scan(const char *path, const char *base_id, int priority, int recurse, int
return 1; return 1;
} }
static int
cache_lock_file(void)
{
char file[PATH_MAX];
struct flock fl;
int lockfd;
snprintf(file, sizeof(file), "%s/efreet/desktop_data.lock", efreet_cache_home_get());
lockfd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (lockfd < 0) return -1;
memset(&fl, 0, sizeof(struct flock));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(lockfd, F_SETLK, &fl) < 0)
{
if (verbose) printf("LOCKED! You may want to delete %s if this persists\n", file);
close(lockfd);
return -1;
}
return lockfd;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -206,7 +230,6 @@ main(int argc, char **argv)
struct stat st; struct stat st;
int changed = 0; int changed = 0;
int i; int i;
struct flock fl;
char file[PATH_MAX] = { '\0' }; char file[PATH_MAX] = { '\0' };
char util_file[PATH_MAX] = { '\0' }; char util_file[PATH_MAX] = { '\0' };
@ -238,20 +261,9 @@ main(int argc, char **argv)
if (!ecore_file_mkpath(file)) goto efreet_error; if (!ecore_file_mkpath(file)) goto efreet_error;
/* lock process, so that we only run one copy of this program */ /* lock process, so that we only run one copy of this program */
snprintf(file, sizeof(file), "%s/efreet/desktop_data.lock", efreet_cache_home_get()); lockfd = cache_lock_file();
lockfd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); if (lockfd == -1) goto efreet_error;
if (lockfd < 0) goto efreet_error;
memset(&fl, 0, sizeof(struct flock));
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(lockfd, F_SETLK, &fl) < 0)
{
if (verbose)
{
printf("LOCKED! You may want to delete %s if this persists\n", file);
}
goto efreet_error;
}
edd = efreet_desktop_edd(); edd = efreet_desktop_edd();
if (!edd) goto edd_error; if (!edd) goto edd_error;