Skip to content

Commit

Permalink
fix: wrong symlink count at startup. corrected post symlink handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dreulavelle committed Jul 25, 2024
1 parent ade0147 commit cbe9012
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/controllers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ async def get_stats(_: Request):
payload = {}
with db.Session() as session:

movies_symlinks = session.execute(select(func.count(Movie._id)).where(Movie.symlinked is True)).scalar_one()
episodes_symlinks = session.execute(select(func.count(Episode._id)).where(Episode.symlinked is True)).scalar_one()
movies_symlinks = session.execute(select(func.count(Movie._id)).where(Movie.symlinked == True)).scalar_one()
episodes_symlinks = session.execute(select(func.count(Episode._id)).where(Episode.symlinked == True)).scalar_one()
total_symlinks = movies_symlinks + episodes_symlinks

total_movies = session.execute(select(func.count(Movie._id))).scalar_one()
Expand Down
2 changes: 2 additions & 0 deletions src/program/db/db_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ def run_delete(_type):
run_delete(Movie)
run_delete(MediaItem)

logger.log("PROGRAM", "Database reset. Turning off HARD_RESET Env Var.")
os.environ["HARD_RESET"] = "False"
4 changes: 2 additions & 2 deletions src/program/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ def start(self):
logger.debug(f"Mapped metadata to {item.type.title()}: {item.log_string}")
session.commit()

movies_symlinks = session.execute(select(func.count(Movie._id)).where(Movie.symlinked is True)).scalar_one()
episodes_symlinks = session.execute(select(func.count(Episode._id)).where(Episode.symlinked is True)).scalar_one()
movies_symlinks = session.execute(select(func.count(Movie._id)).where(Movie.symlinked == True)).scalar_one() # noqa
episodes_symlinks = session.execute(select(func.count(Episode._id)).where(Episode.symlinked == True)).scalar_one() # noqa
total_symlinks = movies_symlinks + episodes_symlinks
total_movies = session.execute(select(func.count(Movie._id))).scalar_one()
total_shows = session.execute(select(func.count(Show._id))).scalar_one()
Expand Down
20 changes: 12 additions & 8 deletions src/program/symlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,10 @@ def _symlink(self, item: Union[Movie, Episode]) -> bool:
if os.path.islink(destination):
os.remove(destination)
os.symlink(source, destination)
item.set("symlinked", True)
item.set("symlinked_at", datetime.now())
item.set("symlinked_times", item.symlinked_times + 1)
except PermissionError as e:
# This still creates the symlinks, however they will have wrong perms. User needs to fix their permissions.
# TODO: Maybe we validate symlink class by symlinking a test file, then try removing it and see if it still exists
logger.error(f"Permission denied when creating symlink for {item.log_string}: {e}")
return True
logger.exception(f"Permission denied when creating symlink for {item.log_string}: {e}")
except OSError as e:
if e.errno == 36:
# This will cause a loop if it hits this.. users will need to fix their paths
Expand All @@ -259,11 +255,19 @@ def _symlink(self, item: Union[Movie, Episode]) -> bool:
logger.error(f"OS error when creating symlink for {item.log_string}: {e}")
return False

# Validate the symlink
if not os.path.islink(destination) or not os.path.exists(destination):
logger.error(f"Symlink validation failed for {item.log_string} from source: {source} to destination: {destination}")
if not os.path.islink(destination):
logger.error(f"Symlink validation failed: {destination} is not a symlink for {item.log_string}")
return False
if os.readlink(destination) != source:
logger.error(f"Symlink validation failed: {destination} does not point to {source} for {item.log_string}")
return False
if not os.path.isfile(destination):
logger.error(f"Symlink validation failed: {destination} is not a valid file for {item.log_string}")
return False

item.set("symlinked", True)
item.set("symlinked_at", datetime.now())
item.set("symlinked_times", item.symlinked_times + 1)
return True

def _create_item_folders(self, item: Union[Movie, Show, Season, Episode], filename: str) -> str:
Expand Down

0 comments on commit cbe9012

Please sign in to comment.