00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "autoscan.h"
00037 #include "storage.h"
00038 #include "content_manager.h"
00039
00040 using namespace zmm;
00041
00042 AutoscanDirectory::AutoscanDirectory()
00043 {
00044 taskCount = 0;
00045 objectID = INVALID_OBJECT_ID;
00046 storageID = INVALID_OBJECT_ID;
00047 last_mod_previous_scan = 0;
00048 last_mod_current_scan = 0;
00049 timer_parameter = Ref<Object> ((Object *)new ContentManager::TimerParameter(ContentManager::TimerParameter::IDAutoscan, INVALID_SCAN_ID));
00050 }
00051
00052 AutoscanDirectory::AutoscanDirectory(String location, scan_mode_t mode,
00053 scan_level_t level, bool recursive, bool persistent,
00054 int id, unsigned int interval, bool hidden)
00055 {
00056 this->location = location;
00057 this->mode = mode;
00058 this->level = level;
00059 this->recursive = recursive;
00060 this->hidden = hidden;
00061 this->interval = interval;
00062 this->persistent_flag = persistent;
00063 scanID = id;
00064 taskCount = 0;
00065 objectID = INVALID_OBJECT_ID;
00066 storageID = INVALID_OBJECT_ID;
00067 last_mod_previous_scan = 0;
00068 last_mod_current_scan = 0;
00069 timer_parameter = Ref<Object> ((Object *)new ContentManager::TimerParameter(ContentManager::TimerParameter::IDAutoscan, INVALID_SCAN_ID));
00070 }
00071
00072 void AutoscanDirectory::setCurrentLMT(time_t lmt)
00073 {
00074 if (lmt > last_mod_current_scan)
00075 last_mod_current_scan = lmt;
00076 }
00077
00078 AutoscanList::AutoscanList()
00079 {
00080 mutex = Ref<Mutex>(new Mutex(true));
00081 list = Ref<Array<AutoscanDirectory> > (new Array<AutoscanDirectory>());
00082 }
00083
00084 void AutoscanList::updateLMinDB()
00085 {
00086 AUTOLOCK(mutex);
00087 for (int i = 0; i < list->size(); i++)
00088 {
00089 log_debug("i: %d\n", i);
00090 Ref<AutoscanDirectory> ad = list->get(i);
00091 if (ad != nil)
00092 Storage::getInstance()->autoscanUpdateLM(ad);
00093 }
00094 }
00095
00096 int AutoscanList::add(Ref<AutoscanDirectory> dir)
00097 {
00098 AUTOLOCK(mutex);
00099 return _add(dir);
00100 }
00101
00102 int AutoscanList::_add(Ref<AutoscanDirectory> dir)
00103 {
00104
00105 String loc = dir->getLocation();
00106 int nil_index = -1;
00107
00108 for (int i = 0; i < list->size(); i++)
00109 {
00110 if (list->get(i) == nil)
00111 {
00112 nil_index = i;
00113 continue;
00114 }
00115
00116 if (loc == list->get(i)->getLocation())
00117 {
00118 throw _Exception(_("Attempted to add same autoscan path twice"));
00119 }
00120 }
00121
00122 if (nil_index != -1)
00123 {
00124 dir->setScanID(nil_index);
00125 list->set(dir, nil_index);
00126 }
00127 else
00128 {
00129 dir->setScanID(list->size());
00130 list->append(dir);
00131 }
00132
00133 return dir->getScanID();
00134 }
00135
00136 void AutoscanList::addList(zmm::Ref<AutoscanList> list)
00137 {
00138 AUTOLOCK(mutex);
00139
00140 for (int i = 0; i < list->list->size(); i++)
00141 {
00142 if (list->list->get(i) == nil)
00143 continue;
00144
00145 _add(list->list->get(i));
00146 }
00147 }
00148
00149 Ref<Array<AutoscanDirectory> > AutoscanList::getArrayCopy()
00150 {
00151 AUTOLOCK(mutex);
00152 Ref<Array<AutoscanDirectory> > copy(new Array<AutoscanDirectory>(list->size()));
00153 for (int i = 0; i < list->size(); i++)
00154 copy->append(list->get(i));
00155
00156 return copy;
00157 }
00158
00159 Ref<AutoscanDirectory> AutoscanList::get(int id)
00160 {
00161 AUTOLOCK(mutex);
00162
00163 if ((id < 0) || (id >= list->size()))
00164 return nil;
00165
00166 return list->get(id);
00167 }
00168
00169 Ref<AutoscanDirectory> AutoscanList::getByObjectID(int objectID)
00170 {
00171 AUTOLOCK(mutex);
00172
00173 for (int i = 0; i < list->size(); i++)
00174 {
00175 if (list->get(i) != nil && objectID == list->get(i)->getObjectID())
00176 return list->get(i);
00177 }
00178 return nil;
00179 }
00180
00181 Ref<AutoscanDirectory> AutoscanList::get(String location)
00182 {
00183 AUTOLOCK(mutex);
00184 for (int i = 0; i < list->size(); i++)
00185 {
00186 if (list->get(i) != nil && (location == list->get(i)->getLocation()))
00187 return list->get(i);
00188 }
00189 return nil;
00190
00191 }
00192
00193 void AutoscanList::remove(int id)
00194 {
00195 AUTOLOCK(mutex);
00196
00197 if ((id < 0) || (id >= list->size()))
00198 {
00199 log_debug("No such ID %d!\n", id);
00200 return;
00201 }
00202
00203 Ref<AutoscanDirectory> dir = list->get(id);
00204 dir->setScanID(INVALID_SCAN_ID);
00205
00206 if (id == list->size()-1)
00207 {
00208 list->removeUnordered(id);
00209 }
00210 else
00211 {
00212 list->set(nil, id);
00213 }
00214
00215 log_debug("ID %d removed!\n", id);
00216 }
00217
00218 int AutoscanList::removeByObjectID(int objectID)
00219 {
00220 AUTOLOCK(mutex);
00221
00222 for (int i = 0; i < list->size(); i++)
00223 {
00224 if (list->get(i) != nil && objectID == list->get(i)->getObjectID())
00225 {
00226 Ref<AutoscanDirectory> dir = list->get(i);
00227 dir->setScanID(INVALID_SCAN_ID);
00228 if (i == list->size()-1)
00229 {
00230 list->removeUnordered(i);
00231 }
00232 else
00233 {
00234 list->set(nil, i);
00235 }
00236 return i;
00237 }
00238 }
00239 return INVALID_SCAN_ID;
00240 }
00241
00242 int AutoscanList::remove(String location)
00243 {
00244 AUTOLOCK(mutex);
00245
00246 for (int i = 0; i < list->size(); i++)
00247 {
00248 if (list->get(i) != nil && location == list->get(i)->getLocation())
00249 {
00250 Ref<AutoscanDirectory> dir = list->get(i);
00251 dir->setScanID(INVALID_SCAN_ID);
00252 if (i == list->size()-1)
00253 {
00254 list->removeUnordered(i);
00255 }
00256 else
00257 {
00258 list->set(nil, i);
00259 }
00260 return i;
00261 }
00262 }
00263 return INVALID_SCAN_ID;
00264 }
00265
00266 Ref<AutoscanList> AutoscanList::removeIfSubdir(String parent, bool persistent)
00267 {
00268 AUTOLOCK(mutex);
00269
00270 Ref<AutoscanList> rm_id_list(new AutoscanList());
00271
00272 for (int i = 0; i < list->size(); i++)
00273 {
00274 if (list->get(i) != nil && (list->get(i)->getLocation().startsWith(parent)))
00275 {
00276 Ref<AutoscanDirectory> dir = list->get(i);
00277 if (dir == nil)
00278 continue;
00279 if (dir->persistent() && (persistent == false))
00280 {
00281 continue;
00282 }
00283 Ref<AutoscanDirectory> copy(new AutoscanDirectory());
00284 dir->copyTo(copy);
00285 rm_id_list->add(copy);
00286 copy->setScanID(dir->getScanID());
00287 dir->setScanID(INVALID_SCAN_ID);
00288 if (i == list->size()-1)
00289 {
00290 list->removeUnordered(i);
00291 }
00292 else
00293 {
00294 list->set(nil, i);
00295 }
00296 }
00297 }
00298
00299 return rm_id_list;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 void AutoscanList::notifyAll(Ref<TimerSubscriberSingleton<Object> > cm)
00320 {
00321 AUTOLOCK(mutex);
00322
00323 Ref<Timer> timer = Timer::getInstance();
00324 for (int i = 0; i < list->size(); i++)
00325 {
00326 if (list->get(i) == nil)
00327 continue;
00328 cm->timerNotify(list->get(i)->getTimerParameter());
00329 }
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 void AutoscanDirectory::setLocation(String location)
00365 {
00366 if (this->location == nil)
00367 this->location = location;
00368 else
00369 throw _Exception(_("UNALLOWED LOCATION CHANGE!"));
00370
00371 }
00372
00373 void AutoscanDirectory::setScanID(int id)
00374 {
00375 scanID = id;
00376 RefCast(timer_parameter, ContentManager::TimerParameter)->setID(id);
00377 }
00378
00379
00380 String AutoscanDirectory::mapScanmode(scan_mode_t scanmode)
00381 {
00382 String scanmode_str = nil;
00383 switch (scanmode)
00384 {
00385 case TimedScanMode: scanmode_str = _("timed"); break;
00386 case InotifyScanMode: scanmode_str = _("inotify"); break;
00387 }
00388 if (scanmode_str == nil)
00389 throw Exception(_("illegal scanmode given to mapScanmode(): ") + scanmode);
00390 return scanmode_str;
00391 }
00392
00393 scan_mode_t AutoscanDirectory::remapScanmode(String scanmode)
00394 {
00395 if (scanmode == "timed")
00396 return TimedScanMode;
00397 if (scanmode == "inotify")
00398 return InotifyScanMode;
00399 else
00400 throw _Exception(_("illegal scanmode (") + scanmode + ") given to remapScanmode()");
00401 }
00402
00403 String AutoscanDirectory::mapScanlevel(scan_level_t scanlevel)
00404 {
00405 String scanlevel_str = nil;
00406 switch (scanlevel)
00407 {
00408 case BasicScanLevel: scanlevel_str = _("basic"); break;
00409 case FullScanLevel: scanlevel_str = _("full"); break;
00410 }
00411 if (scanlevel_str == nil)
00412 throw Exception(_("illegal scanlevel given to mapScanlevel(): ") + scanlevel);
00413 return scanlevel_str;
00414 }
00415
00416 scan_level_t AutoscanDirectory::remapScanlevel(String scanlevel)
00417 {
00418 if (scanlevel == "basic")
00419 return BasicScanLevel;
00420 else if (scanlevel == "full")
00421 return FullScanLevel;
00422 else
00423 throw _Exception(_("illegal scanlevel (") + scanlevel + ") given to remapScanlevel()");
00424 }
00425
00426 void AutoscanDirectory::copyTo(Ref<AutoscanDirectory> copy)
00427 {
00428 copy->location = location;
00429 copy->mode = mode;
00430 copy->level = level;
00431 copy->recursive = recursive;
00432 copy->hidden = hidden;
00433 copy->persistent_flag = persistent_flag;
00434 copy->interval = interval;
00435 copy->taskCount = taskCount;
00436 copy->scanID = scanID;
00437 copy->objectID = objectID;
00438 copy->storageID = storageID;
00439 copy->last_mod_previous_scan = last_mod_previous_scan;
00440 copy->last_mod_current_scan = last_mod_current_scan;
00441 copy->timer_parameter = timer_parameter;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451 Ref<Object> AutoscanDirectory::getTimerParameter()
00452 {
00453 return timer_parameter;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478